From 49ad6f47a5e813bad84b99e8814240c8c45db31b Mon Sep 17 00:00:00 2001 From: William Cheng Date: Tue, 7 Apr 2015 01:37:32 +0800 Subject: [PATCH] fix parameter name, better naming convention for python client --- .../languages/PythonClientCodegen.java | 67 +++++++++++++++++++ .../src/main/resources/python/api.mustache | 6 +- .../models/{Category.py => category.py} | 0 .../client/models/{Order.py => order.py} | 12 ++-- .../python/client/models/{Pet.py => pet.py} | 6 +- .../python/client/models/{Tag.py => tag.py} | 0 .../python/client/models/{User.py => user.py} | 18 ++--- .../python/client/{PetApi.py => pet_api.py} | 38 +++++------ .../client/{StoreApi.py => store_api.py} | 16 ++--- .../python/client/{UserApi.py => user_api.py} | 0 10 files changed, 115 insertions(+), 48 deletions(-) rename samples/client/petstore/python/client/models/{Category.py => category.py} (100%) rename samples/client/petstore/python/client/models/{Order.py => order.py} (89%) rename samples/client/petstore/python/client/models/{Pet.py => pet.py} (94%) rename samples/client/petstore/python/client/models/{Tag.py => tag.py} (100%) rename samples/client/petstore/python/client/models/{User.py => user.py} (86%) rename samples/client/petstore/python/client/{PetApi.py => pet_api.py} (94%) rename samples/client/petstore/python/client/{StoreApi.py => store_api.py} (95%) rename samples/client/petstore/python/client/{UserApi.py => user_api.py} (100%) diff --git a/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/PythonClientCodegen.java b/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/PythonClientCodegen.java index bc47b4041da..74fcf3fbbac 100755 --- a/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/PythonClientCodegen.java +++ b/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/PythonClientCodegen.java @@ -114,4 +114,71 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig // TODO: Support Python def value return "null"; } + + @Override + public String toVarName(String name) { + // replace - with _ e.g. created-at => created_at + name = name.replaceAll("-", "_"); + + // if it's all uppper case, convert to lower case + if (name.matches("^[A-Z_]*$")) + name = name.toLowerCase(); + + // camelize (lower first character) the variable name + // petId => pet_id + name = underscore(name); + + // for reserved word or word starting with number, append _ + if(reservedWords.contains(name) || name.matches("^\\d.*")) + name = escapeReservedWord(name); + + return name; + } + + @Override + public String toParamName(String name) { + // should be the same as variable name + return toVarName(name); + } + + @Override + public String toModelName(String name) { + // model name cannot use reserved keyword, e.g. return + if(reservedWords.contains(name)) + throw new RuntimeException(name + " (reserved word) cannot be used as a model name"); + + // camelize the model name + // phone_number => PhoneNumber + return camelize(name); + } + + @Override + public String toModelFilename(String name) { + // model name cannot use reserved keyword, e.g. return + if(reservedWords.contains(name)) + throw new RuntimeException(name + " (reserved word) cannot be used as a model name"); + + // underscore the model file name + // PhoneNumber.rb => phone_number.rb + return underscore(name); + } + + @Override + public String toApiFilename(String name) { + // replace - with _ e.g. created-at => created_at + name = name.replaceAll("-", "_"); + + // e.g. PhoneNumberApi.rb => phone_number_api.rb + return underscore(name) + "_api"; + } + + @Override + public String toApiName(String name) { + if(name.length() == 0) + return "DefaultApi"; + // e.g. phone_number_api => PhoneNumberApi + return camelize(name) + "Api"; + } + + } diff --git a/modules/swagger-codegen/src/main/resources/python/api.mustache b/modules/swagger-codegen/src/main/resources/python/api.mustache index a5eca0346a0..e1094a1aaed 100644 --- a/modules/swagger-codegen/src/main/resources/python/api.mustache +++ b/modules/swagger-codegen/src/main/resources/python/api.mustache @@ -68,12 +68,12 @@ class {{classname}}(object): {{#queryParams}} if ('{{paramName}}' in params): - queryParams['{{paramName}}'] = self.apiClient.toPathValue(params['{{paramName}}']) + queryParams['{{baseName}}'] = self.apiClient.toPathValue(params['{{paramName}}']) {{/queryParams}} {{#headerParams}} if ('{{paramName}}' in params): - headerParams['{{paramName}}'] = params['{{paramName}}'] + headerParams['{{baseName}}'] = params['{{paramName}}'] {{/headerParams}} {{#pathParams}} @@ -86,7 +86,7 @@ class {{classname}}(object): {{#formParams}} if ('{{paramName}}' in params): - {{#notFile}}formParams['{{paramName}}'] = params['{{paramName}}']{{/notFile}}{{#isFile}}files['{{paramName}}'] = params['{{paramName}}']{{/isFile}} + {{#notFile}}formParams['{{baseName}}'] = params['{{paramName}}']{{/notFile}}{{#isFile}}files['{{baseName}}'] = params['{{paramName}}']{{/isFile}} {{/formParams}} {{#bodyParam}} diff --git a/samples/client/petstore/python/client/models/Category.py b/samples/client/petstore/python/client/models/category.py similarity index 100% rename from samples/client/petstore/python/client/models/Category.py rename to samples/client/petstore/python/client/models/category.py diff --git a/samples/client/petstore/python/client/models/Order.py b/samples/client/petstore/python/client/models/order.py similarity index 89% rename from samples/client/petstore/python/client/models/Order.py rename to samples/client/petstore/python/client/models/order.py index 244fa872cbd..717773a7290 100644 --- a/samples/client/petstore/python/client/models/Order.py +++ b/samples/client/petstore/python/client/models/order.py @@ -31,13 +31,13 @@ class Order(object): 'id': 'long', - 'petId': 'long', + 'pet_id': 'long', 'quantity': 'int', - 'shipDate': 'DateTime', + 'ship_date': 'DateTime', 'status': 'str', @@ -51,11 +51,11 @@ class Order(object): 'id': 'id', - 'petId': 'petId', + 'pet_id': 'petId', 'quantity': 'quantity', - 'shipDate': 'shipDate', + 'ship_date': 'shipDate', 'status': 'status', @@ -68,13 +68,13 @@ class Order(object): self.id = None # long - self.petId = None # long + self.pet_id = None # long self.quantity = None # int - self.shipDate = None # DateTime + self.ship_date = None # DateTime #Order Status diff --git a/samples/client/petstore/python/client/models/Pet.py b/samples/client/petstore/python/client/models/pet.py similarity index 94% rename from samples/client/petstore/python/client/models/Pet.py rename to samples/client/petstore/python/client/models/pet.py index 4c4ee116c09..a7db312641b 100644 --- a/samples/client/petstore/python/client/models/Pet.py +++ b/samples/client/petstore/python/client/models/pet.py @@ -37,7 +37,7 @@ class Pet(object): 'name': 'str', - 'photoUrls': 'list[str]', + 'photo_urls': 'list[str]', 'tags': 'list[Tag]', @@ -55,7 +55,7 @@ class Pet(object): 'name': 'name', - 'photoUrls': 'photoUrls', + 'photo_urls': 'photoUrls', 'tags': 'tags', @@ -74,7 +74,7 @@ class Pet(object): self.name = None # str - self.photoUrls = None # list[str] + self.photo_urls = None # list[str] self.tags = None # list[Tag] diff --git a/samples/client/petstore/python/client/models/Tag.py b/samples/client/petstore/python/client/models/tag.py similarity index 100% rename from samples/client/petstore/python/client/models/Tag.py rename to samples/client/petstore/python/client/models/tag.py diff --git a/samples/client/petstore/python/client/models/User.py b/samples/client/petstore/python/client/models/user.py similarity index 86% rename from samples/client/petstore/python/client/models/User.py rename to samples/client/petstore/python/client/models/user.py index f735aed0082..f798271fbf0 100644 --- a/samples/client/petstore/python/client/models/User.py +++ b/samples/client/petstore/python/client/models/user.py @@ -34,10 +34,10 @@ class User(object): 'username': 'str', - 'firstName': 'str', + 'first_name': 'str', - 'lastName': 'str', + 'last_name': 'str', 'email': 'str', @@ -49,7 +49,7 @@ class User(object): 'phone': 'str', - 'userStatus': 'int' + 'user_status': 'int' } @@ -59,9 +59,9 @@ class User(object): 'username': 'username', - 'firstName': 'firstName', + 'first_name': 'firstName', - 'lastName': 'lastName', + 'last_name': 'lastName', 'email': 'email', @@ -69,7 +69,7 @@ class User(object): 'phone': 'phone', - 'userStatus': 'userStatus' + 'user_status': 'userStatus' } @@ -81,10 +81,10 @@ class User(object): self.username = None # str - self.firstName = None # str + self.first_name = None # str - self.lastName = None # str + self.last_name = None # str self.email = None # str @@ -97,5 +97,5 @@ class User(object): #User Status - self.userStatus = None # int + self.user_status = None # int diff --git a/samples/client/petstore/python/client/PetApi.py b/samples/client/petstore/python/client/pet_api.py similarity index 94% rename from samples/client/petstore/python/client/PetApi.py rename to samples/client/petstore/python/client/pet_api.py index 729431a8110..f90f5bae7a6 100644 --- a/samples/client/petstore/python/client/PetApi.py +++ b/samples/client/petstore/python/client/pet_api.py @@ -272,14 +272,14 @@ class PetApi(object): Args: - petId, long: ID of pet that needs to be fetched (required) + pet_id, long: ID of pet that needs to be fetched (required) Returns: Pet """ - allParams = ['petId'] + allParams = ['pet_id'] params = locals() for (key, val) in params['kwargs'].iteritems(): @@ -306,8 +306,8 @@ class PetApi(object): - if ('petId' in params): - replacement = str(self.apiClient.toPathValue(params['petId'])) + if ('pet_id' in params): + replacement = str(self.apiClient.toPathValue(params['pet_id'])) replacement = urllib.quote(replacement) resourcePath = resourcePath.replace('{' + 'petId' + '}', replacement) @@ -337,7 +337,7 @@ class PetApi(object): Args: - petId, str: ID of pet that needs to be updated (required) + pet_id, str: ID of pet that needs to be updated (required) name, str: Updated name of the pet (required) @@ -350,7 +350,7 @@ class PetApi(object): Returns: """ - allParams = ['petId', 'name', 'status'] + allParams = ['pet_id', 'name', 'status'] params = locals() for (key, val) in params['kwargs'].iteritems(): @@ -377,8 +377,8 @@ class PetApi(object): - if ('petId' in params): - replacement = str(self.apiClient.toPathValue(params['petId'])) + if ('pet_id' in params): + replacement = str(self.apiClient.toPathValue(params['pet_id'])) replacement = urllib.quote(replacement) resourcePath = resourcePath.replace('{' + 'petId' + '}', replacement) @@ -411,14 +411,14 @@ class PetApi(object): api_key, str: (required) - petId, long: Pet id to delete (required) + pet_id, long: Pet id to delete (required) Returns: """ - allParams = ['api_key', 'petId'] + allParams = ['api_key', 'pet_id'] params = locals() for (key, val) in params['kwargs'].iteritems(): @@ -448,8 +448,8 @@ class PetApi(object): - if ('petId' in params): - replacement = str(self.apiClient.toPathValue(params['petId'])) + if ('pet_id' in params): + replacement = str(self.apiClient.toPathValue(params['pet_id'])) replacement = urllib.quote(replacement) resourcePath = resourcePath.replace('{' + 'petId' + '}', replacement) @@ -473,10 +473,10 @@ class PetApi(object): Args: - petId, long: ID of pet to update (required) + pet_id, long: ID of pet to update (required) - additionalMetadata, str: Additional data to pass to server (required) + additional_metadata, str: Additional data to pass to server (required) file, file: file to upload (required) @@ -486,7 +486,7 @@ class PetApi(object): Returns: """ - allParams = ['petId', 'additionalMetadata', 'file'] + allParams = ['pet_id', 'additional_metadata', 'file'] params = locals() for (key, val) in params['kwargs'].iteritems(): @@ -513,16 +513,16 @@ class PetApi(object): - if ('petId' in params): - replacement = str(self.apiClient.toPathValue(params['petId'])) + if ('pet_id' in params): + replacement = str(self.apiClient.toPathValue(params['pet_id'])) replacement = urllib.quote(replacement) resourcePath = resourcePath.replace('{' + 'petId' + '}', replacement) - if ('additionalMetadata' in params): - formParams['additionalMetadata'] = params['additionalMetadata'] + if ('additional_metadata' in params): + formParams['additionalMetadata'] = params['additional_metadata'] if ('file' in params): files['file'] = params['file'] diff --git a/samples/client/petstore/python/client/StoreApi.py b/samples/client/petstore/python/client/store_api.py similarity index 95% rename from samples/client/petstore/python/client/StoreApi.py rename to samples/client/petstore/python/client/store_api.py index 5f7a1b37d7a..bf0b4abfbd8 100644 --- a/samples/client/petstore/python/client/StoreApi.py +++ b/samples/client/petstore/python/client/store_api.py @@ -154,14 +154,14 @@ class StoreApi(object): Args: - orderId, str: ID of pet that needs to be fetched (required) + order_id, str: ID of pet that needs to be fetched (required) Returns: Order """ - allParams = ['orderId'] + allParams = ['order_id'] params = locals() for (key, val) in params['kwargs'].iteritems(): @@ -188,8 +188,8 @@ class StoreApi(object): - if ('orderId' in params): - replacement = str(self.apiClient.toPathValue(params['orderId'])) + if ('order_id' in params): + replacement = str(self.apiClient.toPathValue(params['order_id'])) replacement = urllib.quote(replacement) resourcePath = resourcePath.replace('{' + 'orderId' + '}', replacement) @@ -219,14 +219,14 @@ class StoreApi(object): Args: - orderId, str: ID of the order that needs to be deleted (required) + order_id, str: ID of the order that needs to be deleted (required) Returns: """ - allParams = ['orderId'] + allParams = ['order_id'] params = locals() for (key, val) in params['kwargs'].iteritems(): @@ -253,8 +253,8 @@ class StoreApi(object): - if ('orderId' in params): - replacement = str(self.apiClient.toPathValue(params['orderId'])) + if ('order_id' in params): + replacement = str(self.apiClient.toPathValue(params['order_id'])) replacement = urllib.quote(replacement) resourcePath = resourcePath.replace('{' + 'orderId' + '}', replacement) diff --git a/samples/client/petstore/python/client/UserApi.py b/samples/client/petstore/python/client/user_api.py similarity index 100% rename from samples/client/petstore/python/client/UserApi.py rename to samples/client/petstore/python/client/user_api.py