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

feat(python-sdk): remove client side validation from python sdk #457

Merged
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
37 changes: 0 additions & 37 deletions config/clients/python/template/model.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -147,43 +147,6 @@ class {{classname}}:
{{/isContainer}}
{{/isEnum}}
{{^isEnum}}
{{#hasValidation}}
{{#maxLength}}
if (self.local_vars_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}}`")
{{/maxLength}}
{{#minLength}}
if (self.local_vars_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}}`")
{{/minLength}}
{{#maximum}}
if (self.local_vars_configuration.client_side_validation
and {{name}} is not None and {{name}} >{{#exclusiveMaximum}}={{/exclusiveMaximum}} {{maximum}}):
raise ValueError("Invalid value for `{{name}}`, must be a value less than {{^exclusiveMaximum}}or equal to {{/exclusiveMaximum}}`{{maximum}}`")
{{/maximum}}
{{#minimum}}
if (self.local_vars_configuration.client_side_validation
and {{name}} is not None and {{name}} <{{#exclusiveMinimum}}={{/exclusiveMinimum}} {{minimum}}):
raise ValueError("Invalid value for `{{name}}`, must be a value greater than {{^exclusiveMinimum}}or equal to {{/exclusiveMinimum}}`{{minimum}}`")
{{/minimum}}
{{#pattern}}
if (self.local_vars_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}})):
raise ValueError(r"Invalid value for `{{name}}`, must be a follow pattern or equal to `{{{pattern}}}`")
{{/pattern}}
{{#maxItems}}
if (self.local_vars_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}}`")
{{/maxItems}}
{{#minItems}}
if (self.local_vars_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}}`")
{{/minItems}}
{{/hasValidation}}
{{/isEnum}}

self._{{name}} = {{name}}
Expand Down
41 changes: 0 additions & 41 deletions config/clients/python/template/src/api.py.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -206,47 +206,6 @@ class {{classname}}:
{{/allParams}}

{{#allParams}}
{{#hasValidation}}
{{#maxLength}}
if self.api_client.client_side_validation and ('{{paramName}}' in local_var_params and
len(local_var_params['{{paramName}}']) > {{maxLength}}):
raise ApiValueError(
"Invalid value for parameter `{{paramName}}` when calling `{{operationId}}`, length must be less than or equal to `{{maxLength}}`")
{{/maxLength}}
{{#minLength}}
if self.api_client.client_side_validation and ('{{paramName}}' in local_var_params and
len(local_var_params['{{paramName}}']) < {{minLength}}):
raise ApiValueError(
"Invalid value for parameter `{{paramName}}` when calling `{{operationId}}`, length must be greater than or equal to `{{minLength}}`")
{{/minLength}}
{{#maximum}}
if self.api_client.client_side_validation and '{{paramName}}' in local_var_params and local_var_params['{{paramName}}'] >{{#exclusiveMaximum}}={{/exclusiveMaximum}} {{maximum}}:
raise ApiValueError(
"Invalid value for parameter `{{paramName}}` when calling `{{operationId}}`, must be a value less than {{^exclusiveMaximum}}or equal to {{/exclusiveMaximum}}`{{maximum}}`")
{{/maximum}}
{{#minimum}}
if self.api_client.client_side_validation and '{{paramName}}' in local_var_params and local_var_params['{{paramName}}'] <{{#exclusiveMinimum}}={{/exclusiveMinimum}} {{minimum}}:
raise ApiValueError(
"Invalid value for parameter `{{paramName}}` when calling `{{operationId}}`, must be a value greater than {{^exclusiveMinimum}}or equal to {{/exclusiveMinimum}}`{{minimum}}`")
{{/minimum}}
{{#pattern}}
if self.api_client.client_side_validation and '{{paramName}}' in local_var_params and not re.search(r'{{{vendorExtensions.x-regex}}}', local_var_params['{{paramName}}']{{#vendorExtensions.x-modifiers}}{{#-first}}, flags={{/-first}}re.{{.}}{{^-last}} | {{/-last}}{{/vendorExtensions.x-modifiers}}):
raise ApiValueError(
"Invalid value for parameter `{{paramName}}` when calling `{{operationId}}`, must conform to the pattern `{{{pattern}}}`")
{{/pattern}}
{{#maxItems}}
if self.api_client.client_side_validation and ('{{paramName}}' in local_var_params and
len(local_var_params['{{paramName}}']) > {{maxItems}}):
raise ApiValueError(
"Invalid value for parameter `{{paramName}}` when calling `{{operationId}}`, number of items must be less than or equal to `{{maxItems}}`")
{{/maxItems}}
{{#minItems}}
if self.api_client.client_side_validation and ('{{paramName}}' in local_var_params and
len(local_var_params['{{paramName}}']) < {{minItems}}):
raise ApiValueError(
"Invalid value for parameter `{{paramName}}` when calling `{{operationId}}`, number of items must be greater than or equal to `{{minItems}}`")
{{/minItems}}
{{/hasValidation}}
{{#-last}}
{{/-last}}
{{/allParams}}
Expand Down
39 changes: 0 additions & 39 deletions config/clients/python/template/src/configuration.py.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,6 @@ from {{packageName}}.telemetry.counters import TelemetryCounter
from {{packageName}}.telemetry.histograms import TelemetryHistogram
from {{packageName}}.validation import is_well_formed_ulid_string


JSON_SCHEMA_VALIDATION_KEYWORDS = {
'multipleOf', 'maximum', 'exclusiveMaximum',
'minimum', 'exclusiveMinimum', 'maxLength',
'minLength', 'pattern', 'maxItems', 'minItems'
}

class RetryParams:
"""NOTE: This class is auto generated by OpenAPI Generator

Expand Down Expand Up @@ -122,19 +115,6 @@ class Configuration:
then all undeclared properties received by the server are injected into the
additional properties map. In that case, there are undeclared properties, and
nothing to discard.
:param disabled_client_side_validations (string): Comma-separated list of
JSON schema validation keywords to disable JSON schema structural validation
rules. The following keywords may be specified: multipleOf, maximum,
exclusiveMaximum, minimum, exclusiveMinimum, maxLength, minLength, pattern,
maxItems, minItems.
By default, the validation is performed for data generated locally by the client
and data received from the server, independent of any validation performed by
the server side. If the input data does not satisfy the JSON schema validation
rules specified in the OpenAPI document, an exception is raised.
If disabled_client_side_validations is set, structural validation is
disabled. This can be useful to troubleshoot data validation problem, such as
when the OpenAPI document validation rules do not match the actual API data
received by the server.
:param server_index: Index to servers configuration.
:param server_variables: Mapping with string values to replace variables in
templated server configuration. The validation of enums is performed for
Expand All @@ -159,7 +139,6 @@ class Configuration:
api_key=None, api_key_prefix=None,
username=None, password=None,
discard_unknown_keys=False,
disabled_client_side_validations="",
server_index=None, server_variables=None,
server_operation_index=None, server_operation_variables=None,
ssl_ca_cert=None,
Expand Down Expand Up @@ -216,7 +195,6 @@ class Configuration:
"""Password for HTTP basic authentication
"""
self.discard_unknown_keys = discard_unknown_keys
self.disabled_client_side_validations = disabled_client_side_validations
self.logger = {}
"""Logging Settings
"""
Expand Down Expand Up @@ -724,20 +702,3 @@ class Configuration:
Update timeout milliseconds
"""
self._timeout_millisec = value

@property
def disabled_client_side_validations(self):
"""Return disable_client_side_validations."""
return self._disabled_client_side_validations

@disabled_client_side_validations.setter
def disabled_client_side_validations(self, value):
"""Update disable_client_side_validations."""
self._disabled_client_side_validations = {}

if isinstance(value, str) and value:
s = set(filter(None, value.split(",")))
for v in s:
if v not in JSON_SCHEMA_VALIDATION_KEYWORDS:
raise FgaValidationException(f"Invalid keyword: '{v}''")
self._disabled_client_side_validations = s
41 changes: 0 additions & 41 deletions config/clients/python/template/src/sync/api.py.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -194,47 +194,6 @@ class {{classname}}:
{{/allParams}}

{{#allParams}}
{{#hasValidation}}
{{#maxLength}}
if self.api_client.client_side_validation and ('{{paramName}}' in local_var_params and
len(local_var_params['{{paramName}}']) > {{maxLength}}):
raise ApiValueError(
"Invalid value for parameter `{{paramName}}` when calling `{{operationId}}`, length must be less than or equal to `{{maxLength}}`")
{{/maxLength}}
{{#minLength}}
if self.api_client.client_side_validation and ('{{paramName}}' in local_var_params and
len(local_var_params['{{paramName}}']) < {{minLength}}):
raise ApiValueError(
"Invalid value for parameter `{{paramName}}` when calling `{{operationId}}`, length must be greater than or equal to `{{minLength}}`")
{{/minLength}}
{{#maximum}}
if self.api_client.client_side_validation and '{{paramName}}' in local_var_params and local_var_params['{{paramName}}'] >{{#exclusiveMaximum}}={{/exclusiveMaximum}} {{maximum}}:
raise ApiValueError(
"Invalid value for parameter `{{paramName}}` when calling `{{operationId}}`, must be a value less than {{^exclusiveMaximum}}or equal to {{/exclusiveMaximum}}`{{maximum}}`")
{{/maximum}}
{{#minimum}}
if self.api_client.client_side_validation and '{{paramName}}' in local_var_params and local_var_params['{{paramName}}'] <{{#exclusiveMinimum}}={{/exclusiveMinimum}} {{minimum}}:
raise ApiValueError(
"Invalid value for parameter `{{paramName}}` when calling `{{operationId}}`, must be a value greater than {{^exclusiveMinimum}}or equal to {{/exclusiveMinimum}}`{{minimum}}`")
{{/minimum}}
{{#pattern}}
if self.api_client.client_side_validation and '{{paramName}}' in local_var_params and not re.search(r'{{{vendorExtensions.x-regex}}}', local_var_params['{{paramName}}']{{#vendorExtensions.x-modifiers}}{{#-first}}, flags={{/-first}}re.{{.}}{{^-last}} | {{/-last}}{{/vendorExtensions.x-modifiers}}):
raise ApiValueError(
"Invalid value for parameter `{{paramName}}` when calling `{{operationId}}`, must conform to the pattern `{{{pattern}}}`")
{{/pattern}}
{{#maxItems}}
if self.api_client.client_side_validation and ('{{paramName}}' in local_var_params and
len(local_var_params['{{paramName}}']) > {{maxItems}}):
raise ApiValueError(
"Invalid value for parameter `{{paramName}}` when calling `{{operationId}}`, number of items must be less than or equal to `{{maxItems}}`")
{{/maxItems}}
{{#minItems}}
if self.api_client.client_side_validation and ('{{paramName}}' in local_var_params and
len(local_var_params['{{paramName}}']) < {{minItems}}):
raise ApiValueError(
"Invalid value for parameter `{{paramName}}` when calling `{{operationId}}`, number of items must be greater than or equal to `{{minItems}}`")
{{/minItems}}
{{/hasValidation}}
{{#-last}}
{{/-last}}
{{/allParams}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ class TestConfigurationSetDefaultAndGetDefaultCopy:
default_config.username = "user"
default_config.password = "pass"
default_config.discard_unknown_keys = True
default_config.disabled_client_side_validations = "maxLength,minLength"
default_config.server_index = 1
default_config.server_variables = {"variable1": "value1", "variable2": "value2"}
default_config.server_operation_index = {"operation1": 1, "operation2": 2}
Expand Down Expand Up @@ -154,10 +153,6 @@ class TestConfigurationSetDefaultAndGetDefaultCopy:
assert Configuration._default.username == "user"
assert Configuration._default.password == "pass"
assert Configuration._default.discard_unknown_keys is True
assert Configuration._default.disabled_client_side_validations == {
"maxLength",
"minLength",
}
assert Configuration._default.server_index == 1
assert Configuration._default.server_variables == {
"variable1": "value1",
Expand Down Expand Up @@ -193,7 +188,6 @@ class TestConfigurationSetDefaultAndGetDefaultCopy:
default_config.username = "user"
default_config.password = "pass"
default_config.discard_unknown_keys = True
default_config.disabled_client_side_validations = "maxLength,minLength"
default_config.server_index = 1
default_config.server_variables = {"variable1": "value1", "variable2": "value2"}
default_config.server_operation_index = {"operation1": 1, "operation2": 2}
Expand Down Expand Up @@ -259,20 +253,6 @@ class TestConfigurationLogging:
configuration.logger_file = "debug.log"
assert configuration.logger_file == "debug.log"

def test_configuration_disabled_client_side_validations(self, configuration):
assert configuration.disabled_client_side_validations == {}
configuration.disabled_client_side_validations = "maxLength,minLength"
assert configuration.disabled_client_side_validations == {
"maxLength",
"minLength",
}

def test_configuration_disabled_client_side_validations_invalid_keyword(
self, configuration
):
with pytest.raises(FgaValidationException):
configuration.disabled_client_side_validations = "invalidKeyword"


class TestConfigurationHostSettings:
def test_configuration_get_host_settings(self, configuration):
Expand Down Expand Up @@ -344,7 +324,6 @@ class TestConfigurationMiscellaneous:
username="user",
password="pass",
discard_unknown_keys=True,
disabled_client_side_validations="maxLength,minLength",
server_index=1,
server_variables={"variable1": "value1", "variable2": "value2"},
server_operation_index={"operation1": 1, "operation2": 2},
Expand Down Expand Up @@ -375,10 +354,6 @@ class TestConfigurationMiscellaneous:
assert copied_config.username == config.username
assert copied_config.password == config.password
assert copied_config.discard_unknown_keys == config.discard_unknown_keys
assert (
copied_config.disabled_client_side_validations
== config.disabled_client_side_validations
)
assert copied_config.server_index == config.server_index
assert copied_config.server_variables == config.server_variables
assert copied_config.server_operation_index == config.server_operation_index
Expand Down
Loading