From 6675cbc20e5dc7ee72afa271782837e115020913 Mon Sep 17 00:00:00 2001 From: xhh Date: Wed, 25 Nov 2015 17:55:04 +0800 Subject: [PATCH 1/3] Auto generate operationId for python-flask server codegen --- bin/flaskConnexion.sh | 2 +- .../io/swagger/codegen/DefaultCodegen.java | 63 +++++++++++-------- .../languages/FlaskConnexionCodegen.java | 15 +++-- .../server/petstore/flaskConnexion/README.md | 2 +- .../controllers/default_controller.py | 24 +++---- .../flaskConnexion/swagger/swagger.yaml | 48 +++++++------- 6 files changed, 85 insertions(+), 69 deletions(-) diff --git a/bin/flaskConnexion.sh b/bin/flaskConnexion.sh index f300d3124b16..1745d2a3ba2a 100755 --- a/bin/flaskConnexion.sh +++ b/bin/flaskConnexion.sh @@ -26,6 +26,6 @@ fi # if you've executed sbt assembly previously it will use that instead. export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties" -ags="$@ generate -i modules/swagger-codegen/src/test/resources/2_0/petstore.json -l flaskConnexion -o samples/server/petstore/flaskConnexion " +ags="$@ generate -i modules/swagger-codegen/src/test/resources/2_0/petstore.json -l python-flask -o samples/server/petstore/flaskConnexion " java $JAVA_OPTS -Dservice -jar $executable $ags diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java index 99ceca6796c6..f68772361636 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java @@ -1081,31 +1081,7 @@ public class DefaultCodegen { Set imports = new HashSet(); op.vendorExtensions = operation.getVendorExtensions(); - String operationId = operation.getOperationId(); - if (operationId == null) { - String tmpPath = path; - tmpPath = tmpPath.replaceAll("\\{", ""); - tmpPath = tmpPath.replaceAll("\\}", ""); - String[] parts = (tmpPath + "/" + httpMethod).split("/"); - StringBuilder builder = new StringBuilder(); - if ("/".equals(tmpPath)) { - // must be root tmpPath - builder.append("root"); - } - for (int i = 0; i < parts.length; i++) { - String part = parts[i]; - if (part.length() > 0) { - if (builder.toString().length() == 0) { - part = Character.toLowerCase(part.charAt(0)) + part.substring(1); - } else { - part = initialCaps(part); - } - builder.append(part); - } - } - operationId = builder.toString(); - LOGGER.info("generated operationId " + operationId + "\tfor Path: " + httpMethod + " " + path); - } + String operationId = getOrGenerateOperationId(operation, path, httpMethod); operationId = removeNonNameElementToCamelCase(operationId); op.path = path; op.operationId = toOperationId(operationId); @@ -1620,6 +1596,43 @@ public class DefaultCodegen { return secs; } + /** + * Get operationId from the operation object, and if it's blank, generate a new one from the given parameters. + * + * @param operation the operation object + * @param path the path of the operation + * @param httpMethod the HTTP method of the operation + * @return the (generated) operationId + */ + protected String getOrGenerateOperationId(Operation operation, String path, String httpMethod) { + String operationId = operation.getOperationId(); + if (StringUtils.isBlank(operationId)) { + String tmpPath = path; + tmpPath = tmpPath.replaceAll("\\{", ""); + tmpPath = tmpPath.replaceAll("\\}", ""); + String[] parts = (tmpPath + "/" + httpMethod).split("/"); + StringBuilder builder = new StringBuilder(); + if ("/".equals(tmpPath)) { + // must be root tmpPath + builder.append("root"); + } + for (int i = 0; i < parts.length; i++) { + String part = parts[i]; + if (part.length() > 0) { + if (builder.toString().length() == 0) { + part = Character.toLowerCase(part.charAt(0)) + part.substring(1); + } else { + part = initialCaps(part); + } + builder.append(part); + } + } + operationId = builder.toString(); + LOGGER.info("generated operationId " + operationId + "\tfor Path: " + httpMethod + " " + path); + } + return operationId; + } + /** * Check the type to see if it needs import the library/module/package * diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/FlaskConnexionCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/FlaskConnexionCodegen.java index b3de913501a0..62fe01e2fb07 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/FlaskConnexionCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/FlaskConnexionCodegen.java @@ -5,6 +5,7 @@ import com.google.common.collect.ArrayListMultimap; import com.google.common.collect.Lists; import com.google.common.collect.Multimap; import io.swagger.codegen.*; +import io.swagger.models.HttpMethod; import io.swagger.models.Operation; import io.swagger.models.Path; import io.swagger.models.Swagger; @@ -205,13 +206,15 @@ public class FlaskConnexionCodegen extends DefaultCodegen implements CodegenConf for(String pathname : swagger.getPaths().keySet()) { Path path = swagger.getPath(pathname); if(path.getOperations() != null) { - for(Operation operation : path.getOperations()) { - String operationId = operation.getOperationId(); - if(operationId != null && operationId.indexOf(".") == -1) { - operation.setVendorExtension("x-operationId", underscore(sanitizeName(operationId))); - operationId = controllerPackage + "." + defaultController + "." + underscore(sanitizeName(operationId)); - operation.setOperationId(operationId); + for(Map.Entry entry : path.getOperationMap().entrySet()) { + String httpMethod = entry.getKey().name().toLowerCase(); + Operation operation = entry.getValue(); + String operationId = getOrGenerateOperationId(operation, pathname, httpMethod); + String xOperationId = underscore(sanitizeName(operationId)); + if(!operationId.contains(".")) { + operation.setOperationId(controllerPackage + "." + defaultController + "." + xOperationId); } + operation.setVendorExtension("x-operationId", xOperationId); if(operation.getTags() != null) { List> tags = new ArrayList>(); for(String tag : operation.getTags()) { diff --git a/samples/server/petstore/flaskConnexion/README.md b/samples/server/petstore/flaskConnexion/README.md index d85f471ba406..2b8c96e6170e 100644 --- a/samples/server/petstore/flaskConnexion/README.md +++ b/samples/server/petstore/flaskConnexion/README.md @@ -3,7 +3,7 @@ ## Overview This server was generated by the [swagger-codegen](https://github.com/swagger-api/swagger-codegen) project. By using the [swagger-spec](https://github.com/swagger-api/swagger-core/wiki) from a remote server, you can easily generate a server stub. This -is an example of building a swagger-enabled JAX-RS server. +is an example of building a swagger-enabled flask server. This example uses the [connexion](https://github.com/zalando/connexion) library on top of Flask. diff --git a/samples/server/petstore/flaskConnexion/controllers/default_controller.py b/samples/server/petstore/flaskConnexion/controllers/default_controller.py index 8d771afed91c..d42c48575d1e 100644 --- a/samples/server/petstore/flaskConnexion/controllers/default_controller.py +++ b/samples/server/petstore/flaskConnexion/controllers/default_controller.py @@ -23,6 +23,18 @@ def update_user(username, body) -> str: def delete_user(username) -> str: return 'do some magic!' +def get_inventory() -> str: + return 'do some magic!' + +def place_order(body) -> str: + return 'do some magic!' + +def get_order_by_id(orderId) -> str: + return 'do some magic!' + +def delete_order(orderId) -> str: + return 'do some magic!' + def update_pet(body) -> str: return 'do some magic!' @@ -46,15 +58,3 @@ def delete_pet(petId, apiKey) -> str: def upload_file(petId, additionalMetadata, file) -> str: return 'do some magic!' - -def get_inventory() -> str: - return 'do some magic!' - -def place_order(body) -> str: - return 'do some magic!' - -def get_order_by_id(orderId) -> str: - return 'do some magic!' - -def delete_order(orderId) -> str: - return 'do some magic!' diff --git a/samples/server/petstore/flaskConnexion/swagger/swagger.yaml b/samples/server/petstore/flaskConnexion/swagger/swagger.yaml index 047d1652a87e..e6455eeab987 100644 --- a/samples/server/petstore/flaskConnexion/swagger/swagger.yaml +++ b/samples/server/petstore/flaskConnexion/swagger/swagger.yaml @@ -45,9 +45,9 @@ paths: - petstore_auth: - "write:pets" - "read:pets" - x-operationId: "add_pet" x-tags: - tag: "pet" + x-operationId: "add_pet" put: tags: - "pet" @@ -78,9 +78,9 @@ paths: - petstore_auth: - "write:pets" - "read:pets" - x-operationId: "update_pet" x-tags: - tag: "pet" + x-operationId: "update_pet" /pet/findByStatus: get: tags: @@ -114,9 +114,9 @@ paths: - petstore_auth: - "write:pets" - "read:pets" - x-operationId: "find_pets_by_status" x-tags: - tag: "pet" + x-operationId: "find_pets_by_status" /pet/findByTags: get: tags: @@ -150,9 +150,9 @@ paths: - petstore_auth: - "write:pets" - "read:pets" - x-operationId: "find_pets_by_tags" x-tags: - tag: "pet" + x-operationId: "find_pets_by_tags" /pet/{petId}: get: tags: @@ -185,9 +185,9 @@ paths: - petstore_auth: - "write:pets" - "read:pets" - x-operationId: "get_pet_by_id" x-tags: - tag: "pet" + x-operationId: "get_pet_by_id" post: tags: - "pet" @@ -222,9 +222,9 @@ paths: - petstore_auth: - "write:pets" - "read:pets" - x-operationId: "update_pet_with_form" x-tags: - tag: "pet" + x-operationId: "update_pet_with_form" delete: tags: - "pet" @@ -253,9 +253,9 @@ paths: - petstore_auth: - "write:pets" - "read:pets" - x-operationId: "delete_pet" x-tags: - tag: "pet" + x-operationId: "delete_pet" /pet/{petId}/uploadImage: post: tags: @@ -292,9 +292,9 @@ paths: - petstore_auth: - "write:pets" - "read:pets" - x-operationId: "upload_file" x-tags: - tag: "pet" + x-operationId: "upload_file" /store/inventory: get: tags: @@ -316,9 +316,9 @@ paths: format: "int32" security: - api_key: [] - x-operationId: "get_inventory" x-tags: - tag: "store" + x-operationId: "get_inventory" /store/order: post: tags: @@ -343,9 +343,9 @@ paths: $ref: "#/definitions/Order" 400: description: "Invalid Order" - x-operationId: "place_order" x-tags: - tag: "store" + x-operationId: "place_order" /store/order/{orderId}: get: tags: @@ -372,9 +372,9 @@ paths: description: "Invalid ID supplied" 404: description: "Order not found" - x-operationId: "get_order_by_id" x-tags: - tag: "store" + x-operationId: "get_order_by_id" delete: tags: - "store" @@ -396,9 +396,9 @@ paths: description: "Invalid ID supplied" 404: description: "Order not found" - x-operationId: "delete_order" x-tags: - tag: "store" + x-operationId: "delete_order" /user: post: tags: @@ -419,9 +419,9 @@ paths: responses: default: description: "successful operation" - x-operationId: "create_user" x-tags: - tag: "user" + x-operationId: "create_user" /user/createWithArray: post: tags: @@ -444,9 +444,9 @@ paths: responses: default: description: "successful operation" - x-operationId: "create_users_with_array_input" x-tags: - tag: "user" + x-operationId: "create_users_with_array_input" /user/createWithList: post: tags: @@ -469,9 +469,9 @@ paths: responses: default: description: "successful operation" - x-operationId: "create_users_with_list_input" x-tags: - tag: "user" + x-operationId: "create_users_with_list_input" /user/login: get: tags: @@ -500,9 +500,9 @@ paths: type: "string" 400: description: "Invalid username/password supplied" - x-operationId: "login_user" x-tags: - tag: "user" + x-operationId: "login_user" /user/logout: get: tags: @@ -517,9 +517,9 @@ paths: responses: default: description: "successful operation" - x-operationId: "logout_user" x-tags: - tag: "user" + x-operationId: "logout_user" /user/{username}: get: tags: @@ -555,9 +555,9 @@ paths: description: "Invalid username supplied" 404: description: "User not found" - x-operationId: "get_user_by_name" x-tags: - tag: "user" + x-operationId: "get_user_by_name" put: tags: - "user" @@ -584,9 +584,9 @@ paths: description: "Invalid user supplied" 404: description: "User not found" - x-operationId: "update_user" x-tags: - tag: "user" + x-operationId: "update_user" delete: tags: - "user" @@ -607,14 +607,10 @@ paths: description: "Invalid username supplied" 404: description: "User not found" - x-operationId: "delete_user" x-tags: - tag: "user" + x-operationId: "delete_user" securityDefinitions: - api_key: - type: "apiKey" - name: "api_key" - in: "header" petstore_auth: type: "oauth2" authorizationUrl: "http://petstore.swagger.io/api/oauth/dialog" @@ -622,6 +618,10 @@ securityDefinitions: scopes: write:pets: "modify pets in your account" read:pets: "read your pets" + api_key: + type: "apiKey" + name: "api_key" + in: "header" definitions: User: properties: From 801efaa3d4a7848e99482305edf73c0192d17514 Mon Sep 17 00:00:00 2001 From: xhh Date: Wed, 25 Nov 2015 18:13:39 +0800 Subject: [PATCH 2/3] Replace "nickname" with "operationId" in some API templates --- .../src/main/resources/Java/api.mustache | 4 ++-- .../resources/Java/libraries/jersey2/api.mustache | 4 ++-- .../Java/libraries/okhttp-gson/api.mustache | 14 +++++++------- .../resources/Java/libraries/retrofit/api.mustache | 4 ++-- .../Java/libraries/retrofit2/api.mustache | 2 +- .../src/main/resources/JavaInflector/api.mustache | 2 +- .../src/main/resources/JavaJaxRS/api.mustache | 4 ++-- .../src/main/resources/JavaSpringMVC/api.mustache | 2 +- .../src/main/resources/clojure/api.mustache | 4 ++-- .../src/main/resources/codegen/api.template | 4 ++-- .../src/main/resources/ruby/api.mustache | 10 +++++----- .../src/main/resources/scala/api.mustache | 2 +- 12 files changed, 28 insertions(+), 28 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/Java/api.mustache b/modules/swagger-codegen/src/main/resources/Java/api.mustache index 3ac280b2d706..5f617a253f14 100644 --- a/modules/swagger-codegen/src/main/resources/Java/api.mustache +++ b/modules/swagger-codegen/src/main/resources/Java/api.mustache @@ -41,13 +41,13 @@ public class {{classname}} { {{#allParams}} * @param {{paramName}} {{description}} {{/allParams}} * @return {{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}} */ - public {{#returnType}}{{{returnType}}} {{/returnType}}{{^returnType}}void {{/returnType}}{{nickname}} ({{#allParams}}{{{dataType}}} {{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) throws ApiException { + public {{#returnType}}{{{returnType}}} {{/returnType}}{{^returnType}}void {{/returnType}}{{operationId}}({{#allParams}}{{{dataType}}} {{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) throws ApiException { Object {{localVariablePrefix}}postBody = {{#bodyParam}}{{^isBinary}}{{paramName}}{{/isBinary}}{{#isBinary}}null{{/isBinary}}{{/bodyParam}}{{^bodyParam}}null{{/bodyParam}}; byte[] {{localVariablePrefix}}postBinaryBody = {{#bodyParam}}{{#isBinary}}{{paramName}}{{/isBinary}}{{^isBinary}}null{{/isBinary}}{{/bodyParam}}{{^bodyParam}}null{{/bodyParam}}; {{#allParams}}{{#required}} // verify the required parameter '{{paramName}}' is set if ({{paramName}} == null) { - throw new ApiException(400, "Missing the required parameter '{{paramName}}' when calling {{nickname}}"); + throw new ApiException(400, "Missing the required parameter '{{paramName}}' when calling {{operationId}}"); } {{/required}}{{/allParams}} // create path and map variables diff --git a/modules/swagger-codegen/src/main/resources/Java/libraries/jersey2/api.mustache b/modules/swagger-codegen/src/main/resources/Java/libraries/jersey2/api.mustache index 10f2fa2f7446..0d5c3bcf7f0b 100644 --- a/modules/swagger-codegen/src/main/resources/Java/libraries/jersey2/api.mustache +++ b/modules/swagger-codegen/src/main/resources/Java/libraries/jersey2/api.mustache @@ -41,12 +41,12 @@ public class {{classname}} { {{#allParams}} * @param {{paramName}} {{description}} {{/allParams}} * @return {{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}} */ - public {{#returnType}}{{{returnType}}} {{/returnType}}{{^returnType}}void {{/returnType}}{{nickname}} ({{#allParams}}{{{dataType}}} {{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) throws ApiException { + public {{#returnType}}{{{returnType}}} {{/returnType}}{{^returnType}}void {{/returnType}}{{operationId}}({{#allParams}}{{{dataType}}} {{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) throws ApiException { Object {{localVariablePrefix}}postBody = {{#bodyParam}}{{paramName}}{{/bodyParam}}{{^bodyParam}}null{{/bodyParam}}; {{#allParams}}{{#required}} // verify the required parameter '{{paramName}}' is set if ({{paramName}} == null) { - throw new ApiException(400, "Missing the required parameter '{{paramName}}' when calling {{nickname}}"); + throw new ApiException(400, "Missing the required parameter '{{paramName}}' when calling {{operationId}}"); } {{/required}}{{/allParams}} // create path and map variables diff --git a/modules/swagger-codegen/src/main/resources/Java/libraries/okhttp-gson/api.mustache b/modules/swagger-codegen/src/main/resources/Java/libraries/okhttp-gson/api.mustache index efb1a14f27d7..1bf63010a1e1 100644 --- a/modules/swagger-codegen/src/main/resources/Java/libraries/okhttp-gson/api.mustache +++ b/modules/swagger-codegen/src/main/resources/Java/libraries/okhttp-gson/api.mustache @@ -39,13 +39,13 @@ public class {{classname}} { } {{#operation}} - /* Build call for {{nickname}} */ - private Call {{nickname}}Call({{#allParams}}{{{dataType}}} {{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) throws ApiException { + /* Build call for {{operationId}} */ + private Call {{operationId}}Call({{#allParams}}{{{dataType}}} {{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) throws ApiException { Object {{localVariablePrefix}}postBody = {{#bodyParam}}{{paramName}}{{/bodyParam}}{{^bodyParam}}null{{/bodyParam}}; {{#allParams}}{{#required}} // verify the required parameter '{{paramName}}' is set if ({{paramName}} == null) { - throw new ApiException("Missing the required parameter '{{paramName}}' when calling {{nickname}}(Async)"); + throw new ApiException("Missing the required parameter '{{paramName}}' when calling {{operationId}}(Async)"); } {{/required}}{{/allParams}} @@ -87,8 +87,8 @@ public class {{classname}} { * @param {{paramName}} {{description}}{{/allParams}}{{#returnType}} * @return {{{returnType}}}{{/returnType}} */ - public {{#returnType}}{{{returnType}}} {{/returnType}}{{^returnType}}void {{/returnType}}{{nickname}}({{#allParams}}{{{dataType}}} {{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) throws ApiException { - Call {{localVariablePrefix}}call = {{nickname}}Call({{#allParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}); + public {{#returnType}}{{{returnType}}} {{/returnType}}{{^returnType}}void {{/returnType}}{{operationId}}({{#allParams}}{{{dataType}}} {{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) throws ApiException { + Call {{localVariablePrefix}}call = {{operationId}}Call({{#allParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}); {{#returnType}}Type {{localVariablePrefix}}returnType = new TypeToken<{{{returnType}}}>(){}.getType(); return {{localVariablePrefix}}apiClient.execute({{localVariablePrefix}}call, {{localVariablePrefix}}returnType);{{/returnType}}{{^returnType}}{{localVariablePrefix}}apiClient.execute({{localVariablePrefix}}call);{{/returnType}} } @@ -100,8 +100,8 @@ public class {{classname}} { * @param callback The callback to be executed when the API call finishes * @return The request call */ - public Call {{nickname}}Async({{#allParams}}{{{dataType}}} {{paramName}}, {{/allParams}}ApiCallback<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Void{{/returnType}}> {{localVariablePrefix}}callback) throws ApiException { - Call {{localVariablePrefix}}call = {{nickname}}Call({{#allParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}); + public Call {{operationId}}Async({{#allParams}}{{{dataType}}} {{paramName}}, {{/allParams}}ApiCallback<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Void{{/returnType}}> {{localVariablePrefix}}callback) throws ApiException { + Call {{localVariablePrefix}}call = {{operationId}}Call({{#allParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}); {{#returnType}}Type {{localVariablePrefix}}returnType = new TypeToken<{{{returnType}}}>(){}.getType(); {{localVariablePrefix}}apiClient.executeAsync({{localVariablePrefix}}call, {{localVariablePrefix}}returnType, {{localVariablePrefix}}callback);{{/returnType}}{{^returnType}}{{localVariablePrefix}}apiClient.executeAsync({{localVariablePrefix}}call, {{localVariablePrefix}}callback);{{/returnType}} return {{localVariablePrefix}}call; diff --git a/modules/swagger-codegen/src/main/resources/Java/libraries/retrofit/api.mustache b/modules/swagger-codegen/src/main/resources/Java/libraries/retrofit/api.mustache index 84c799d62520..9d758bddd212 100644 --- a/modules/swagger-codegen/src/main/resources/Java/libraries/retrofit/api.mustache +++ b/modules/swagger-codegen/src/main/resources/Java/libraries/retrofit/api.mustache @@ -26,7 +26,7 @@ public interface {{classname}} { {{#formParams}}{{#-first}} {{#isMultipart}}@Multipart{{/isMultipart}}{{^isMultipart}}@FormUrlEncoded{{/isMultipart}}{{/-first}}{{/formParams}} @{{httpMethod}}("{{path}}") - {{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Object{{/returnType}} {{nickname}}({{^allParams}});{{/allParams}} + {{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Object{{/returnType}} {{operationId}}({{^allParams}});{{/allParams}} {{#allParams}}{{>libraries/retrofit/queryParams}}{{>libraries/retrofit/pathParams}}{{>libraries/retrofit/headerParams}}{{>libraries/retrofit/bodyParams}}{{>libraries/retrofit/formParams}}{{#hasMore}}, {{/hasMore}}{{^hasMore}} );{{/hasMore}}{{/allParams}} @@ -40,7 +40,7 @@ public interface {{classname}} { {{#formParams}}{{#-first}} {{#isMultipart}}@Multipart{{/isMultipart}}{{^isMultipart}}@FormUrlEncoded{{/isMultipart}}{{/-first}}{{/formParams}} @{{httpMethod}}("{{path}}") - void {{nickname}}( + void {{operationId}}( {{#allParams}}{{>libraries/retrofit/queryParams}}{{>libraries/retrofit/pathParams}}{{>libraries/retrofit/headerParams}}{{>libraries/retrofit/bodyParams}}{{>libraries/retrofit/formParams}}, {{/allParams}}Callback<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Object{{/returnType}}> cb ); {{/operation}} diff --git a/modules/swagger-codegen/src/main/resources/Java/libraries/retrofit2/api.mustache b/modules/swagger-codegen/src/main/resources/Java/libraries/retrofit2/api.mustache index d0205c3d0b1e..a066a625cffa 100644 --- a/modules/swagger-codegen/src/main/resources/Java/libraries/retrofit2/api.mustache +++ b/modules/swagger-codegen/src/main/resources/Java/libraries/retrofit2/api.mustache @@ -25,7 +25,7 @@ public interface {{classname}} { {{#formParams}}{{#-first}} {{#isMultipart}}@Multipart{{/isMultipart}}{{^isMultipart}}@FormUrlEncoded{{/isMultipart}}{{/-first}}{{/formParams}} @{{httpMethod}}("{{path}}") - Call<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Object{{/returnType}}> {{nickname}}({{^allParams}});{{/allParams}} + Call<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Object{{/returnType}}> {{operationId}}({{^allParams}});{{/allParams}} {{#allParams}}{{>libraries/retrofit2/queryParams}}{{>libraries/retrofit2/pathParams}}{{>libraries/retrofit2/headerParams}}{{>libraries/retrofit2/bodyParams}}{{>libraries/retrofit2/formParams}}{{#hasMore}}, {{/hasMore}}{{^hasMore}} );{{/hasMore}}{{/allParams}} diff --git a/modules/swagger-codegen/src/main/resources/JavaInflector/api.mustache b/modules/swagger-codegen/src/main/resources/JavaInflector/api.mustache index 74e688ee23e0..0a5e225306ce 100644 --- a/modules/swagger-codegen/src/main/resources/JavaInflector/api.mustache +++ b/modules/swagger-codegen/src/main/resources/JavaInflector/api.mustache @@ -24,7 +24,7 @@ public class {{classname}} { {{#operation}} /* - public ResponseContext {{nickname}}(RequestContext request {{#allParams}}, {{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{/allParams}}) { + public ResponseContext {{operationId}}(RequestContext request {{#allParams}}, {{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{/allParams}}) { return new ResponseContext().status(Status.INTERNAL_SERVER_ERROR).entity( "Not implemented" ); } */ diff --git a/modules/swagger-codegen/src/main/resources/JavaJaxRS/api.mustache b/modules/swagger-codegen/src/main/resources/JavaJaxRS/api.mustache index ea9f2518b019..da3eba49461a 100644 --- a/modules/swagger-codegen/src/main/resources/JavaJaxRS/api.mustache +++ b/modules/swagger-codegen/src/main/resources/JavaJaxRS/api.mustache @@ -47,10 +47,10 @@ public class {{classname}} { @io.swagger.annotations.ApiResponse(code = {{{code}}}, message = "{{{message}}}", response = {{{returnType}}}.class{{#returnContainer}}, responseContainer = "{{{returnContainer}}}"{{/returnContainer}}){{#hasMore}}, {{/hasMore}}{{/responses}} }) - public Response {{nickname}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{#hasMore}}, + public Response {{operationId}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) throws NotFoundException { - return delegate.{{nickname}}({{#allParams}}{{#isFile}}fileDetail{{/isFile}}{{^isFile}}{{paramName}}{{/isFile}}{{#hasMore}},{{/hasMore}}{{/allParams}}); + return delegate.{{operationId}}({{#allParams}}{{#isFile}}fileDetail{{/isFile}}{{^isFile}}{{paramName}}{{/isFile}}{{#hasMore}},{{/hasMore}}{{/allParams}}); } {{/operation}} } diff --git a/modules/swagger-codegen/src/main/resources/JavaSpringMVC/api.mustache b/modules/swagger-codegen/src/main/resources/JavaSpringMVC/api.mustache index 6eb6358a7026..a882c0069754 100644 --- a/modules/swagger-codegen/src/main/resources/JavaSpringMVC/api.mustache +++ b/modules/swagger-codegen/src/main/resources/JavaSpringMVC/api.mustache @@ -50,7 +50,7 @@ public class {{classname}} { {{#hasProduces}}produces = { {{#produces}}"{{mediaType}}"{{#hasMore}}, {{/hasMore}}{{/produces}} }, {{/hasProduces}} {{#hasConsumes}}consumes = { {{#consumes}}"{{mediaType}}"{{#hasMore}}, {{/hasMore}}{{/consumes}} },{{/hasConsumes}} method = RequestMethod.{{httpMethod}}) - public ResponseEntity<{{>returnTypes}}> {{nickname}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{#hasMore}}, + public ResponseEntity<{{>returnTypes}}> {{operationId}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) throws NotFoundException { // do some magic! diff --git a/modules/swagger-codegen/src/main/resources/clojure/api.mustache b/modules/swagger-codegen/src/main/resources/clojure/api.mustache index 1acdf94d764a..94e95dffaf76 100644 --- a/modules/swagger-codegen/src/main/resources/clojure/api.mustache +++ b/modules/swagger-codegen/src/main/resources/clojure/api.mustache @@ -2,10 +2,10 @@ (:require [.core :refer [call-api check-required-params]]) (:import (java.io File))) <#operations><#operation> -(defn +(defn "<&summary><#notes> <¬es>"<#hasOptionalParams> - ([<#allParams><#required><#isFile>^File ] (<#allParams><#required> nil)) + ([<#allParams><#required><#isFile>^File ] (<#allParams><#required> nil)) <#hasOptionalParams>([<#allParams><#required><#isFile>^File <#hasOptionalParams>{:keys [<#allParams><^required><#isFile>^File ]}]<#hasRequiredParams> <#hasOptionalParams> (check-required-params<#allParams><#required> ) <#hasOptionalParams> (call-api "" : diff --git a/modules/swagger-codegen/src/main/resources/codegen/api.template b/modules/swagger-codegen/src/main/resources/codegen/api.template index 9dbc8dd4ba13..62802f7f1e78 100644 --- a/modules/swagger-codegen/src/main/resources/codegen/api.template +++ b/modules/swagger-codegen/src/main/resources/codegen/api.template @@ -14,8 +14,8 @@ classname: {{classname}} # loop over each operation in the API: {{#operation}} -# each operation has a `nickname`: -nickname: {{nickname}} +# each operation has an `operationId`: +operationId: {{operationId}} # and parameters: {{#allParams}} diff --git a/modules/swagger-codegen/src/main/resources/ruby/api.mustache b/modules/swagger-codegen/src/main/resources/ruby/api.mustache index a43260c06079..b8e2e0da4c88 100644 --- a/modules/swagger-codegen/src/main/resources/ruby/api.mustache +++ b/modules/swagger-codegen/src/main/resources/ruby/api.mustache @@ -16,13 +16,13 @@ module {{moduleName}} {{/required}}{{/allParams}} # @param [Hash] opts the optional parameters {{#allParams}}{{^required}} # @option opts [{{{dataType}}}] :{{paramName}} {{description}} {{/required}}{{/allParams}} # @return [{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}nil{{/returnType}}] - def {{nickname}}({{#allParams}}{{#required}}{{paramName}}, {{/required}}{{/allParams}}opts = {}) + def {{operationId}}({{#allParams}}{{#required}}{{paramName}}, {{/required}}{{/allParams}}opts = {}) if Configuration.debugging - Configuration.logger.debug "Calling API: {{classname}}#{{nickname}} ..." + Configuration.logger.debug "Calling API: {{classname}}#{{operationId}} ..." end {{#allParams}}{{#required}} # verify the required parameter '{{paramName}}' is set - fail "Missing the required parameter '{{paramName}}' when calling {{nickname}}" if {{{paramName}}}.nil?{{#isEnum}} + fail "Missing the required parameter '{{paramName}}' when calling {{operationId}}" if {{{paramName}}}.nil?{{#isEnum}} unless [{{#allowableValues}}{{#values}}'{{{this}}}'{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}].include?({{{paramName}}}) fail "invalid value for '{{{paramName}}}', must be one of {{#allowableValues}}{{#values}}{{{this}}}{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}" end{{/isEnum}} @@ -71,7 +71,7 @@ module {{moduleName}} :auth_names => auth_names, :return_type => '{{{returnType}}}') if Configuration.debugging - Configuration.logger.debug "API called: {{classname}}#{{nickname}}. Result: #{result.inspect}" + Configuration.logger.debug "API called: {{classname}}#{{operationId}}. Result: #{result.inspect}" end return result{{/returnType}}{{^returnType}}@api_client.call_api(:{{httpMethod}}, path, :header_params => header_params, @@ -80,7 +80,7 @@ module {{moduleName}} :body => post_body, :auth_names => auth_names) if Configuration.debugging - Configuration.logger.debug "API called: {{classname}}#{{nickname}}" + Configuration.logger.debug "API called: {{classname}}#{{operationId}}" end return nil{{/returnType}} end diff --git a/modules/swagger-codegen/src/main/resources/scala/api.mustache b/modules/swagger-codegen/src/main/resources/scala/api.mustache index 87b04b5900f7..3b74dca81626 100644 --- a/modules/swagger-codegen/src/main/resources/scala/api.mustache +++ b/modules/swagger-codegen/src/main/resources/scala/api.mustache @@ -30,7 +30,7 @@ class {{classname}}(val defBasePath: String = "{{basePath}}", {{#allParams}} * @param {{paramName}} {{description}} {{/allParams}} * @return {{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}} */ - def {{nickname}} ({{#allParams}}{{paramName}}: {{dataType}}{{#defaultValue}} /* = {{{defaultValue}}} */{{/defaultValue}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) {{#returnType}}: Option[{{returnType}}]{{/returnType}} = { + def {{operationId}} ({{#allParams}}{{paramName}}: {{dataType}}{{#defaultValue}} /* = {{{defaultValue}}} */{{/defaultValue}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) {{#returnType}}: Option[{{returnType}}]{{/returnType}} = { // create path and map variables val path = "{{path}}".replaceAll("\\{format\\}","json"){{#pathParams}}.replaceAll("\\{" + "{{baseName}}" + "\\}",apiInvoker.escape({{paramName}})) From 27e43d3fbbc9766b31a6b54d30e00ac303688516 Mon Sep 17 00:00:00 2001 From: xhh Date: Wed, 25 Nov 2015 19:41:40 +0800 Subject: [PATCH 3/3] Implement toOperationId for python-flask server codegen and use "operationId" in the template to replace vendorExtensions.x-operationId --- .../languages/FlaskConnexionCodegen.java | 19 +++++++++++++++--- .../flaskConnexion/controller.mustache | 2 +- .../flaskConnexion/swagger/swagger.yaml | 20 ------------------- 3 files changed, 17 insertions(+), 24 deletions(-) diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/FlaskConnexionCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/FlaskConnexionCodegen.java index 62fe01e2fb07..7a06ee8b845f 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/FlaskConnexionCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/FlaskConnexionCodegen.java @@ -207,14 +207,16 @@ public class FlaskConnexionCodegen extends DefaultCodegen implements CodegenConf Path path = swagger.getPath(pathname); if(path.getOperations() != null) { for(Map.Entry entry : path.getOperationMap().entrySet()) { + // Normalize `operationId` and add package/class path in front, e.g. + // controllers.default_controller.add_pet String httpMethod = entry.getKey().name().toLowerCase(); Operation operation = entry.getValue(); String operationId = getOrGenerateOperationId(operation, pathname, httpMethod); - String xOperationId = underscore(sanitizeName(operationId)); if(!operationId.contains(".")) { - operation.setOperationId(controllerPackage + "." + defaultController + "." + xOperationId); + operationId = underscore(sanitizeName(operationId)); + operationId = controllerPackage + "." + defaultController + "." + operationId; } - operation.setVendorExtension("x-operationId", xOperationId); + operation.setOperationId(operationId); if(operation.getTags() != null) { List> tags = new ArrayList>(); for(String tag : operation.getTags()) { @@ -295,4 +297,15 @@ public class FlaskConnexionCodegen extends DefaultCodegen implements CodegenConf } return super.postProcessSupportingFileData(objs); } + + @Override + public String toOperationId(String operationId) { + operationId = super.toOperationId(operationId); + // Use the part after the last dot, e.g. + // controllers.defaultController.addPet => addPet + operationId = operationId.replaceAll(".*\\.", ""); + // Need to underscore it since it has been processed via removeNonNameElementToCamelCase, e.g. + // addPet => add_pet + return underscore(operationId); + } } \ No newline at end of file diff --git a/modules/swagger-codegen/src/main/resources/flaskConnexion/controller.mustache b/modules/swagger-codegen/src/main/resources/flaskConnexion/controller.mustache index cb8544aa1b43..06b3c63fdb06 100644 --- a/modules/swagger-codegen/src/main/resources/flaskConnexion/controller.mustache +++ b/modules/swagger-codegen/src/main/resources/flaskConnexion/controller.mustache @@ -3,7 +3,7 @@ {{#operations}} {{#operation}} -def {{vendorExtensions.x-operationId}}({{#allParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) -> str: +def {{operationId}}({{#allParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) -> str: return 'do some magic!' {{/operation}} {{/operations}} diff --git a/samples/server/petstore/flaskConnexion/swagger/swagger.yaml b/samples/server/petstore/flaskConnexion/swagger/swagger.yaml index e6455eeab987..1a4ff85e60ab 100644 --- a/samples/server/petstore/flaskConnexion/swagger/swagger.yaml +++ b/samples/server/petstore/flaskConnexion/swagger/swagger.yaml @@ -47,7 +47,6 @@ paths: - "read:pets" x-tags: - tag: "pet" - x-operationId: "add_pet" put: tags: - "pet" @@ -80,7 +79,6 @@ paths: - "read:pets" x-tags: - tag: "pet" - x-operationId: "update_pet" /pet/findByStatus: get: tags: @@ -116,7 +114,6 @@ paths: - "read:pets" x-tags: - tag: "pet" - x-operationId: "find_pets_by_status" /pet/findByTags: get: tags: @@ -152,7 +149,6 @@ paths: - "read:pets" x-tags: - tag: "pet" - x-operationId: "find_pets_by_tags" /pet/{petId}: get: tags: @@ -187,7 +183,6 @@ paths: - "read:pets" x-tags: - tag: "pet" - x-operationId: "get_pet_by_id" post: tags: - "pet" @@ -224,7 +219,6 @@ paths: - "read:pets" x-tags: - tag: "pet" - x-operationId: "update_pet_with_form" delete: tags: - "pet" @@ -255,7 +249,6 @@ paths: - "read:pets" x-tags: - tag: "pet" - x-operationId: "delete_pet" /pet/{petId}/uploadImage: post: tags: @@ -294,7 +287,6 @@ paths: - "read:pets" x-tags: - tag: "pet" - x-operationId: "upload_file" /store/inventory: get: tags: @@ -318,7 +310,6 @@ paths: - api_key: [] x-tags: - tag: "store" - x-operationId: "get_inventory" /store/order: post: tags: @@ -345,7 +336,6 @@ paths: description: "Invalid Order" x-tags: - tag: "store" - x-operationId: "place_order" /store/order/{orderId}: get: tags: @@ -374,7 +364,6 @@ paths: description: "Order not found" x-tags: - tag: "store" - x-operationId: "get_order_by_id" delete: tags: - "store" @@ -398,7 +387,6 @@ paths: description: "Order not found" x-tags: - tag: "store" - x-operationId: "delete_order" /user: post: tags: @@ -421,7 +409,6 @@ paths: description: "successful operation" x-tags: - tag: "user" - x-operationId: "create_user" /user/createWithArray: post: tags: @@ -446,7 +433,6 @@ paths: description: "successful operation" x-tags: - tag: "user" - x-operationId: "create_users_with_array_input" /user/createWithList: post: tags: @@ -471,7 +457,6 @@ paths: description: "successful operation" x-tags: - tag: "user" - x-operationId: "create_users_with_list_input" /user/login: get: tags: @@ -502,7 +487,6 @@ paths: description: "Invalid username/password supplied" x-tags: - tag: "user" - x-operationId: "login_user" /user/logout: get: tags: @@ -519,7 +503,6 @@ paths: description: "successful operation" x-tags: - tag: "user" - x-operationId: "logout_user" /user/{username}: get: tags: @@ -557,7 +540,6 @@ paths: description: "User not found" x-tags: - tag: "user" - x-operationId: "get_user_by_name" put: tags: - "user" @@ -586,7 +568,6 @@ paths: description: "User not found" x-tags: - tag: "user" - x-operationId: "update_user" delete: tags: - "user" @@ -609,7 +590,6 @@ paths: description: "User not found" x-tags: - tag: "user" - x-operationId: "delete_user" securityDefinitions: petstore_auth: type: "oauth2"