Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Python] Option to skip client validations #9719

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions modules/swagger-codegen/src/main/resources/python/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -100,44 +100,44 @@ class {{classname}}(object):
{{#allParams}}
{{#required}}
# verify the required parameter '{{paramName}}' is set
if ('{{paramName}}' not in params or
params['{{paramName}}'] is None):
if self.api_client.client_side_validation and ('{{paramName}}' not in params or
params['{{paramName}}'] is None): # noqa: E501
raise ValueError("Missing the required parameter `{{paramName}}` when calling `{{operationId}}`") # noqa: E501
{{/required}}
{{/allParams}}

{{#allParams}}
{{#hasValidation}}
{{#maxLength}}
if ('{{paramName}}' in params and
len(params['{{paramName}}']) > {{maxLength}}):
if self.api_client.client_side_validation and ('{{paramName}}' in params and
len(params['{{paramName}}']) > {{maxLength}}):
raise ValueError("Invalid value for parameter `{{paramName}}` when calling `{{operationId}}`, length must be less than or equal to `{{maxLength}}`") # noqa: E501
{{/maxLength}}
{{#minLength}}
if ('{{paramName}}' in params and
len(params['{{paramName}}']) < {{minLength}}):
if self.api_client.client_side_validation and ('{{paramName}}' in params and
len(params['{{paramName}}']) < {{minLength}}):
raise ValueError("Invalid value for parameter `{{paramName}}` when calling `{{operationId}}`, length must be greater than or equal to `{{minLength}}`") # noqa: E501
{{/minLength}}
{{#maximum}}
if '{{paramName}}' in params and params['{{paramName}}'] >{{#exclusiveMaximum}}={{/exclusiveMaximum}} {{maximum}}: # noqa: E501
if self.api_client.client_side_validation and ('{{paramName}}' in params and params['{{paramName}}'] >{{#exclusiveMaximum}}={{/exclusiveMaximum}} {{maximum}}): # noqa: E501
raise ValueError("Invalid value for parameter `{{paramName}}` when calling `{{operationId}}`, must be a value less than {{^exclusiveMaximum}}or equal to {{/exclusiveMaximum}}`{{maximum}}`") # noqa: E501
{{/maximum}}
{{#minimum}}
if '{{paramName}}' in params and params['{{paramName}}'] <{{#exclusiveMinimum}}={{/exclusiveMinimum}} {{minimum}}: # noqa: E501
if self.api_client.client_side_validation and ('{{paramName}}' in params and params['{{paramName}}'] <{{#exclusiveMinimum}}={{/exclusiveMinimum}} {{minimum}}): # noqa: E501
raise ValueError("Invalid value for parameter `{{paramName}}` when calling `{{operationId}}`, must be a value greater than {{^exclusiveMinimum}}or equal to {{/exclusiveMinimum}}`{{minimum}}`") # noqa: E501
{{/minimum}}
{{#pattern}}
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
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
raise ValueError("Invalid value for parameter `{{paramName}}` when calling `{{operationId}}`, must conform to the pattern `{{{pattern}}}`") # noqa: E501
{{/pattern}}
{{#maxItems}}
if ('{{paramName}}' in params and
len(params['{{paramName}}']) > {{maxItems}}):
if self.api_client.client_side_validation and ('{{paramName}}' in params and
len(params['{{paramName}}']) > {{maxItems}}):
raise ValueError("Invalid value for parameter `{{paramName}}` when calling `{{operationId}}`, number of items must be less than or equal to `{{maxItems}}`") # noqa: E501
{{/maxItems}}
{{#minItems}}
if ('{{paramName}}' in params and
len(params['{{paramName}}']) < {{minItems}}):
if self.api_client.client_side_validation and ('{{paramName}}' in params and
len(params['{{paramName}}']) < {{minItems}}):
raise ValueError("Invalid value for parameter `{{paramName}}` when calling `{{operationId}}`, number of items must be greater than or equal to `{{minItems}}`") # noqa: E501
{{/minItems}}
{{/hasValidation}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class ApiClient(object):
self.cookie = cookie
# Set default User-Agent.
self.user_agent = '{{#httpUserAgent}}{{{.}}}{{/httpUserAgent}}{{^httpUserAgent}}Swagger-Codegen/{{{packageVersion}}}/python{{/httpUserAgent}}'
self.client_side_validation = configuration.client_side_validation

def __del__(self):
if self._pool is not None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ class Configuration(object):
# Safe chars for path_param
self.safe_chars_for_path_param = ''

# Disable client side validation
self.client_side_validation = True

@classmethod
def set_default(cls, default):
cls._default = default
Expand Down
46 changes: 32 additions & 14 deletions modules/swagger-codegen/src/main/resources/python/model.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import re # noqa: F401

import six

from {{packageName}}.configuration import Configuration


{{#models}}
{{#model}}
Expand Down Expand Up @@ -50,8 +52,11 @@ class {{classname}}(object):
}
{{/discriminator}}

def __init__(self{{#vars}}, {{name}}={{#defaultValue}}{{{defaultValue}}}{{/defaultValue}}{{^defaultValue}}None{{/defaultValue}}{{/vars}}): # noqa: E501
def __init__(self{{#vars}}, {{name}}={{#defaultValue}}{{{defaultValue}}}{{/defaultValue}}{{^defaultValue}}None{{/defaultValue}}{{/vars}}, _configuration=None): # noqa: E501
"""{{classname}} - a model defined in Swagger""" # noqa: E501
if _configuration is None:
_configuration = Configuration()
self._configuration = _configuration
{{#vars}}{{#-first}}
{{/-first}}
self._{{name}} = None
Expand Down Expand Up @@ -94,22 +99,24 @@ class {{classname}}(object):
:type: {{datatype}}
"""
{{#required}}
if {{name}} is None:
if self._configuration.client_side_validation and {{name}} is None:
raise ValueError("Invalid value for `{{name}}`, must not be `None`") # noqa: E501
{{/required}}
{{#isEnum}}
{{#isContainer}}
allowed_values = [{{#allowableValues}}{{#values}}{{#items.isString}}"{{/items.isString}}{{{this}}}{{#items.isString}}"{{/items.isString}}{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}] # noqa: E501
{{#isListContainer}}
if not set({{{name}}}).issubset(set(allowed_values)):
if (self._configuration.client_side_validation and
not set({{{name}}}).issubset(set(allowed_values))): # noqa: E501
raise ValueError(
"Invalid values for `{{{name}}}` [{0}], must be a subset of [{1}]" # noqa: E501
.format(", ".join(map(str, set({{{name}}}) - set(allowed_values))), # noqa: E501
", ".join(map(str, allowed_values)))
)
{{/isListContainer}}
{{#isMapContainer}}
if not set({{{name}}}.keys()).issubset(set(allowed_values)):
if (self._configuration.client_side_validation and
not set({{{name}}}.keys()).issubset(set(allowed_values))): # noqa: E501
raise ValueError(
"Invalid keys in `{{{name}}}` [{0}], must be a subset of [{1}]" # noqa: E501
.format(", ".join(map(str, set({{{name}}}.keys()) - set(allowed_values))), # noqa: E501
Expand All @@ -119,7 +126,8 @@ class {{classname}}(object):
{{/isContainer}}
{{^isContainer}}
allowed_values = [{{#allowableValues}}{{#values}}{{#isString}}"{{/isString}}{{{this}}}{{#isString}}"{{/isString}}{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}] # noqa: E501
if {{{name}}} not in allowed_values:
if (self._configuration.client_side_validation and
{{{name}}} not in allowed_values):
raise ValueError(
"Invalid value for `{{{name}}}` ({0}), must be one of {1}" # noqa: E501
.format({{{name}}}, allowed_values)
Expand All @@ -129,31 +137,38 @@ class {{classname}}(object):
{{^isEnum}}
{{#hasValidation}}
{{#maxLength}}
if {{name}} is not None and len({{name}}) > {{maxLength}}:
if (self._configuration.client_side_validation and
{{name}} is not None and len({{name}}) > {{maxLength}}):
raise ValueError("Invalid value for `{{name}}`, length must be less than or equal to `{{maxLength}}`") # noqa: E501
{{/maxLength}}
{{#minLength}}
if {{name}} is not None and len({{name}}) < {{minLength}}:
if (self._configuration.client_side_validation and
{{name}} is not None and len({{name}}) < {{minLength}}):
raise ValueError("Invalid value for `{{name}}`, length must be greater than or equal to `{{minLength}}`") # noqa: E501
{{/minLength}}
{{#maximum}}
if {{name}} is not None and {{name}} >{{#exclusiveMaximum}}={{/exclusiveMaximum}} {{maximum}}: # noqa: E501
if (self._configuration.client_side_validation and
{{name}} is not None and {{name}} >{{#exclusiveMaximum}}={{/exclusiveMaximum}} {{maximum}}): # noqa: E501
raise ValueError("Invalid value for `{{name}}`, must be a value less than {{^exclusiveMaximum}}or equal to {{/exclusiveMaximum}}`{{maximum}}`") # noqa: E501
{{/maximum}}
{{#minimum}}
if {{name}} is not None and {{name}} <{{#exclusiveMinimum}}={{/exclusiveMinimum}} {{minimum}}: # noqa: E501
if (self._configuration.client_side_validation and
{{name}} is not None and {{name}} <{{#exclusiveMinimum}}={{/exclusiveMinimum}} {{minimum}}): # noqa: E501
raise ValueError("Invalid value for `{{name}}`, must be a value greater than {{^exclusiveMinimum}}or equal to {{/exclusiveMinimum}}`{{minimum}}`") # noqa: E501
{{/minimum}}
{{#pattern}}
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
if (self._configuration.client_side_validation and
{{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
raise ValueError(r"Invalid value for `{{name}}`, must be a follow pattern or equal to `{{{pattern}}}`") # noqa: E501
{{/pattern}}
{{#maxItems}}
if {{name}} is not None and len({{name}}) > {{maxItems}}:
if (self._configuration.client_side_validation and
{{name}} is not None and len({{name}}) > {{maxItems}}):
raise ValueError("Invalid value for `{{name}}`, number of items must be less than or equal to `{{maxItems}}`") # noqa: E501
{{/maxItems}}
{{#minItems}}
if {{name}} is not None and len({{name}}) < {{minItems}}:
if (self._configuration.client_side_validation and
{{name}} is not None and len({{name}}) < {{minItems}}):
raise ValueError("Invalid value for `{{name}}`, number of items must be greater than or equal to `{{minItems}}`") # noqa: E501
{{/minItems}}
{{/hasValidation}}
Expand Down Expand Up @@ -209,10 +224,13 @@ class {{classname}}(object):
if not isinstance(other, {{classname}}):
return False

return self.__dict__ == other.__dict__
return self.to_dict() == other.to_dict()

def __ne__(self, other):
"""Returns true if both objects are not equal"""
return not self == other
if not isinstance(other, {{classname}}):
return True

return self.to_dict() != other.to_dict()
{{/model}}
{{/models}}
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ def __init__(self, configuration=None, header_name=None, header_value=None,
self.cookie = cookie
# Set default User-Agent.
self.user_agent = 'Swagger-Codegen/1.0.0/python'
self.client_side_validation = configuration.client_side_validation

def __del__(self):
if self._pool is not None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ def __init__(self):
# Safe chars for path_param
self.safe_chars_for_path_param = ''

# Disable client side validation
self.client_side_validation = True

@classmethod
def set_default(cls, default):
cls._default = default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

import six

from petstore_api.configuration import Configuration


class ModelReturn(object):
"""NOTE: This class is auto generated by the swagger code generator program.
Expand All @@ -38,8 +40,11 @@ class ModelReturn(object):
'_return': 'return'
}

def __init__(self, _return=None): # noqa: E501
def __init__(self, _return=None, _configuration=None): # noqa: E501
"""ModelReturn - a model defined in Swagger""" # noqa: E501
if _configuration is None:
_configuration = Configuration()
self._configuration = _configuration

self.__return = None
self.discriminator = None
Expand Down Expand Up @@ -110,8 +115,11 @@ def __eq__(self, other):
if not isinstance(other, ModelReturn):
return False

return self.__dict__ == other.__dict__
return self.to_dict() == other.to_dict()

def __ne__(self, other):
"""Returns true if both objects are not equal"""
return not self == other
if not isinstance(other, ModelReturn):
return True

return self.to_dict() != other.to_dict()
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ def test_special_tags_with_http_info(self, body, **kwargs): # noqa: E501
params[key] = val
del params['kwargs']
# verify the required parameter 'body' is set
if ('body' not in params or
params['body'] is None):
if self.api_client.client_side_validation and ('body' not in params or
params['body'] is None): # noqa: E501
raise ValueError("Missing the required parameter `body` when calling `test_special_tags`") # noqa: E501

collection_formats = {}
Expand Down
Loading