diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/SymfonyServerCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/SymfonyServerCodegen.java index b1a0877a473c..95732fb66abf 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/SymfonyServerCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/SymfonyServerCodegen.java @@ -281,6 +281,7 @@ public Map postProcessOperations(Map objs) { operations.put("controllerName", toControllerName((String) operations.get("pathPrefix"))); operations.put("symfonyService", toSymfonyService((String) operations.get("pathPrefix"))); + HashSet authMethods = new HashSet<>(); HashSet imports = new HashSet<>(); List operationList = (List) operations.get("operation"); for (CodegenOperation op : operationList) { @@ -310,9 +311,15 @@ public Map postProcessOperations(Map objs) { imports.add(exception); } } + + // Add operation's authentication methods to whole interface + if (op.authMethods != null) { + authMethods.addAll(op.authMethods); + } } operations.put("imports", new ArrayList<>(imports)); + operations.put("authMethods", authMethods); return objs; } diff --git a/modules/swagger-codegen/src/main/resources/php-symfony/api.mustache b/modules/swagger-codegen/src/main/resources/php-symfony/api.mustache index a55569a38316..0caef2cfc6d4 100644 --- a/modules/swagger-codegen/src/main/resources/php-symfony/api.mustache +++ b/modules/swagger-codegen/src/main/resources/php-symfony/api.mustache @@ -31,6 +31,17 @@ namespace {{apiPackage}}; */ interface {{classname}} { + {{#authMethods}} + + /** + * Sets authentication method {{name}} + * + * @param string $value Value of the {{name}} authentication method. + * + * @return void + */ + public function set{{name}}($value); + {{/authMethods}} {{#operation}} /** diff --git a/modules/swagger-codegen/src/main/resources/php-symfony/api_controller.mustache b/modules/swagger-codegen/src/main/resources/php-symfony/api_controller.mustache index 19b65101ac12..c78a507203ee 100644 --- a/modules/swagger-codegen/src/main/resources/php-symfony/api_controller.mustache +++ b/modules/swagger-codegen/src/main/resources/php-symfony/api_controller.mustache @@ -55,17 +55,38 @@ class {{controllerName}} extends Controller */ public function {{operationId}}Action(Request $request) { + {{#authMethods}} + // Authentication '{{name}}' required + {{#isApiKey}} + {{#isKeyInHeader}} + // Set key with prefix in header + $security{{name}} = $request->headers->get('{{keyParamName}}'); + {{/isKeyInHeader}} + {{#isKeyInQuery}} + // Set key with prefix in query string + $security{{name}} = $request->query->get('{{keyParamName}}'); + {{/isKeyInQuery}} + {{/isApiKey}} + {{#isBasic}} + // HTTP basic authentication required + $security{{name}} = $request->headers->get('authorization'); + {{/isBasic}} + {{#isOAuth}} + // Oauth required + $security{{name}} = $request->headers->get('authorization'); + {{/isOAuth}} + {{/authMethods}} {{#queryParams}} // Handle query params ${{paramName}} = $this->fromQuery($request->query->get('{{paramName}}'), '{{dataType}}'); {{/queryParams}} {{#headerParams}} // Handle header params - ${{paramName}} = $this->fromHeader($request->headers->get('{{paramName}}'), '{{dataType}}'); + ${{paramName}} = $this->fromHeader($request->headers->get('{{paramName}}'), '{{dataType}}'); {{/headerParams}} {{#pathParams}} // Handle path params - ${{paramName}} = $this->fromPath($request->attributes->get('{{paramName}}'), '{{dataType}}'); + ${{paramName}} = $this->fromPath($request->attributes->get('{{paramName}}'), '{{dataType}}'); {{/pathParams}} {{#formParams}} {{#isFile}} @@ -131,9 +152,15 @@ class {{controllerName}} extends Controller // Call the API interface try { + $handler = $this->getApiHandler(); + + {{#authMethods}} + // Set authentication method '{{name}}' + $handler->set{{name}}($security{{name}}); + {{/authMethods}} {{#returnType}} // Expecting a return value (exception otherwise) - $result = $this->getApiHandler()->{{operationId}}({{#allParams}}${{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}); + $result = $handler->{{operationId}}({{#allParams}}${{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}); {{#responses}} {{^vendorExtensions.x-symfonyExceptionSimple}} @@ -148,7 +175,7 @@ class {{controllerName}} extends Controller {{/returnType}} {{^returnType}} // No return type expected; return empty response - $this->getApiHandler()->{{operationId}}({{#allParams}}${{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}); + $handler->{{operationId}}({{#allParams}}${{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}); return new Response('', 204); {{/returnType}} {{#responses}}