Implement toOperationId for python-flask server codegen

and use "operationId" in the template to replace
vendorExtensions.x-operationId
This commit is contained in:
xhh 2015-11-25 19:41:40 +08:00
parent 801efaa3d4
commit 27e43d3fbb
3 changed files with 17 additions and 24 deletions

View File

@ -207,14 +207,16 @@ public class FlaskConnexionCodegen extends DefaultCodegen implements CodegenConf
Path path = swagger.getPath(pathname); Path path = swagger.getPath(pathname);
if(path.getOperations() != null) { if(path.getOperations() != null) {
for(Map.Entry<HttpMethod, Operation> entry : path.getOperationMap().entrySet()) { for(Map.Entry<HttpMethod, Operation> 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(); String httpMethod = entry.getKey().name().toLowerCase();
Operation operation = entry.getValue(); Operation operation = entry.getValue();
String operationId = getOrGenerateOperationId(operation, pathname, httpMethod); String operationId = getOrGenerateOperationId(operation, pathname, httpMethod);
String xOperationId = underscore(sanitizeName(operationId));
if(!operationId.contains(".")) { 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) { if(operation.getTags() != null) {
List<Map<String, String>> tags = new ArrayList<Map<String, String>>(); List<Map<String, String>> tags = new ArrayList<Map<String, String>>();
for(String tag : operation.getTags()) { for(String tag : operation.getTags()) {
@ -295,4 +297,15 @@ public class FlaskConnexionCodegen extends DefaultCodegen implements CodegenConf
} }
return super.postProcessSupportingFileData(objs); 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);
}
} }

View File

@ -3,7 +3,7 @@
{{#operations}} {{#operations}}
{{#operation}} {{#operation}}
def {{vendorExtensions.x-operationId}}({{#allParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) -> str: def {{operationId}}({{#allParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) -> str:
return 'do some magic!' return 'do some magic!'
{{/operation}} {{/operation}}
{{/operations}} {{/operations}}

View File

@ -47,7 +47,6 @@ paths:
- "read:pets" - "read:pets"
x-tags: x-tags:
- tag: "pet" - tag: "pet"
x-operationId: "add_pet"
put: put:
tags: tags:
- "pet" - "pet"
@ -80,7 +79,6 @@ paths:
- "read:pets" - "read:pets"
x-tags: x-tags:
- tag: "pet" - tag: "pet"
x-operationId: "update_pet"
/pet/findByStatus: /pet/findByStatus:
get: get:
tags: tags:
@ -116,7 +114,6 @@ paths:
- "read:pets" - "read:pets"
x-tags: x-tags:
- tag: "pet" - tag: "pet"
x-operationId: "find_pets_by_status"
/pet/findByTags: /pet/findByTags:
get: get:
tags: tags:
@ -152,7 +149,6 @@ paths:
- "read:pets" - "read:pets"
x-tags: x-tags:
- tag: "pet" - tag: "pet"
x-operationId: "find_pets_by_tags"
/pet/{petId}: /pet/{petId}:
get: get:
tags: tags:
@ -187,7 +183,6 @@ paths:
- "read:pets" - "read:pets"
x-tags: x-tags:
- tag: "pet" - tag: "pet"
x-operationId: "get_pet_by_id"
post: post:
tags: tags:
- "pet" - "pet"
@ -224,7 +219,6 @@ paths:
- "read:pets" - "read:pets"
x-tags: x-tags:
- tag: "pet" - tag: "pet"
x-operationId: "update_pet_with_form"
delete: delete:
tags: tags:
- "pet" - "pet"
@ -255,7 +249,6 @@ paths:
- "read:pets" - "read:pets"
x-tags: x-tags:
- tag: "pet" - tag: "pet"
x-operationId: "delete_pet"
/pet/{petId}/uploadImage: /pet/{petId}/uploadImage:
post: post:
tags: tags:
@ -294,7 +287,6 @@ paths:
- "read:pets" - "read:pets"
x-tags: x-tags:
- tag: "pet" - tag: "pet"
x-operationId: "upload_file"
/store/inventory: /store/inventory:
get: get:
tags: tags:
@ -318,7 +310,6 @@ paths:
- api_key: [] - api_key: []
x-tags: x-tags:
- tag: "store" - tag: "store"
x-operationId: "get_inventory"
/store/order: /store/order:
post: post:
tags: tags:
@ -345,7 +336,6 @@ paths:
description: "Invalid Order" description: "Invalid Order"
x-tags: x-tags:
- tag: "store" - tag: "store"
x-operationId: "place_order"
/store/order/{orderId}: /store/order/{orderId}:
get: get:
tags: tags:
@ -374,7 +364,6 @@ paths:
description: "Order not found" description: "Order not found"
x-tags: x-tags:
- tag: "store" - tag: "store"
x-operationId: "get_order_by_id"
delete: delete:
tags: tags:
- "store" - "store"
@ -398,7 +387,6 @@ paths:
description: "Order not found" description: "Order not found"
x-tags: x-tags:
- tag: "store" - tag: "store"
x-operationId: "delete_order"
/user: /user:
post: post:
tags: tags:
@ -421,7 +409,6 @@ paths:
description: "successful operation" description: "successful operation"
x-tags: x-tags:
- tag: "user" - tag: "user"
x-operationId: "create_user"
/user/createWithArray: /user/createWithArray:
post: post:
tags: tags:
@ -446,7 +433,6 @@ paths:
description: "successful operation" description: "successful operation"
x-tags: x-tags:
- tag: "user" - tag: "user"
x-operationId: "create_users_with_array_input"
/user/createWithList: /user/createWithList:
post: post:
tags: tags:
@ -471,7 +457,6 @@ paths:
description: "successful operation" description: "successful operation"
x-tags: x-tags:
- tag: "user" - tag: "user"
x-operationId: "create_users_with_list_input"
/user/login: /user/login:
get: get:
tags: tags:
@ -502,7 +487,6 @@ paths:
description: "Invalid username/password supplied" description: "Invalid username/password supplied"
x-tags: x-tags:
- tag: "user" - tag: "user"
x-operationId: "login_user"
/user/logout: /user/logout:
get: get:
tags: tags:
@ -519,7 +503,6 @@ paths:
description: "successful operation" description: "successful operation"
x-tags: x-tags:
- tag: "user" - tag: "user"
x-operationId: "logout_user"
/user/{username}: /user/{username}:
get: get:
tags: tags:
@ -557,7 +540,6 @@ paths:
description: "User not found" description: "User not found"
x-tags: x-tags:
- tag: "user" - tag: "user"
x-operationId: "get_user_by_name"
put: put:
tags: tags:
- "user" - "user"
@ -586,7 +568,6 @@ paths:
description: "User not found" description: "User not found"
x-tags: x-tags:
- tag: "user" - tag: "user"
x-operationId: "update_user"
delete: delete:
tags: tags:
- "user" - "user"
@ -609,7 +590,6 @@ paths:
description: "User not found" description: "User not found"
x-tags: x-tags:
- tag: "user" - tag: "user"
x-operationId: "delete_user"
securityDefinitions: securityDefinitions:
petstore_auth: petstore_auth:
type: "oauth2" type: "oauth2"