From 6e8497fdb74011496204855cf43601d8ab65bf38 Mon Sep 17 00:00:00 2001 From: Scott Williams Date: Sun, 1 May 2016 16:09:02 +0100 Subject: [PATCH 1/9] Add validation to api class --- .../src/main/resources/python/api.mustache | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/modules/swagger-codegen/src/main/resources/python/api.mustache b/modules/swagger-codegen/src/main/resources/python/api.mustache index 0967a2ec6b3..1a50f753409 100644 --- a/modules/swagger-codegen/src/main/resources/python/api.mustache +++ b/modules/swagger-codegen/src/main/resources/python/api.mustache @@ -21,6 +21,7 @@ from __future__ import absolute_import import sys import os +import re # python 2 and python 3 compatibility library from six import iteritems @@ -94,6 +95,23 @@ class {{classname}}(object): if ('{{paramName}}' not in params) or (params['{{paramName}}'] is None): raise ValueError("Missing the required parameter `{{paramName}}` when calling `{{operationId}}`") {{/required}} +{{/allParams}} + +{{#allParams}} +{{#hasValidation}} + {{#maximum}} + if {{#required}}{{paramName}}{{/required}}{{^required}}'{{paramName}}' in params and params['{{paramName}}']{{/required}} > {{maximum}}: + raise ValueError("Invalid value for parameter `{{paramName}}` when calling `{{operationId}}`, must be a value less than `{{maximum}}`") + {{/maximum}} + {{#minimum}} + if {{#required}}{{paramName}}{{/required}}{{^required}}'{{paramName}}' in params and params['{{paramName}}']{{/required}} < {{minimum}}: + raise ValueError("Invalid value for parameter `{{paramName}}` when calling `{{operationId}}`, must be a value greater than `{{minimum}}`") + {{/minimum}} + {{#pattern}} + #if not re.match('{{pattern}}', {{#required}}{{paramName}}{{/required}}{{^required}}'{{paramName}}' in params and params['{{paramName}}']{{/required}}): + # raise ValueError("Invalid value for parameter `{{paramName}}` when calling `{{operationId}}`, must conform to the pattern `{{pattern}}`") + {{/pattern}} +{{/hasValidation}} {{/allParams}} resource_path = '{{path}}'.replace('{format}', 'json') From cea6bce196893d804633d0904afbb0861314b10a Mon Sep 17 00:00:00 2001 From: Scott Williams Date: Sun, 1 May 2016 16:15:29 +0100 Subject: [PATCH 2/9] Follow convention in place when accessing attributes --- .../swagger-codegen/src/main/resources/python/api.mustache | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/python/api.mustache b/modules/swagger-codegen/src/main/resources/python/api.mustache index 1a50f753409..e74365399e2 100644 --- a/modules/swagger-codegen/src/main/resources/python/api.mustache +++ b/modules/swagger-codegen/src/main/resources/python/api.mustache @@ -100,15 +100,15 @@ class {{classname}}(object): {{#allParams}} {{#hasValidation}} {{#maximum}} - if {{#required}}{{paramName}}{{/required}}{{^required}}'{{paramName}}' in params and params['{{paramName}}']{{/required}} > {{maximum}}: + if '{{paramName}}' in params and params['{{paramName}}'] > {{maximum}}: raise ValueError("Invalid value for parameter `{{paramName}}` when calling `{{operationId}}`, must be a value less than `{{maximum}}`") {{/maximum}} {{#minimum}} - if {{#required}}{{paramName}}{{/required}}{{^required}}'{{paramName}}' in params and params['{{paramName}}']{{/required}} < {{minimum}}: + if '{{paramName}}' in params and params['{{paramName}}'] < {{minimum}}: raise ValueError("Invalid value for parameter `{{paramName}}` when calling `{{operationId}}`, must be a value greater than `{{minimum}}`") {{/minimum}} {{#pattern}} - #if not re.match('{{pattern}}', {{#required}}{{paramName}}{{/required}}{{^required}}'{{paramName}}' in params and params['{{paramName}}']{{/required}}): + #if not re.match('{{pattern}}', '{{paramName}}' in params and params['{{paramName}}']): # raise ValueError("Invalid value for parameter `{{paramName}}` when calling `{{operationId}}`, must conform to the pattern `{{pattern}}`") {{/pattern}} {{/hasValidation}} From 97e69aabc3ea62e913228b76a514fb61d10183cc Mon Sep 17 00:00:00 2001 From: Scott Williams Date: Sun, 1 May 2016 16:38:56 +0100 Subject: [PATCH 3/9] Add support for max/min string length --- .../src/main/resources/python/api.mustache | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/modules/swagger-codegen/src/main/resources/python/api.mustache b/modules/swagger-codegen/src/main/resources/python/api.mustache index e74365399e2..5a38ee255ee 100644 --- a/modules/swagger-codegen/src/main/resources/python/api.mustache +++ b/modules/swagger-codegen/src/main/resources/python/api.mustache @@ -99,6 +99,14 @@ class {{classname}}(object): {{#allParams}} {{#hasValidation}} + {{#maxLength}} + if '{{paramName}}' in params and len(params['{{paramName}}']) > {{maxLength}}: + raise ValueError("Invalid value for parameter `{{paramName}}` when calling `{{operationId}}`, length must be less than `{{maxLength}}`") + {{/maxLength}} + {{#minLength}} + if '{{paramName}}' in params and len(params['{{paramName}}']) < {{minLength}}: + raise ValueError("Invalid value for parameter `{{paramName}}` when calling `{{operationId}}`, length must be greater than `{{minLength}}`") + {{/minLength}} {{#maximum}} if '{{paramName}}' in params and params['{{paramName}}'] > {{maximum}}: raise ValueError("Invalid value for parameter `{{paramName}}` when calling `{{operationId}}`, must be a value less than `{{maximum}}`") From 3dbdc839811f27e85d35277d33cbc40a915a7ec4 Mon Sep 17 00:00:00 2001 From: Scott Williams Date: Mon, 2 May 2016 16:25:46 +0100 Subject: [PATCH 4/9] Add validation to model --- .../src/main/resources/python/model.mustache | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/modules/swagger-codegen/src/main/resources/python/model.mustache b/modules/swagger-codegen/src/main/resources/python/model.mustache index bd33a997c3e..340c73687f5 100644 --- a/modules/swagger-codegen/src/main/resources/python/model.mustache +++ b/modules/swagger-codegen/src/main/resources/python/model.mustache @@ -79,7 +79,32 @@ class {{classname}}(object): "Invalid value for `{{name}}`, must be one of {0}" .format(allowed_values) ) - {{/isEnum}}self._{{name}} = {{name}} + {{/isEnum}} + {{^isEnum}} + + {{#hasValidation}} + {{#maxLength}} + if len({{name}}) > {{maxLength}}: + raise ValueError("Invalid value for `{{name}}`, length must be less than `{{maxLength}}`") + {{/maxLength}} + {{#minLength}} + if len({{name}}) < {{minLength}}: + raise ValueError("Invalid value for `{{name}}`, length must be greater than `{{minLength}}`") + {{/minLength}} + {{#maximum}} + if {{name}} > {{maximum}}: + raise ValueError("Invalid value for `{{name}}`, must be a value less than `{{maximum}}`") + {{/maximum}} + {{#minimum}} + if {{name}} < {{minimum}}: + raise ValueError("Invalid value for `{{name}}`, must be a value greater than `{{minimum}}`") + {{/minimum}} + {{#pattern}} + #Check pattern + {{/pattern}} + {{/hasValidation}} + {{/isEnum}} + self._{{name}} = {{name}} {{/vars}} def to_dict(self): From 4a440f4ee4cdf19f7cf427625359bde363affb2e Mon Sep 17 00:00:00 2001 From: Scott Williams Date: Mon, 2 May 2016 16:37:32 +0100 Subject: [PATCH 5/9] Fix excpetion message to include --- .../src/main/resources/python/api.mustache | 8 ++++---- .../src/main/resources/python/model.mustache | 14 +++++++++----- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/python/api.mustache b/modules/swagger-codegen/src/main/resources/python/api.mustache index 5a38ee255ee..7902754e5fb 100644 --- a/modules/swagger-codegen/src/main/resources/python/api.mustache +++ b/modules/swagger-codegen/src/main/resources/python/api.mustache @@ -101,19 +101,19 @@ class {{classname}}(object): {{#hasValidation}} {{#maxLength}} if '{{paramName}}' in params and len(params['{{paramName}}']) > {{maxLength}}: - raise ValueError("Invalid value for parameter `{{paramName}}` when calling `{{operationId}}`, length must be less than `{{maxLength}}`") + raise ValueError("Invalid value for parameter `{{paramName}}` when calling `{{operationId}}`, length must be less than or equal to `{{maxLength}}`") {{/maxLength}} {{#minLength}} if '{{paramName}}' in params and len(params['{{paramName}}']) < {{minLength}}: - raise ValueError("Invalid value for parameter `{{paramName}}` when calling `{{operationId}}`, length must be greater than `{{minLength}}`") + raise ValueError("Invalid value for parameter `{{paramName}}` when calling `{{operationId}}`, length must be greater than or equal to `{{minLength}}`") {{/minLength}} {{#maximum}} if '{{paramName}}' in params and params['{{paramName}}'] > {{maximum}}: - raise ValueError("Invalid value for parameter `{{paramName}}` when calling `{{operationId}}`, must be a value less than `{{maximum}}`") + raise ValueError("Invalid value for parameter `{{paramName}}` when calling `{{operationId}}`, must be a value less than or equal to `{{maximum}}`") {{/maximum}} {{#minimum}} if '{{paramName}}' in params and params['{{paramName}}'] < {{minimum}}: - raise ValueError("Invalid value for parameter `{{paramName}}` when calling `{{operationId}}`, must be a value greater than `{{minimum}}`") + raise ValueError("Invalid value for parameter `{{paramName}}` when calling `{{operationId}}`, must be a value greater than or equal to `{{minimum}}`") {{/minimum}} {{#pattern}} #if not re.match('{{pattern}}', '{{paramName}}' in params and params['{{paramName}}']): diff --git a/modules/swagger-codegen/src/main/resources/python/model.mustache b/modules/swagger-codegen/src/main/resources/python/model.mustache index 340c73687f5..78fe2ff2acd 100644 --- a/modules/swagger-codegen/src/main/resources/python/model.mustache +++ b/modules/swagger-codegen/src/main/resources/python/model.mustache @@ -21,7 +21,8 @@ Copyright 2016 SmartBear Software {{#models}} {{#model}} from pprint import pformat -from six import iteritems +from six import +import re class {{classname}}(object): @@ -83,24 +84,27 @@ class {{classname}}(object): {{^isEnum}} {{#hasValidation}} + if not {{name}}: + raise ValueError("Invalid value for `{{name}}`, must not be `None`") {{#maxLength}} if len({{name}}) > {{maxLength}}: raise ValueError("Invalid value for `{{name}}`, length must be less than `{{maxLength}}`") {{/maxLength}} {{#minLength}} if len({{name}}) < {{minLength}}: - raise ValueError("Invalid value for `{{name}}`, length must be greater than `{{minLength}}`") + raise ValueError("Invalid value for `{{name}}`, length must be greater than or equal to `{{minLength}}`") {{/minLength}} {{#maximum}} if {{name}} > {{maximum}}: - raise ValueError("Invalid value for `{{name}}`, must be a value less than `{{maximum}}`") + raise ValueError("Invalid value for `{{name}}`, must be a value less than or equal to `{{maximum}}`") {{/maximum}} {{#minimum}} if {{name}} < {{minimum}}: - raise ValueError("Invalid value for `{{name}}`, must be a value greater than `{{minimum}}`") + raise ValueError("Invalid value for `{{name}}`, must be a value greater than or equal to `{{minimum}}`") {{/minimum}} {{#pattern}} - #Check pattern + #if not re.match('/[a-z]/i', {{name}}): + # raise ValueError("Invalid value for `{{name}}`, must be a follow pattern or equal to `{{pattern}}`") {{/pattern}} {{/hasValidation}} {{/isEnum}} From 1fef0ef691e5c8d3075711d0c333670e23590a64 Mon Sep 17 00:00:00 2001 From: Scott Williams Date: Mon, 2 May 2016 16:39:25 +0100 Subject: [PATCH 6/9] Fix import statement --- .../swagger-codegen/src/main/resources/python/model.mustache | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/swagger-codegen/src/main/resources/python/model.mustache b/modules/swagger-codegen/src/main/resources/python/model.mustache index 78fe2ff2acd..3fce2b47697 100644 --- a/modules/swagger-codegen/src/main/resources/python/model.mustache +++ b/modules/swagger-codegen/src/main/resources/python/model.mustache @@ -21,7 +21,7 @@ Copyright 2016 SmartBear Software {{#models}} {{#model}} from pprint import pformat -from six import +from six import iteritems import re From fed22a1f72e6559536c974181cf281ac9ed1e95a Mon Sep 17 00:00:00 2001 From: Scott Williams Date: Fri, 6 May 2016 22:52:44 +0100 Subject: [PATCH 7/9] Add regex support --- .../src/main/resources/python/api.mustache | 4 +- .../src/main/resources/python/model.mustache | 35 +++++------ samples/client/petstore/python/README.md | 6 +- samples/client/petstore/python/git_push.sh | 4 +- .../python/swagger_client/apis/fake_api.py | 26 ++++++++ .../python/swagger_client/apis/pet_api.py | 9 +++ .../python/swagger_client/apis/store_api.py | 11 ++++ .../python/swagger_client/apis/user_api.py | 9 +++ .../python/swagger_client/models/animal.py | 2 + .../swagger_client/models/api_response.py | 4 ++ .../python/swagger_client/models/cat.py | 3 + .../python/swagger_client/models/category.py | 3 + .../python/swagger_client/models/dog.py | 3 + .../swagger_client/models/format_test.py | 61 +++++++++++++++++++ .../models/model_200_response.py | 2 + .../swagger_client/models/model_return.py | 2 + .../python/swagger_client/models/name.py | 4 ++ .../python/swagger_client/models/order.py | 7 +++ .../python/swagger_client/models/pet.py | 7 +++ .../models/special_model_name.py | 2 + .../python/swagger_client/models/tag.py | 3 + .../python/swagger_client/models/user.py | 9 +++ 22 files changed, 192 insertions(+), 24 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/python/api.mustache b/modules/swagger-codegen/src/main/resources/python/api.mustache index 7902754e5fb..399c7f58451 100644 --- a/modules/swagger-codegen/src/main/resources/python/api.mustache +++ b/modules/swagger-codegen/src/main/resources/python/api.mustache @@ -116,8 +116,8 @@ class {{classname}}(object): raise ValueError("Invalid value for parameter `{{paramName}}` when calling `{{operationId}}`, must be a value greater than or equal to `{{minimum}}`") {{/minimum}} {{#pattern}} - #if not re.match('{{pattern}}', '{{paramName}}' in params and params['{{paramName}}']): - # raise ValueError("Invalid value for parameter `{{paramName}}` when calling `{{operationId}}`, must conform to the pattern `{{pattern}}`") + if '{{paramName}}' in params and not re.match('{{pattern}}', params['{{paramName}}']): + raise ValueError("Invalid value for parameter `{{paramName}}` when calling `{{operationId}}`, must conform to the pattern `{{pattern}}`") {{/pattern}} {{/hasValidation}} {{/allParams}} diff --git a/modules/swagger-codegen/src/main/resources/python/model.mustache b/modules/swagger-codegen/src/main/resources/python/model.mustache index 3fce2b47697..0cb98a57ef0 100644 --- a/modules/swagger-codegen/src/main/resources/python/model.mustache +++ b/modules/swagger-codegen/src/main/resources/python/model.mustache @@ -80,34 +80,35 @@ class {{classname}}(object): "Invalid value for `{{name}}`, must be one of {0}" .format(allowed_values) ) - {{/isEnum}} - {{^isEnum}} +{{/isEnum}} +{{^isEnum}} +{{#hasValidation}} - {{#hasValidation}} if not {{name}}: raise ValueError("Invalid value for `{{name}}`, must not be `None`") - {{#maxLength}} +{{#maxLength}} if len({{name}}) > {{maxLength}}: raise ValueError("Invalid value for `{{name}}`, length must be less than `{{maxLength}}`") - {{/maxLength}} - {{#minLength}} +{{/maxLength}} +{{#minLength}} if len({{name}}) < {{minLength}}: raise ValueError("Invalid value for `{{name}}`, length must be greater than or equal to `{{minLength}}`") - {{/minLength}} - {{#maximum}} +{{/minLength}} +{{#maximum}} if {{name}} > {{maximum}}: raise ValueError("Invalid value for `{{name}}`, must be a value less than or equal to `{{maximum}}`") - {{/maximum}} - {{#minimum}} +{{/maximum}} +{{#minimum}} if {{name}} < {{minimum}}: raise ValueError("Invalid value for `{{name}}`, must be a value greater than or equal to `{{minimum}}`") - {{/minimum}} - {{#pattern}} - #if not re.match('/[a-z]/i', {{name}}): - # raise ValueError("Invalid value for `{{name}}`, must be a follow pattern or equal to `{{pattern}}`") - {{/pattern}} - {{/hasValidation}} - {{/isEnum}} +{{/minimum}} +{{#pattern}} + if not re.match('{{pattern}}', {{name}}): + raise ValueError("Invalid value for `{{name}}`, must be a follow pattern or equal to `{{pattern}}`") +{{/pattern}} +{{/hasValidation}} +{{/isEnum}} + self._{{name}} = {{name}} {{/vars}} diff --git a/samples/client/petstore/python/README.md b/samples/client/petstore/python/README.md index 6640785fae9..990d5e53651 100644 --- a/samples/client/petstore/python/README.md +++ b/samples/client/petstore/python/README.md @@ -5,7 +5,7 @@ This Python package is automatically generated by the [Swagger Codegen](https:// - API version: 1.0.0 - Package version: 1.0.0 -- Build date: 2016-04-27T22:50:21.115+01:00 +- Build date: 2016-05-06T22:43:12.044+01:00 - Build package: class io.swagger.codegen.languages.PythonClientCodegen ## Requirements. @@ -18,9 +18,9 @@ Python 2.7 and 3.4+ If the python package is hosted on Github, you can install directly from Github ```sh -pip install git+https://github.com/YOUR_GIT_USR_ID/YOUR_GIT_REPO_ID.git +pip install git+https://github.com/GIT_USER_ID/GIT_REPO_ID.git ``` -(you may need to run `pip` with root permission: `sudo pip install git+https://github.com/YOUR_GIT_USR_ID/YOUR_GIT_REPO_ID.git`) +(you may need to run `pip` with root permission: `sudo pip install git+https://github.com/GIT_USER_ID/GIT_REPO_ID.git`) Then import the package: ```python diff --git a/samples/client/petstore/python/git_push.sh b/samples/client/petstore/python/git_push.sh index 1a36388db02..ed374619b13 100644 --- a/samples/client/petstore/python/git_push.sh +++ b/samples/client/petstore/python/git_push.sh @@ -8,12 +8,12 @@ git_repo_id=$2 release_note=$3 if [ "$git_user_id" = "" ]; then - git_user_id="YOUR_GIT_USR_ID" + git_user_id="GIT_USER_ID" echo "[INFO] No command line input provided. Set \$git_user_id to $git_user_id" fi if [ "$git_repo_id" = "" ]; then - git_repo_id="YOUR_GIT_REPO_ID" + git_repo_id="GIT_REPO_ID" echo "[INFO] No command line input provided. Set \$git_repo_id to $git_repo_id" fi diff --git a/samples/client/petstore/python/swagger_client/apis/fake_api.py b/samples/client/petstore/python/swagger_client/apis/fake_api.py index 2b183794ef1..ba79874f6c7 100644 --- a/samples/client/petstore/python/swagger_client/apis/fake_api.py +++ b/samples/client/petstore/python/swagger_client/apis/fake_api.py @@ -21,6 +21,7 @@ from __future__ import absolute_import import sys import os +import re # python 2 and python 3 compatibility library from six import iteritems @@ -103,6 +104,31 @@ class FakeApi(object): if ('byte' not in params) or (params['byte'] is None): raise ValueError("Missing the required parameter `byte` when calling `test_endpoint_parameters`") + if 'number' in params and params['number'] > 543.2: + raise ValueError("Invalid value for parameter `number` when calling `test_endpoint_parameters`, must be a value less than or equal to `543.2`") + if 'number' in params and params['number'] < 32.1: + raise ValueError("Invalid value for parameter `number` when calling `test_endpoint_parameters`, must be a value greater than or equal to `32.1`") + if 'double' in params and params['double'] > 123.4: + raise ValueError("Invalid value for parameter `double` when calling `test_endpoint_parameters`, must be a value less than or equal to `123.4`") + if 'double' in params and params['double'] < 67.8: + raise ValueError("Invalid value for parameter `double` when calling `test_endpoint_parameters`, must be a value greater than or equal to `67.8`") + if 'string' in params and not re.match('[a-z]+', params['string']): + raise ValueError("Invalid value for parameter `string` when calling `test_endpoint_parameters`, must conform to the pattern `[a-z]+`") + if 'integer' in params and params['integer'] > 100.0: + raise ValueError("Invalid value for parameter `integer` when calling `test_endpoint_parameters`, must be a value less than or equal to `100.0`") + if 'integer' in params and params['integer'] < 10.0: + raise ValueError("Invalid value for parameter `integer` when calling `test_endpoint_parameters`, must be a value greater than or equal to `10.0`") + if 'int32' in params and params['int32'] > 200.0: + raise ValueError("Invalid value for parameter `int32` when calling `test_endpoint_parameters`, must be a value less than or equal to `200.0`") + if 'int32' in params and params['int32'] < 20.0: + raise ValueError("Invalid value for parameter `int32` when calling `test_endpoint_parameters`, must be a value greater than or equal to `20.0`") + if 'float' in params and params['float'] > 987.6: + raise ValueError("Invalid value for parameter `float` when calling `test_endpoint_parameters`, must be a value less than or equal to `987.6`") + if 'password' in params and len(params['password']) > 64: + raise ValueError("Invalid value for parameter `password` when calling `test_endpoint_parameters`, length must be less than or equal to `64`") + if 'password' in params and len(params['password']) < 10: + raise ValueError("Invalid value for parameter `password` when calling `test_endpoint_parameters`, length must be greater than or equal to `10`") + resource_path = '/fake'.replace('{format}', 'json') path_params = {} diff --git a/samples/client/petstore/python/swagger_client/apis/pet_api.py b/samples/client/petstore/python/swagger_client/apis/pet_api.py index cab854e3019..3b6d9ba82fe 100644 --- a/samples/client/petstore/python/swagger_client/apis/pet_api.py +++ b/samples/client/petstore/python/swagger_client/apis/pet_api.py @@ -21,6 +21,7 @@ from __future__ import absolute_import import sys import os +import re # python 2 and python 3 compatibility library from six import iteritems @@ -83,6 +84,7 @@ class PetApi(object): if ('body' not in params) or (params['body'] is None): raise ValueError("Missing the required parameter `body` when calling `add_pet`") + resource_path = '/pet'.replace('{format}', 'json') path_params = {} @@ -161,6 +163,7 @@ class PetApi(object): if ('pet_id' not in params) or (params['pet_id'] is None): raise ValueError("Missing the required parameter `pet_id` when calling `delete_pet`") + resource_path = '/pet/{petId}'.replace('{format}', 'json') path_params = {} if 'pet_id' in params: @@ -240,6 +243,7 @@ class PetApi(object): if ('status' not in params) or (params['status'] is None): raise ValueError("Missing the required parameter `status` when calling `find_pets_by_status`") + resource_path = '/pet/findByStatus'.replace('{format}', 'json') path_params = {} @@ -317,6 +321,7 @@ class PetApi(object): if ('tags' not in params) or (params['tags'] is None): raise ValueError("Missing the required parameter `tags` when calling `find_pets_by_tags`") + resource_path = '/pet/findByTags'.replace('{format}', 'json') path_params = {} @@ -394,6 +399,7 @@ class PetApi(object): if ('pet_id' not in params) or (params['pet_id'] is None): raise ValueError("Missing the required parameter `pet_id` when calling `get_pet_by_id`") + resource_path = '/pet/{petId}'.replace('{format}', 'json') path_params = {} if 'pet_id' in params: @@ -471,6 +477,7 @@ class PetApi(object): if ('body' not in params) or (params['body'] is None): raise ValueError("Missing the required parameter `body` when calling `update_pet`") + resource_path = '/pet'.replace('{format}', 'json') path_params = {} @@ -550,6 +557,7 @@ class PetApi(object): if ('pet_id' not in params) or (params['pet_id'] is None): raise ValueError("Missing the required parameter `pet_id` when calling `update_pet_with_form`") + resource_path = '/pet/{petId}'.replace('{format}', 'json') path_params = {} if 'pet_id' in params: @@ -633,6 +641,7 @@ class PetApi(object): if ('pet_id' not in params) or (params['pet_id'] is None): raise ValueError("Missing the required parameter `pet_id` when calling `upload_file`") + resource_path = '/pet/{petId}/uploadImage'.replace('{format}', 'json') path_params = {} if 'pet_id' in params: diff --git a/samples/client/petstore/python/swagger_client/apis/store_api.py b/samples/client/petstore/python/swagger_client/apis/store_api.py index 9391a6a6096..352ab7eae63 100644 --- a/samples/client/petstore/python/swagger_client/apis/store_api.py +++ b/samples/client/petstore/python/swagger_client/apis/store_api.py @@ -21,6 +21,7 @@ from __future__ import absolute_import import sys import os +import re # python 2 and python 3 compatibility library from six import iteritems @@ -83,6 +84,9 @@ class StoreApi(object): if ('order_id' not in params) or (params['order_id'] is None): raise ValueError("Missing the required parameter `order_id` when calling `delete_order`") + if 'order_id' in params and params['order_id'] < 1.0: + raise ValueError("Invalid value for parameter `order_id` when calling `delete_order`, must be a value greater than or equal to `1.0`") + resource_path = '/store/order/{orderId}'.replace('{format}', 'json') path_params = {} if 'order_id' in params: @@ -156,6 +160,7 @@ class StoreApi(object): del params['kwargs'] + resource_path = '/store/inventory'.replace('{format}', 'json') path_params = {} @@ -231,6 +236,11 @@ class StoreApi(object): if ('order_id' not in params) or (params['order_id'] is None): raise ValueError("Missing the required parameter `order_id` when calling `get_order_by_id`") + if 'order_id' in params and params['order_id'] > 5.0: + raise ValueError("Invalid value for parameter `order_id` when calling `get_order_by_id`, must be a value less than or equal to `5.0`") + if 'order_id' in params and params['order_id'] < 1.0: + raise ValueError("Invalid value for parameter `order_id` when calling `get_order_by_id`, must be a value greater than or equal to `1.0`") + resource_path = '/store/order/{orderId}'.replace('{format}', 'json') path_params = {} if 'order_id' in params: @@ -308,6 +318,7 @@ class StoreApi(object): if ('body' not in params) or (params['body'] is None): raise ValueError("Missing the required parameter `body` when calling `place_order`") + resource_path = '/store/order'.replace('{format}', 'json') path_params = {} diff --git a/samples/client/petstore/python/swagger_client/apis/user_api.py b/samples/client/petstore/python/swagger_client/apis/user_api.py index 733a950f393..d5df6f0f541 100644 --- a/samples/client/petstore/python/swagger_client/apis/user_api.py +++ b/samples/client/petstore/python/swagger_client/apis/user_api.py @@ -21,6 +21,7 @@ from __future__ import absolute_import import sys import os +import re # python 2 and python 3 compatibility library from six import iteritems @@ -83,6 +84,7 @@ class UserApi(object): if ('body' not in params) or (params['body'] is None): raise ValueError("Missing the required parameter `body` when calling `create_user`") + resource_path = '/user'.replace('{format}', 'json') path_params = {} @@ -160,6 +162,7 @@ class UserApi(object): if ('body' not in params) or (params['body'] is None): raise ValueError("Missing the required parameter `body` when calling `create_users_with_array_input`") + resource_path = '/user/createWithArray'.replace('{format}', 'json') path_params = {} @@ -237,6 +240,7 @@ class UserApi(object): if ('body' not in params) or (params['body'] is None): raise ValueError("Missing the required parameter `body` when calling `create_users_with_list_input`") + resource_path = '/user/createWithList'.replace('{format}', 'json') path_params = {} @@ -314,6 +318,7 @@ class UserApi(object): if ('username' not in params) or (params['username'] is None): raise ValueError("Missing the required parameter `username` when calling `delete_user`") + resource_path = '/user/{username}'.replace('{format}', 'json') path_params = {} if 'username' in params: @@ -391,6 +396,7 @@ class UserApi(object): if ('username' not in params) or (params['username'] is None): raise ValueError("Missing the required parameter `username` when calling `get_user_by_name`") + resource_path = '/user/{username}'.replace('{format}', 'json') path_params = {} if 'username' in params: @@ -472,6 +478,7 @@ class UserApi(object): if ('password' not in params) or (params['password'] is None): raise ValueError("Missing the required parameter `password` when calling `login_user`") + resource_path = '/user/login'.replace('{format}', 'json') path_params = {} @@ -547,6 +554,7 @@ class UserApi(object): del params['kwargs'] + resource_path = '/user/logout'.replace('{format}', 'json') path_params = {} @@ -626,6 +634,7 @@ class UserApi(object): if ('body' not in params) or (params['body'] is None): raise ValueError("Missing the required parameter `body` when calling `update_user`") + resource_path = '/user/{username}'.replace('{format}', 'json') path_params = {} if 'username' in params: diff --git a/samples/client/petstore/python/swagger_client/models/animal.py b/samples/client/petstore/python/swagger_client/models/animal.py index 762e3df5b37..0ae8f2b4bde 100644 --- a/samples/client/petstore/python/swagger_client/models/animal.py +++ b/samples/client/petstore/python/swagger_client/models/animal.py @@ -20,6 +20,7 @@ Copyright 2016 SmartBear Software from pprint import pformat from six import iteritems +import re class Animal(object): @@ -66,6 +67,7 @@ class Animal(object): :param class_name: The class_name of this Animal. :type: str """ + self._class_name = class_name def to_dict(self): diff --git a/samples/client/petstore/python/swagger_client/models/api_response.py b/samples/client/petstore/python/swagger_client/models/api_response.py index c49bde7fbc6..918b4a98223 100644 --- a/samples/client/petstore/python/swagger_client/models/api_response.py +++ b/samples/client/petstore/python/swagger_client/models/api_response.py @@ -20,6 +20,7 @@ Copyright 2016 SmartBear Software from pprint import pformat from six import iteritems +import re class ApiResponse(object): @@ -72,6 +73,7 @@ class ApiResponse(object): :param code: The code of this ApiResponse. :type: int """ + self._code = code @property @@ -94,6 +96,7 @@ class ApiResponse(object): :param type: The type of this ApiResponse. :type: str """ + self._type = type @property @@ -116,6 +119,7 @@ class ApiResponse(object): :param message: The message of this ApiResponse. :type: str """ + self._message = message def to_dict(self): diff --git a/samples/client/petstore/python/swagger_client/models/cat.py b/samples/client/petstore/python/swagger_client/models/cat.py index 4744fc4821c..fd2e5e4fce4 100644 --- a/samples/client/petstore/python/swagger_client/models/cat.py +++ b/samples/client/petstore/python/swagger_client/models/cat.py @@ -20,6 +20,7 @@ Copyright 2016 SmartBear Software from pprint import pformat from six import iteritems +import re class Cat(object): @@ -69,6 +70,7 @@ class Cat(object): :param class_name: The class_name of this Cat. :type: str """ + self._class_name = class_name @property @@ -91,6 +93,7 @@ class Cat(object): :param declawed: The declawed of this Cat. :type: bool """ + self._declawed = declawed def to_dict(self): diff --git a/samples/client/petstore/python/swagger_client/models/category.py b/samples/client/petstore/python/swagger_client/models/category.py index b8d540c51bd..e4f4a993cab 100644 --- a/samples/client/petstore/python/swagger_client/models/category.py +++ b/samples/client/petstore/python/swagger_client/models/category.py @@ -20,6 +20,7 @@ Copyright 2016 SmartBear Software from pprint import pformat from six import iteritems +import re class Category(object): @@ -69,6 +70,7 @@ class Category(object): :param id: The id of this Category. :type: int """ + self._id = id @property @@ -91,6 +93,7 @@ class Category(object): :param name: The name of this Category. :type: str """ + self._name = name def to_dict(self): diff --git a/samples/client/petstore/python/swagger_client/models/dog.py b/samples/client/petstore/python/swagger_client/models/dog.py index 3885dd314ef..ac25ef01807 100644 --- a/samples/client/petstore/python/swagger_client/models/dog.py +++ b/samples/client/petstore/python/swagger_client/models/dog.py @@ -20,6 +20,7 @@ Copyright 2016 SmartBear Software from pprint import pformat from six import iteritems +import re class Dog(object): @@ -69,6 +70,7 @@ class Dog(object): :param class_name: The class_name of this Dog. :type: str """ + self._class_name = class_name @property @@ -91,6 +93,7 @@ class Dog(object): :param breed: The breed of this Dog. :type: str """ + self._breed = breed def to_dict(self): diff --git a/samples/client/petstore/python/swagger_client/models/format_test.py b/samples/client/petstore/python/swagger_client/models/format_test.py index 28f348edf04..eee1b5c9ef7 100644 --- a/samples/client/petstore/python/swagger_client/models/format_test.py +++ b/samples/client/petstore/python/swagger_client/models/format_test.py @@ -20,6 +20,7 @@ Copyright 2016 SmartBear Software from pprint import pformat from six import iteritems +import re class FormatTest(object): @@ -102,6 +103,14 @@ class FormatTest(object): :param integer: The integer of this FormatTest. :type: int """ + + if not integer: + raise ValueError("Invalid value for `integer`, must not be `None`") + if integer > 100.0: + raise ValueError("Invalid value for `integer`, must be a value less than or equal to `100.0`") + if integer < 10.0: + raise ValueError("Invalid value for `integer`, must be a value greater than or equal to `10.0`") + self._integer = integer @property @@ -124,6 +133,14 @@ class FormatTest(object): :param int32: The int32 of this FormatTest. :type: int """ + + if not int32: + raise ValueError("Invalid value for `int32`, must not be `None`") + if int32 > 200.0: + raise ValueError("Invalid value for `int32`, must be a value less than or equal to `200.0`") + if int32 < 20.0: + raise ValueError("Invalid value for `int32`, must be a value greater than or equal to `20.0`") + self._int32 = int32 @property @@ -146,6 +163,7 @@ class FormatTest(object): :param int64: The int64 of this FormatTest. :type: int """ + self._int64 = int64 @property @@ -168,6 +186,14 @@ class FormatTest(object): :param number: The number of this FormatTest. :type: float """ + + if not number: + raise ValueError("Invalid value for `number`, must not be `None`") + if number > 543.2: + raise ValueError("Invalid value for `number`, must be a value less than or equal to `543.2`") + if number < 32.1: + raise ValueError("Invalid value for `number`, must be a value greater than or equal to `32.1`") + self._number = number @property @@ -190,6 +216,14 @@ class FormatTest(object): :param float: The float of this FormatTest. :type: float """ + + if not float: + raise ValueError("Invalid value for `float`, must not be `None`") + if float > 987.6: + raise ValueError("Invalid value for `float`, must be a value less than or equal to `987.6`") + if float < 54.3: + raise ValueError("Invalid value for `float`, must be a value greater than or equal to `54.3`") + self._float = float @property @@ -212,6 +246,14 @@ class FormatTest(object): :param double: The double of this FormatTest. :type: float """ + + if not double: + raise ValueError("Invalid value for `double`, must not be `None`") + if double > 123.4: + raise ValueError("Invalid value for `double`, must be a value less than or equal to `123.4`") + if double < 67.8: + raise ValueError("Invalid value for `double`, must be a value greater than or equal to `67.8`") + self._double = double @property @@ -234,6 +276,12 @@ class FormatTest(object): :param string: The string of this FormatTest. :type: str """ + + if not string: + raise ValueError("Invalid value for `string`, must not be `None`") + if not re.match('/[a-z]/i', string): + raise ValueError("Invalid value for `string`, must be a follow pattern or equal to `/[a-z]/i`") + self._string = string @property @@ -256,6 +304,7 @@ class FormatTest(object): :param byte: The byte of this FormatTest. :type: str """ + self._byte = byte @property @@ -278,6 +327,7 @@ class FormatTest(object): :param binary: The binary of this FormatTest. :type: str """ + self._binary = binary @property @@ -300,6 +350,7 @@ class FormatTest(object): :param date: The date of this FormatTest. :type: date """ + self._date = date @property @@ -322,6 +373,7 @@ class FormatTest(object): :param date_time: The date_time of this FormatTest. :type: datetime """ + self._date_time = date_time @property @@ -344,6 +396,7 @@ class FormatTest(object): :param uuid: The uuid of this FormatTest. :type: str """ + self._uuid = uuid @property @@ -366,6 +419,14 @@ class FormatTest(object): :param password: The password of this FormatTest. :type: str """ + + if not password: + raise ValueError("Invalid value for `password`, must not be `None`") + if len(password) > 64: + raise ValueError("Invalid value for `password`, length must be less than `64`") + if len(password) < 10: + raise ValueError("Invalid value for `password`, length must be greater than or equal to `10`") + self._password = password def to_dict(self): diff --git a/samples/client/petstore/python/swagger_client/models/model_200_response.py b/samples/client/petstore/python/swagger_client/models/model_200_response.py index 681de963e6e..f99847cbdd5 100644 --- a/samples/client/petstore/python/swagger_client/models/model_200_response.py +++ b/samples/client/petstore/python/swagger_client/models/model_200_response.py @@ -20,6 +20,7 @@ Copyright 2016 SmartBear Software from pprint import pformat from six import iteritems +import re class Model200Response(object): @@ -66,6 +67,7 @@ class Model200Response(object): :param name: The name of this Model200Response. :type: int """ + self._name = name def to_dict(self): diff --git a/samples/client/petstore/python/swagger_client/models/model_return.py b/samples/client/petstore/python/swagger_client/models/model_return.py index 75b46259d6f..8228f41943c 100644 --- a/samples/client/petstore/python/swagger_client/models/model_return.py +++ b/samples/client/petstore/python/swagger_client/models/model_return.py @@ -20,6 +20,7 @@ Copyright 2016 SmartBear Software from pprint import pformat from six import iteritems +import re class ModelReturn(object): @@ -66,6 +67,7 @@ class ModelReturn(object): :param _return: The _return of this ModelReturn. :type: int """ + self.__return = _return def to_dict(self): diff --git a/samples/client/petstore/python/swagger_client/models/name.py b/samples/client/petstore/python/swagger_client/models/name.py index 4fbf1a03211..51345d131ff 100644 --- a/samples/client/petstore/python/swagger_client/models/name.py +++ b/samples/client/petstore/python/swagger_client/models/name.py @@ -20,6 +20,7 @@ Copyright 2016 SmartBear Software from pprint import pformat from six import iteritems +import re class Name(object): @@ -72,6 +73,7 @@ class Name(object): :param name: The name of this Name. :type: int """ + self._name = name @property @@ -94,6 +96,7 @@ class Name(object): :param snake_case: The snake_case of this Name. :type: int """ + self._snake_case = snake_case @property @@ -116,6 +119,7 @@ class Name(object): :param _property: The _property of this Name. :type: str """ + self.__property = _property def to_dict(self): diff --git a/samples/client/petstore/python/swagger_client/models/order.py b/samples/client/petstore/python/swagger_client/models/order.py index 8fe90765e03..07f2c627d91 100644 --- a/samples/client/petstore/python/swagger_client/models/order.py +++ b/samples/client/petstore/python/swagger_client/models/order.py @@ -20,6 +20,7 @@ Copyright 2016 SmartBear Software from pprint import pformat from six import iteritems +import re class Order(object): @@ -81,6 +82,7 @@ class Order(object): :param id: The id of this Order. :type: int """ + self._id = id @property @@ -103,6 +105,7 @@ class Order(object): :param pet_id: The pet_id of this Order. :type: int """ + self._pet_id = pet_id @property @@ -125,6 +128,7 @@ class Order(object): :param quantity: The quantity of this Order. :type: int """ + self._quantity = quantity @property @@ -147,6 +151,7 @@ class Order(object): :param ship_date: The ship_date of this Order. :type: datetime """ + self._ship_date = ship_date @property @@ -175,6 +180,7 @@ class Order(object): "Invalid value for `status`, must be one of {0}" .format(allowed_values) ) + self._status = status @property @@ -197,6 +203,7 @@ class Order(object): :param complete: The complete of this Order. :type: bool """ + self._complete = complete def to_dict(self): diff --git a/samples/client/petstore/python/swagger_client/models/pet.py b/samples/client/petstore/python/swagger_client/models/pet.py index dfa614b1e9b..60f762238f3 100644 --- a/samples/client/petstore/python/swagger_client/models/pet.py +++ b/samples/client/petstore/python/swagger_client/models/pet.py @@ -20,6 +20,7 @@ Copyright 2016 SmartBear Software from pprint import pformat from six import iteritems +import re class Pet(object): @@ -81,6 +82,7 @@ class Pet(object): :param id: The id of this Pet. :type: int """ + self._id = id @property @@ -103,6 +105,7 @@ class Pet(object): :param category: The category of this Pet. :type: Category """ + self._category = category @property @@ -125,6 +128,7 @@ class Pet(object): :param name: The name of this Pet. :type: str """ + self._name = name @property @@ -147,6 +151,7 @@ class Pet(object): :param photo_urls: The photo_urls of this Pet. :type: list[str] """ + self._photo_urls = photo_urls @property @@ -169,6 +174,7 @@ class Pet(object): :param tags: The tags of this Pet. :type: list[Tag] """ + self._tags = tags @property @@ -197,6 +203,7 @@ class Pet(object): "Invalid value for `status`, must be one of {0}" .format(allowed_values) ) + self._status = status def to_dict(self): diff --git a/samples/client/petstore/python/swagger_client/models/special_model_name.py b/samples/client/petstore/python/swagger_client/models/special_model_name.py index 191798d7d9a..3e45c0e4496 100644 --- a/samples/client/petstore/python/swagger_client/models/special_model_name.py +++ b/samples/client/petstore/python/swagger_client/models/special_model_name.py @@ -20,6 +20,7 @@ Copyright 2016 SmartBear Software from pprint import pformat from six import iteritems +import re class SpecialModelName(object): @@ -66,6 +67,7 @@ class SpecialModelName(object): :param special_property_name: The special_property_name of this SpecialModelName. :type: int """ + self._special_property_name = special_property_name def to_dict(self): diff --git a/samples/client/petstore/python/swagger_client/models/tag.py b/samples/client/petstore/python/swagger_client/models/tag.py index 50f829d65e3..f09a4c36df3 100644 --- a/samples/client/petstore/python/swagger_client/models/tag.py +++ b/samples/client/petstore/python/swagger_client/models/tag.py @@ -20,6 +20,7 @@ Copyright 2016 SmartBear Software from pprint import pformat from six import iteritems +import re class Tag(object): @@ -69,6 +70,7 @@ class Tag(object): :param id: The id of this Tag. :type: int """ + self._id = id @property @@ -91,6 +93,7 @@ class Tag(object): :param name: The name of this Tag. :type: str """ + self._name = name def to_dict(self): diff --git a/samples/client/petstore/python/swagger_client/models/user.py b/samples/client/petstore/python/swagger_client/models/user.py index fd6d16824b3..87d132a5037 100644 --- a/samples/client/petstore/python/swagger_client/models/user.py +++ b/samples/client/petstore/python/swagger_client/models/user.py @@ -20,6 +20,7 @@ Copyright 2016 SmartBear Software from pprint import pformat from six import iteritems +import re class User(object): @@ -87,6 +88,7 @@ class User(object): :param id: The id of this User. :type: int """ + self._id = id @property @@ -109,6 +111,7 @@ class User(object): :param username: The username of this User. :type: str """ + self._username = username @property @@ -131,6 +134,7 @@ class User(object): :param first_name: The first_name of this User. :type: str """ + self._first_name = first_name @property @@ -153,6 +157,7 @@ class User(object): :param last_name: The last_name of this User. :type: str """ + self._last_name = last_name @property @@ -175,6 +180,7 @@ class User(object): :param email: The email of this User. :type: str """ + self._email = email @property @@ -197,6 +203,7 @@ class User(object): :param password: The password of this User. :type: str """ + self._password = password @property @@ -219,6 +226,7 @@ class User(object): :param phone: The phone of this User. :type: str """ + self._phone = phone @property @@ -241,6 +249,7 @@ class User(object): :param user_status: The user_status of this User. :type: int """ + self._user_status = user_status def to_dict(self): From 25ebd5466de888fa2ac49873466b30792c9f70e9 Mon Sep 17 00:00:00 2001 From: Scott Williams Date: Mon, 9 May 2016 01:11:29 +0100 Subject: [PATCH 8/9] Update pattern to support Perl /pattern/modifiers convention --- .../languages/PythonClientCodegen.java | 56 +++++++++++++++++++ .../src/main/resources/python/api.mustache | 2 +- .../src/main/resources/python/model.mustache | 2 +- samples/client/petstore/python/README.md | 2 +- .../python/swagger_client/apis/fake_api.py | 4 +- .../swagger_client/models/format_test.py | 2 +- 6 files changed, 62 insertions(+), 6 deletions(-) diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/PythonClientCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/PythonClientCodegen.java index 1bc95cfdd67..e4bf48e9d5b 100755 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/PythonClientCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/PythonClientCodegen.java @@ -3,15 +3,21 @@ package io.swagger.codegen.languages; import io.swagger.codegen.CliOption; import io.swagger.codegen.CodegenConfig; import io.swagger.codegen.CodegenConstants; +import io.swagger.codegen.CodegenModel; import io.swagger.codegen.CodegenParameter; +import io.swagger.codegen.CodegenProperty; import io.swagger.codegen.CodegenType; import io.swagger.codegen.DefaultCodegen; import io.swagger.codegen.SupportingFile; import io.swagger.models.properties.*; import java.io.File; +import java.util.ArrayList; import java.util.Arrays; +import java.util.HashMap; import java.util.HashSet; +import java.util.List; +import java.util.Map; import org.apache.commons.lang.StringUtils; @@ -21,6 +27,8 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig protected String apiDocPath = "docs/"; protected String modelDocPath = "docs/"; + protected Map regexModifiers; + private String testFolder; public PythonClientCodegen() { @@ -87,6 +95,14 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig "assert", "else", "if", "pass", "yield", "break", "except", "import", "print", "class", "exec", "in", "raise", "continue", "finally", "is", "return", "def", "for", "lambda", "try", "self")); + + regexModifiers = new HashMap(); + regexModifiers.put('i', "IGNORECASE"); + regexModifiers.put('l', "LOCALE"); + regexModifiers.put('m', "MULTILINE"); + regexModifiers.put('s', "DOTALL"); + regexModifiers.put('u', "UNICODE"); + regexModifiers.put('x', "VERBOSE"); cliOptions.clear(); cliOptions.add(new CliOption(CodegenConstants.PACKAGE_NAME, "python package name (convention: snake_case).") @@ -143,6 +159,46 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig private static String dropDots(String str) { return str.replaceAll("\\.", "_"); } + + @Override + public void postProcessParameter(CodegenParameter parameter){ + postProcessPattern(parameter.pattern, parameter.vendorExtensions); + } + + @Override + public void postProcessModelProperty(CodegenModel model, CodegenProperty property) { + postProcessPattern(property.pattern, property.vendorExtensions); + } + + /* + * The swagger pattern spec follows the Perl convention and style of modifiers. Python + * does not support this in as natural a way so it needs to convert it. See + * https://docs.python.org/2/howto/regex.html#compilation-flags for details. + */ + public void postProcessPattern(String pattern, Map vendorExtensions){ + if(pattern != null) { + int i = pattern.lastIndexOf('/'); + + //Must follow Perl /pattern/modifiers convention + if(pattern.charAt(0) != '/' || i < 2) { + throw new IllegalArgumentException("Pattern must follow the Perl " + + "/pattern/modifiers convention. "+pattern+" is not valid."); + } + + String regex = pattern.substring(1, i).replace("'", "\'"); + List modifiers = new ArrayList(); + + for(char c : pattern.substring(i).toCharArray()) { + if(regexModifiers.containsKey(c)) { + String modifier = regexModifiers.get(c); + modifiers.add(modifier); + } + } + + vendorExtensions.put("x-regex", regex); + vendorExtensions.put("x-modifiers", modifiers); + } + } @Override public CodegenType getTag() { diff --git a/modules/swagger-codegen/src/main/resources/python/api.mustache b/modules/swagger-codegen/src/main/resources/python/api.mustache index 399c7f58451..94458e3f724 100644 --- a/modules/swagger-codegen/src/main/resources/python/api.mustache +++ b/modules/swagger-codegen/src/main/resources/python/api.mustache @@ -116,7 +116,7 @@ class {{classname}}(object): raise ValueError("Invalid value for parameter `{{paramName}}` when calling `{{operationId}}`, must be a value greater than or equal to `{{minimum}}`") {{/minimum}} {{#pattern}} - if '{{paramName}}' in params and not re.match('{{pattern}}', params['{{paramName}}']): + if '{{paramName}}' in params and not re.search('{{vendorExtensions.x-regex}}', params['{{paramName}}']{{#vendorExtensions.x-modifiers}}{{#-first}}, flags={{/-first}}re.{{.}}{{^-last}} | {{/-last}}{{/vendorExtensions.x-modifiers}}): raise ValueError("Invalid value for parameter `{{paramName}}` when calling `{{operationId}}`, must conform to the pattern `{{pattern}}`") {{/pattern}} {{/hasValidation}} diff --git a/modules/swagger-codegen/src/main/resources/python/model.mustache b/modules/swagger-codegen/src/main/resources/python/model.mustache index 0cb98a57ef0..e62fa2cbf66 100644 --- a/modules/swagger-codegen/src/main/resources/python/model.mustache +++ b/modules/swagger-codegen/src/main/resources/python/model.mustache @@ -103,7 +103,7 @@ class {{classname}}(object): raise ValueError("Invalid value for `{{name}}`, must be a value greater than or equal to `{{minimum}}`") {{/minimum}} {{#pattern}} - if not re.match('{{pattern}}', {{name}}): + if not re.search('{{vendorExtensions.x-regex}}', {{name}}{{#vendorExtensions.x-modifiers}}{{#-first}}, flags={{/-first}}re.{{.}}{{^-last}} | {{/-last}}{{/vendorExtensions.x-modifiers}}): raise ValueError("Invalid value for `{{name}}`, must be a follow pattern or equal to `{{pattern}}`") {{/pattern}} {{/hasValidation}} diff --git a/samples/client/petstore/python/README.md b/samples/client/petstore/python/README.md index 990d5e53651..3cdf5029787 100644 --- a/samples/client/petstore/python/README.md +++ b/samples/client/petstore/python/README.md @@ -5,7 +5,7 @@ This Python package is automatically generated by the [Swagger Codegen](https:// - API version: 1.0.0 - Package version: 1.0.0 -- Build date: 2016-05-06T22:43:12.044+01:00 +- Build date: 2016-05-09T01:08:25.311+01:00 - Build package: class io.swagger.codegen.languages.PythonClientCodegen ## Requirements. diff --git a/samples/client/petstore/python/swagger_client/apis/fake_api.py b/samples/client/petstore/python/swagger_client/apis/fake_api.py index ba79874f6c7..5d3903b2d21 100644 --- a/samples/client/petstore/python/swagger_client/apis/fake_api.py +++ b/samples/client/petstore/python/swagger_client/apis/fake_api.py @@ -112,8 +112,8 @@ class FakeApi(object): raise ValueError("Invalid value for parameter `double` when calling `test_endpoint_parameters`, must be a value less than or equal to `123.4`") if 'double' in params and params['double'] < 67.8: raise ValueError("Invalid value for parameter `double` when calling `test_endpoint_parameters`, must be a value greater than or equal to `67.8`") - if 'string' in params and not re.match('[a-z]+', params['string']): - raise ValueError("Invalid value for parameter `string` when calling `test_endpoint_parameters`, must conform to the pattern `[a-z]+`") + if 'string' in params and not re.search('[a-z]', params['string'], flags=re.IGNORECASE): + raise ValueError("Invalid value for parameter `string` when calling `test_endpoint_parameters`, must conform to the pattern `/[a-z]/i`") if 'integer' in params and params['integer'] > 100.0: raise ValueError("Invalid value for parameter `integer` when calling `test_endpoint_parameters`, must be a value less than or equal to `100.0`") if 'integer' in params and params['integer'] < 10.0: diff --git a/samples/client/petstore/python/swagger_client/models/format_test.py b/samples/client/petstore/python/swagger_client/models/format_test.py index eee1b5c9ef7..7fce3351dd4 100644 --- a/samples/client/petstore/python/swagger_client/models/format_test.py +++ b/samples/client/petstore/python/swagger_client/models/format_test.py @@ -279,7 +279,7 @@ class FormatTest(object): if not string: raise ValueError("Invalid value for `string`, must not be `None`") - if not re.match('/[a-z]/i', string): + if not re.search('[a-z]', string, flags=re.IGNORECASE): raise ValueError("Invalid value for `string`, must be a follow pattern or equal to `/[a-z]/i`") self._string = string From 4718c34984cc5a8915f8bf6db41ebd98fd001815 Mon Sep 17 00:00:00 2001 From: Scott Williams Date: Mon, 9 May 2016 01:25:13 +0100 Subject: [PATCH 9/9] Replace tabs with spaces --- .../languages/PythonClientCodegen.java | 54 +++++++++---------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/PythonClientCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/PythonClientCodegen.java index e4bf48e9d5b..c1e34b981bb 100755 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/PythonClientCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/PythonClientCodegen.java @@ -162,42 +162,42 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig @Override public void postProcessParameter(CodegenParameter parameter){ - postProcessPattern(parameter.pattern, parameter.vendorExtensions); + postProcessPattern(parameter.pattern, parameter.vendorExtensions); } - + @Override public void postProcessModelProperty(CodegenModel model, CodegenProperty property) { - postProcessPattern(property.pattern, property.vendorExtensions); + postProcessPattern(property.pattern, property.vendorExtensions); } - /* - * The swagger pattern spec follows the Perl convention and style of modifiers. Python - * does not support this in as natural a way so it needs to convert it. See - * https://docs.python.org/2/howto/regex.html#compilation-flags for details. - */ + /* + * The swagger pattern spec follows the Perl convention and style of modifiers. Python + * does not support this in as natural a way so it needs to convert it. See + * https://docs.python.org/2/howto/regex.html#compilation-flags for details. + */ public void postProcessPattern(String pattern, Map vendorExtensions){ - if(pattern != null) { - int i = pattern.lastIndexOf('/'); - - //Must follow Perl /pattern/modifiers convention - if(pattern.charAt(0) != '/' || i < 2) { - throw new IllegalArgumentException("Pattern must follow the Perl " - + "/pattern/modifiers convention. "+pattern+" is not valid."); - } + if(pattern != null) { + int i = pattern.lastIndexOf('/'); - String regex = pattern.substring(1, i).replace("'", "\'"); - List modifiers = new ArrayList(); + //Must follow Perl /pattern/modifiers convention + if(pattern.charAt(0) != '/' || i < 2) { + throw new IllegalArgumentException("Pattern must follow the Perl " + + "/pattern/modifiers convention. "+pattern+" is not valid."); + } - for(char c : pattern.substring(i).toCharArray()) { - if(regexModifiers.containsKey(c)) { - String modifier = regexModifiers.get(c); - modifiers.add(modifier); - } - } + String regex = pattern.substring(1, i).replace("'", "\'"); + List modifiers = new ArrayList(); - vendorExtensions.put("x-regex", regex); - vendorExtensions.put("x-modifiers", modifiers); - } + for(char c : pattern.substring(i).toCharArray()) { + if(regexModifiers.containsKey(c)) { + String modifier = regexModifiers.get(c); + modifiers.add(modifier); + } + } + + vendorExtensions.put("x-regex", regex); + vendorExtensions.put("x-modifiers", modifiers); + } } @Override