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: