Skip to content

Commit 74393ff

Browse files
authored
Merge pull request #10847 from swagger-api/arunnalla-py_option_to_skip_client_validations
added option to skip client validaton
2 parents 5aae7d7 + 54716f0 commit 74393ff

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+989
-333
lines changed

modules/swagger-codegen/src/main/resources/python/api.mustache

+13-13
Original file line numberDiff line numberDiff line change
@@ -100,44 +100,44 @@ class {{classname}}(object):
100100
{{#allParams}}
101101
{{#required}}
102102
# verify the required parameter '{{paramName}}' is set
103-
if ('{{paramName}}' not in params or
104-
params['{{paramName}}'] is None):
103+
if self.api_client.client_side_validation and ('{{paramName}}' not in params or
104+
params['{{paramName}}'] is None): # noqa: E501
105105
raise ValueError("Missing the required parameter `{{paramName}}` when calling `{{operationId}}`") # noqa: E501
106106
{{/required}}
107107
{{/allParams}}
108108

109109
{{#allParams}}
110110
{{#hasValidation}}
111111
{{#maxLength}}
112-
if ('{{paramName}}' in params and
113-
len(params['{{paramName}}']) > {{maxLength}}):
112+
if self.api_client.client_side_validation and ('{{paramName}}' in params and
113+
len(params['{{paramName}}']) > {{maxLength}}):
114114
raise ValueError("Invalid value for parameter `{{paramName}}` when calling `{{operationId}}`, length must be less than or equal to `{{maxLength}}`") # noqa: E501
115115
{{/maxLength}}
116116
{{#minLength}}
117-
if ('{{paramName}}' in params and
118-
len(params['{{paramName}}']) < {{minLength}}):
117+
if self.api_client.client_side_validation and ('{{paramName}}' in params and
118+
len(params['{{paramName}}']) < {{minLength}}):
119119
raise ValueError("Invalid value for parameter `{{paramName}}` when calling `{{operationId}}`, length must be greater than or equal to `{{minLength}}`") # noqa: E501
120120
{{/minLength}}
121121
{{#maximum}}
122-
if '{{paramName}}' in params and params['{{paramName}}'] >{{#exclusiveMaximum}}={{/exclusiveMaximum}} {{maximum}}: # noqa: E501
122+
if self.api_client.client_side_validation and ('{{paramName}}' in params and params['{{paramName}}'] >{{#exclusiveMaximum}}={{/exclusiveMaximum}} {{maximum}}): # noqa: E501
123123
raise ValueError("Invalid value for parameter `{{paramName}}` when calling `{{operationId}}`, must be a value less than {{^exclusiveMaximum}}or equal to {{/exclusiveMaximum}}`{{maximum}}`") # noqa: E501
124124
{{/maximum}}
125125
{{#minimum}}
126-
if '{{paramName}}' in params and params['{{paramName}}'] <{{#exclusiveMinimum}}={{/exclusiveMinimum}} {{minimum}}: # noqa: E501
126+
if self.api_client.client_side_validation and ('{{paramName}}' in params and params['{{paramName}}'] <{{#exclusiveMinimum}}={{/exclusiveMinimum}} {{minimum}}): # noqa: E501
127127
raise ValueError("Invalid value for parameter `{{paramName}}` when calling `{{operationId}}`, must be a value greater than {{^exclusiveMinimum}}or equal to {{/exclusiveMinimum}}`{{minimum}}`") # noqa: E501
128128
{{/minimum}}
129129
{{#pattern}}
130-
if '{{paramName}}' in params and not re.search(r'{{{vendorExtensions.x-regex}}}', params['{{paramName}}']{{#vendorExtensions.x-modifiers}}{{#-first}}, flags={{/-first}}re.{{.}}{{^-last}} | {{/-last}}{{/vendorExtensions.x-modifiers}}): # noqa: E501
130+
if self.api_client.client_side_validation and ('{{paramName}}' in params and not re.search(r'{{{vendorExtensions.x-regex}}}', params['{{paramName}}']{{#vendorExtensions.x-modifiers}}{{#-first}}, flags={{/-first}}re.{{.}}{{^-last}} | {{/-last}}{{/vendorExtensions.x-modifiers}})): # noqa: E501
131131
raise ValueError("Invalid value for parameter `{{paramName}}` when calling `{{operationId}}`, must conform to the pattern `{{{pattern}}}`") # noqa: E501
132132
{{/pattern}}
133133
{{#maxItems}}
134-
if ('{{paramName}}' in params and
135-
len(params['{{paramName}}']) > {{maxItems}}):
134+
if self.api_client.client_side_validation and ('{{paramName}}' in params and
135+
len(params['{{paramName}}']) > {{maxItems}}):
136136
raise ValueError("Invalid value for parameter `{{paramName}}` when calling `{{operationId}}`, number of items must be less than or equal to `{{maxItems}}`") # noqa: E501
137137
{{/maxItems}}
138138
{{#minItems}}
139-
if ('{{paramName}}' in params and
140-
len(params['{{paramName}}']) < {{minItems}}):
139+
if self.api_client.client_side_validation and ('{{paramName}}' in params and
140+
len(params['{{paramName}}']) < {{minItems}}):
141141
raise ValueError("Invalid value for parameter `{{paramName}}` when calling `{{operationId}}`, number of items must be greater than or equal to `{{minItems}}`") # noqa: E501
142142
{{/minItems}}
143143
{{/hasValidation}}

modules/swagger-codegen/src/main/resources/python/api_client.mustache

+1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ class ApiClient(object):
6969
self.cookie = cookie
7070
# Set default User-Agent.
7171
self.user_agent = '{{#httpUserAgent}}{{{.}}}{{/httpUserAgent}}{{^httpUserAgent}}Swagger-Codegen/{{{packageVersion}}}/python{{/httpUserAgent}}'
72+
self.client_side_validation = configuration.client_side_validation
7273

7374
def __del__(self):
7475
if self._pool is not None:

modules/swagger-codegen/src/main/resources/python/configuration.mustache

+4-1
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ class Configuration(object):
9090
# Safe chars for path_param
9191
self.safe_chars_for_path_param = ''
9292

93+
# Disable client side validation
94+
self.client_side_validation = True
95+
9396
@classmethod
9497
def set_default(cls, default):
9598
cls._default = default
@@ -199,7 +202,7 @@ class Configuration(object):
199202

200203
if self.refresh_api_key_hook:
201204
self.refresh_api_key_hook(self)
202-
205+
203206
key = self.api_key.get(identifier)
204207
if key:
205208
prefix = self.api_key_prefix.get(identifier)

modules/swagger-codegen/src/main/resources/python/model.mustache

+32-14
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import re # noqa: F401
77

88
import six
99

10+
from {{packageName}}.configuration import Configuration
11+
1012

1113
{{#models}}
1214
{{#model}}
@@ -50,8 +52,11 @@ class {{classname}}(object):
5052
}
5153
{{/discriminator}}
5254

53-
def __init__(self{{#vars}}, {{name}}={{#defaultValue}}{{{defaultValue}}}{{/defaultValue}}{{^defaultValue}}None{{/defaultValue}}{{/vars}}): # noqa: E501
55+
def __init__(self{{#vars}}, {{name}}={{#defaultValue}}{{{defaultValue}}}{{/defaultValue}}{{^defaultValue}}None{{/defaultValue}}{{/vars}}, _configuration=None): # noqa: E501
5456
"""{{classname}} - a model defined in Swagger""" # noqa: E501
57+
if _configuration is None:
58+
_configuration = Configuration()
59+
self._configuration = _configuration
5560
{{#vars}}{{#-first}}
5661
{{/-first}}
5762
self._{{name}} = None
@@ -94,22 +99,24 @@ class {{classname}}(object):
9499
:type: {{datatype}}
95100
"""
96101
{{#required}}
97-
if {{name}} is None:
102+
if self._configuration.client_side_validation and {{name}} is None:
98103
raise ValueError("Invalid value for `{{name}}`, must not be `None`") # noqa: E501
99104
{{/required}}
100105
{{#isEnum}}
101106
{{#isContainer}}
102107
allowed_values = [{{#allowableValues}}{{#values}}{{#items.isString}}"{{/items.isString}}{{{this}}}{{#items.isString}}"{{/items.isString}}{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}] # noqa: E501
103108
{{#isListContainer}}
104-
if not set({{{name}}}).issubset(set(allowed_values)):
109+
if (self._configuration.client_side_validation and
110+
not set({{{name}}}).issubset(set(allowed_values))): # noqa: E501
105111
raise ValueError(
106112
"Invalid values for `{{{name}}}` [{0}], must be a subset of [{1}]" # noqa: E501
107113
.format(", ".join(map(str, set({{{name}}}) - set(allowed_values))), # noqa: E501
108114
", ".join(map(str, allowed_values)))
109115
)
110116
{{/isListContainer}}
111117
{{#isMapContainer}}
112-
if not set({{{name}}}.keys()).issubset(set(allowed_values)):
118+
if (self._configuration.client_side_validation and
119+
not set({{{name}}}.keys()).issubset(set(allowed_values))): # noqa: E501
113120
raise ValueError(
114121
"Invalid keys in `{{{name}}}` [{0}], must be a subset of [{1}]" # noqa: E501
115122
.format(", ".join(map(str, set({{{name}}}.keys()) - set(allowed_values))), # noqa: E501
@@ -119,7 +126,8 @@ class {{classname}}(object):
119126
{{/isContainer}}
120127
{{^isContainer}}
121128
allowed_values = [{{#allowableValues}}{{#values}}{{#isString}}"{{/isString}}{{{this}}}{{#isString}}"{{/isString}}{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}] # noqa: E501
122-
if {{{name}}} not in allowed_values:
129+
if (self._configuration.client_side_validation and
130+
{{{name}}} not in allowed_values):
123131
raise ValueError(
124132
"Invalid value for `{{{name}}}` ({0}), must be one of {1}" # noqa: E501
125133
.format({{{name}}}, allowed_values)
@@ -129,31 +137,38 @@ class {{classname}}(object):
129137
{{^isEnum}}
130138
{{#hasValidation}}
131139
{{#maxLength}}
132-
if {{name}} is not None and len({{name}}) > {{maxLength}}:
140+
if (self._configuration.client_side_validation and
141+
{{name}} is not None and len({{name}}) > {{maxLength}}):
133142
raise ValueError("Invalid value for `{{name}}`, length must be less than or equal to `{{maxLength}}`") # noqa: E501
134143
{{/maxLength}}
135144
{{#minLength}}
136-
if {{name}} is not None and len({{name}}) < {{minLength}}:
145+
if (self._configuration.client_side_validation and
146+
{{name}} is not None and len({{name}}) < {{minLength}}):
137147
raise ValueError("Invalid value for `{{name}}`, length must be greater than or equal to `{{minLength}}`") # noqa: E501
138148
{{/minLength}}
139149
{{#maximum}}
140-
if {{name}} is not None and {{name}} >{{#exclusiveMaximum}}={{/exclusiveMaximum}} {{maximum}}: # noqa: E501
150+
if (self._configuration.client_side_validation and
151+
{{name}} is not None and {{name}} >{{#exclusiveMaximum}}={{/exclusiveMaximum}} {{maximum}}): # noqa: E501
141152
raise ValueError("Invalid value for `{{name}}`, must be a value less than {{^exclusiveMaximum}}or equal to {{/exclusiveMaximum}}`{{maximum}}`") # noqa: E501
142153
{{/maximum}}
143154
{{#minimum}}
144-
if {{name}} is not None and {{name}} <{{#exclusiveMinimum}}={{/exclusiveMinimum}} {{minimum}}: # noqa: E501
155+
if (self._configuration.client_side_validation and
156+
{{name}} is not None and {{name}} <{{#exclusiveMinimum}}={{/exclusiveMinimum}} {{minimum}}): # noqa: E501
145157
raise ValueError("Invalid value for `{{name}}`, must be a value greater than {{^exclusiveMinimum}}or equal to {{/exclusiveMinimum}}`{{minimum}}`") # noqa: E501
146158
{{/minimum}}
147159
{{#pattern}}
148-
if {{name}} is not None and not re.search(r'{{{vendorExtensions.x-regex}}}', {{name}}{{#vendorExtensions.x-modifiers}}{{#-first}}, flags={{/-first}}re.{{.}}{{^-last}} | {{/-last}}{{/vendorExtensions.x-modifiers}}): # noqa: E501
160+
if (self._configuration.client_side_validation and
161+
{{name}} is not None and not re.search(r'{{{vendorExtensions.x-regex}}}', {{name}}{{#vendorExtensions.x-modifiers}}{{#-first}}, flags={{/-first}}re.{{.}}{{^-last}} | {{/-last}}{{/vendorExtensions.x-modifiers}})): # noqa: E501
149162
raise ValueError(r"Invalid value for `{{name}}`, must be a follow pattern or equal to `{{{pattern}}}`") # noqa: E501
150163
{{/pattern}}
151164
{{#maxItems}}
152-
if {{name}} is not None and len({{name}}) > {{maxItems}}:
165+
if (self._configuration.client_side_validation and
166+
{{name}} is not None and len({{name}}) > {{maxItems}}):
153167
raise ValueError("Invalid value for `{{name}}`, number of items must be less than or equal to `{{maxItems}}`") # noqa: E501
154168
{{/maxItems}}
155169
{{#minItems}}
156-
if {{name}} is not None and len({{name}}) < {{minItems}}:
170+
if (self._configuration.client_side_validation and
171+
{{name}} is not None and len({{name}}) < {{minItems}}):
157172
raise ValueError("Invalid value for `{{name}}`, number of items must be greater than or equal to `{{minItems}}`") # noqa: E501
158173
{{/minItems}}
159174
{{/hasValidation}}
@@ -209,10 +224,13 @@ class {{classname}}(object):
209224
if not isinstance(other, {{classname}}):
210225
return False
211226

212-
return self.__dict__ == other.__dict__
227+
return self.to_dict() == other.to_dict()
213228

214229
def __ne__(self, other):
215230
"""Returns true if both objects are not equal"""
216-
return not self == other
231+
if not isinstance(other, {{classname}}):
232+
return True
233+
234+
return self.to_dict() != other.to_dict()
217235
{{/model}}
218236
{{/models}}
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2.4.9-SNAPSHOT
1+
2.4.19-SNAPSHOT

samples/client/petstore/python/README.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -113,22 +113,27 @@ Class | Method | HTTP request | Description
113113
- [ArrayOfArrayOfNumberOnly](docs/ArrayOfArrayOfNumberOnly.md)
114114
- [ArrayOfNumberOnly](docs/ArrayOfNumberOnly.md)
115115
- [ArrayTest](docs/ArrayTest.md)
116+
- [Boolean](docs/Boolean.md)
116117
- [Capitalization](docs/Capitalization.md)
118+
- [Cat](docs/Cat.md)
117119
- [Category](docs/Category.md)
118120
- [ClassModel](docs/ClassModel.md)
119121
- [Client](docs/Client.md)
122+
- [Dog](docs/Dog.md)
120123
- [EnumArrays](docs/EnumArrays.md)
121124
- [EnumClass](docs/EnumClass.md)
122125
- [EnumTest](docs/EnumTest.md)
123126
- [FormatTest](docs/FormatTest.md)
124127
- [HasOnlyReadOnly](docs/HasOnlyReadOnly.md)
128+
- [Ints](docs/Ints.md)
125129
- [List](docs/List.md)
126130
- [MapTest](docs/MapTest.md)
127131
- [MixedPropertiesAndAdditionalPropertiesClass](docs/MixedPropertiesAndAdditionalPropertiesClass.md)
128132
- [Model200Response](docs/Model200Response.md)
129133
- [ModelReturn](docs/ModelReturn.md)
130134
- [Name](docs/Name.md)
131135
- [NumberOnly](docs/NumberOnly.md)
136+
- [Numbers](docs/Numbers.md)
132137
- [Order](docs/Order.md)
133138
- [OuterBoolean](docs/OuterBoolean.md)
134139
- [OuterComposite](docs/OuterComposite.md)
@@ -140,8 +145,6 @@ Class | Method | HTTP request | Description
140145
- [SpecialModelName](docs/SpecialModelName.md)
141146
- [Tag](docs/Tag.md)
142147
- [User](docs/User.md)
143-
- [Cat](docs/Cat.md)
144-
- [Dog](docs/Dog.md)
145148

146149

147150
## Documentation For Authorization
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Boolean
2+
3+
## Properties
4+
Name | Type | Description | Notes
5+
------------ | ------------- | ------------- | -------------
6+
7+
[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)
8+
9+

samples/client/petstore/python/docs/InlineResponse200.md

-15
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Ints
2+
3+
## Properties
4+
Name | Type | Description | Notes
5+
------------ | ------------- | ------------- | -------------
6+
7+
[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)
8+
9+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Numbers
2+
3+
## Properties
4+
Name | Type | Description | Notes
5+
------------ | ------------- | ------------- | -------------
6+
7+
[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)
8+
9+

samples/client/petstore/python/petstore_api/__init__.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,27 @@
3434
from petstore_api.models.array_of_array_of_number_only import ArrayOfArrayOfNumberOnly
3535
from petstore_api.models.array_of_number_only import ArrayOfNumberOnly
3636
from petstore_api.models.array_test import ArrayTest
37+
from petstore_api.models.boolean import Boolean
3738
from petstore_api.models.capitalization import Capitalization
39+
from petstore_api.models.cat import Cat
3840
from petstore_api.models.category import Category
3941
from petstore_api.models.class_model import ClassModel
4042
from petstore_api.models.client import Client
43+
from petstore_api.models.dog import Dog
4144
from petstore_api.models.enum_arrays import EnumArrays
4245
from petstore_api.models.enum_class import EnumClass
4346
from petstore_api.models.enum_test import EnumTest
4447
from petstore_api.models.format_test import FormatTest
4548
from petstore_api.models.has_only_read_only import HasOnlyReadOnly
49+
from petstore_api.models.ints import Ints
4650
from petstore_api.models.list import List
4751
from petstore_api.models.map_test import MapTest
4852
from petstore_api.models.mixed_properties_and_additional_properties_class import MixedPropertiesAndAdditionalPropertiesClass
4953
from petstore_api.models.model200_response import Model200Response
5054
from petstore_api.models.model_return import ModelReturn
5155
from petstore_api.models.name import Name
5256
from petstore_api.models.number_only import NumberOnly
57+
from petstore_api.models.numbers import Numbers
5358
from petstore_api.models.order import Order
5459
from petstore_api.models.outer_boolean import OuterBoolean
5560
from petstore_api.models.outer_composite import OuterComposite
@@ -61,5 +66,3 @@
6166
from petstore_api.models.special_model_name import SpecialModelName
6267
from petstore_api.models.tag import Tag
6368
from petstore_api.models.user import User
64-
from petstore_api.models.cat import Cat
65-
from petstore_api.models.dog import Dog

samples/client/petstore/python/petstore_api/api/another_fake_api.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ def test_special_tags_with_http_info(self, body, **kwargs): # noqa: E501
8787
params[key] = val
8888
del params['kwargs']
8989
# verify the required parameter 'body' is set
90-
if ('body' not in params or
91-
params['body'] is None):
90+
if self.api_client.client_side_validation and ('body' not in params or
91+
params['body'] is None): # noqa: E501
9292
raise ValueError("Missing the required parameter `body` when calling `test_special_tags`") # noqa: E501
9393

9494
collection_formats = {}

0 commit comments

Comments
 (0)