From 2815df09c8d199f313ee1ea9f5d2031196e65962 Mon Sep 17 00:00:00 2001 From: GavinMorrisZA Date: Sat, 30 Nov 2024 11:36:43 +0200 Subject: [PATCH] feat: remove client side validation from python sdk --- config/clients/python/template/model.mustache | 37 ----------------- .../python/template/src/api.py.mustache | 41 ------------------- .../template/src/configuration.py.mustache | 39 ------------------ .../python/template/src/sync/api.py.mustache | 41 ------------------- .../test/configuration_test.py.mustache | 25 ----------- 5 files changed, 183 deletions(-) diff --git a/config/clients/python/template/model.mustache b/config/clients/python/template/model.mustache index 2b4d1b20..d620f96f 100644 --- a/config/clients/python/template/model.mustache +++ b/config/clients/python/template/model.mustache @@ -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}} diff --git a/config/clients/python/template/src/api.py.mustache b/config/clients/python/template/src/api.py.mustache index 36b85937..aafc8d39 100644 --- a/config/clients/python/template/src/api.py.mustache +++ b/config/clients/python/template/src/api.py.mustache @@ -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}} diff --git a/config/clients/python/template/src/configuration.py.mustache b/config/clients/python/template/src/configuration.py.mustache index d5af509f..910ecccc 100644 --- a/config/clients/python/template/src/configuration.py.mustache +++ b/config/clients/python/template/src/configuration.py.mustache @@ -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 @@ -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 @@ -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, @@ -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 """ @@ -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 diff --git a/config/clients/python/template/src/sync/api.py.mustache b/config/clients/python/template/src/sync/api.py.mustache index 32c40fdc..9e232cf5 100644 --- a/config/clients/python/template/src/sync/api.py.mustache +++ b/config/clients/python/template/src/sync/api.py.mustache @@ -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}} diff --git a/config/clients/python/template/test/configuration_test.py.mustache b/config/clients/python/template/test/configuration_test.py.mustache index d8e4abd6..751c820d 100644 --- a/config/clients/python/template/test/configuration_test.py.mustache +++ b/config/clients/python/template/test/configuration_test.py.mustache @@ -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} @@ -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", @@ -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} @@ -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): @@ -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}, @@ -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