def test_deserialize_isosceles_triangle_do_not_discard_unknown_properties( self): """ deserialize IsoscelesTriangle with unknown properties. Strict validation is enabled. Composed schema scenario. """ config = Configuration(discard_unknown_keys=False) api_client = petstore_api.ApiClient(config) data = { 'shape_type': 'Triangle', 'triangle_type': 'EquilateralTriangle', # Below is an unknown property not explicitly declared in the OpenAPI document. # It should not be in the payload because additional properties (undeclared) are # not allowed in the schema (additionalProperties: false). 'unknown_property': 'a-value' } response = MockResponse(data=json.dumps(data)) # Deserializing with strict validation raises an exception because the 'unknown_property' # is undeclared. with self.assertRaises(petstore_api.ApiValueError) as cm: deserialized = api_client.deserialize( response, ((petstore_api.IsoscelesTriangle), ), True) self.assertTrue( re.match('.*Not all inputs were used.*unknown_property.*', str(cm.exception)), 'Exception message: {0}'.format(str(cm.exception)))
def test_deserialize_banana_req_do_not_discard_unknown_properties(self): """ deserialize bananaReq with unknown properties. Strict validation is enabled. Simple (non-composed) schema scenario. """ config = Configuration(discard_unknown_keys=False) api_client = petstore_api.ApiClient(config) data = { 'lengthCm': 21.3, 'sweet': False, # Below is an unknown property not explicitly declared in the OpenAPI document. # It should not be in the payload because additional properties (undeclared) are # not allowed in the bananaReq schema (additionalProperties: false). 'unknown_property': 'a-value' } response = MockResponse(data=json.dumps(data)) # Deserializing with strict validation raises an exception because the 'unknown_property' # is undeclared. with self.assertRaises( petstore_api.exceptions.ApiAttributeError) as cm: deserialized = api_client.deserialize(response, ((petstore_api.BananaReq), ), True) self.assertTrue( re.match("BananaReq has no attribute 'unknown_property' at.*", str(cm.exception)), 'Exception message: {0}'.format(str(cm.exception)))
def parameters_to_tuples(self, params, collection_formats): """Get parameters as list of tuples, formatting collections. :param params: Parameters as dict or list of two-tuples :param dict collection_formats: Parameter collection formats :return: Parameters as list of tuples, collections formatted """ new_params = [] if collection_formats is None: collection_formats = {} for k, v in six.iteritems(params) if isinstance( params, dict) else params: # noqa: E501 if k in collection_formats: collection_format = collection_formats[k] if collection_format == 'multi': new_params.extend((k, value) for value in v) else: if collection_format == 'ssv': delimiter = ' ' elif collection_format == 'tsv': delimiter = '\t' elif collection_format == 'pipes': delimiter = '|' else: # csv is the default delimiter = ',' new_params.append( (k, delimiter.join(str(value) for value in v))) else: new_params.append((k, v)) return new_params
def test_deserialize_dog_do_not_discard_unknown_properties(self): """ deserialize str, Dog) with unknown properties, strict validation is enabled """ config = Configuration(discard_unknown_keys=False) api_client = petstore_api.ApiClient(config) data = { "class_name": "Dog", "color": "black", "breed": "husky", "unknown_property": "a-value" } response = MockResponse(data=json.dumps(data)) # Deserializing with strict validation raises an exception because the 'unknown_property' # is undeclared. with self.assertRaises(petstore_api.ApiValueError) as cm: deserialized = api_client.deserialize(response, ((petstore_api.Dog),), True) self.assertTrue(re.match('.*Not all inputs were used.*unknown_property.*', str(cm.exception)), 'Exception message: {0}'.format(str(cm.exception)))
def test_deserialize_none(self): """ deserialize None """ response = MockResponse(data=json.dumps(None)) error_msg = ( "Invalid type for variable 'received_data'. Required value type is " "datetime and passed type was NoneType at ['received_data']") with self.assertRaises(ApiTypeError) as exc: deserialized = self.deserialize(response, (datetime.datetime, ), True) self.assertEqual(str(exc.exception), error_msg)
def to_str(self): """Returns the string representation of the model""" return str(self.value)
def __call_api(self, resource_path, method, path_params=None, query_params=None, header_params=None, body=None, post_params=None, files=None, response_type=None, auth_settings=None, _return_http_data_only=None, collection_formats=None, _preload_content=True, _request_timeout=None, _host=None, _check_type=None): config = self.configuration # header parameters header_params = header_params or {} header_params.update(self.default_headers) if self.cookie: header_params['Cookie'] = self.cookie if header_params: header_params = self.sanitize_for_serialization(header_params) header_params = dict( self.parameters_to_tuples(header_params, collection_formats)) # path parameters if path_params: path_params = self.sanitize_for_serialization(path_params) path_params = self.parameters_to_tuples(path_params, collection_formats) for k, v in path_params: # specified safe chars, encode everything resource_path = resource_path.replace( '{%s}' % k, quote(str(v), safe=config.safe_chars_for_path_param)) # query parameters if query_params: query_params = self.sanitize_for_serialization(query_params) query_params = self.parameters_to_tuples(query_params, collection_formats) # post parameters if post_params or files: post_params = post_params if post_params else [] post_params = self.sanitize_for_serialization(post_params) post_params = self.parameters_to_tuples(post_params, collection_formats) post_params.extend(self.files_parameters(files)) # body if body: body = self.sanitize_for_serialization(body) # auth setting self.update_params_for_auth(header_params, query_params, auth_settings, resource_path, method, body) # request url if _host is None: url = self.configuration.host + resource_path else: # use server/host defined in path or operation instead url = _host + resource_path try: # perform request and return response response_data = self.request(method, url, query_params=query_params, headers=header_params, post_params=post_params, body=body, _preload_content=_preload_content, _request_timeout=_request_timeout) except ApiException as e: e.body = e.body.decode('utf-8') if six.PY3 else e.body raise e content_type = response_data.getheader('content-type') self.last_response = response_data return_data = response_data if not _preload_content: return (return_data) return return_data if six.PY3 and response_type not in ["file", "bytes"]: match = None if content_type is not None: match = re.search(r"charset=([a-zA-Z\-\d]+)[\s\;]?", content_type) encoding = match.group(1) if match else "utf-8" response_data.data = response_data.data.decode(encoding) # deserialize response data if response_type: return_data = self.deserialize(response_data, response_type, _check_type) else: return_data = None if _return_http_data_only: return (return_data) else: return (return_data, response_data.status, response_data.getheaders())
def __call_api(self, resource_path, method, path_params=None, query_params=None, header_params=None, body=None, post_params=None, files=None, response_type=None, auth_settings=None, _return_http_data_only=None, collection_formats=None, _preload_content=True, _request_timeout=None, _host=None, _check_type=None): config = self.configuration # header parameters header_params = header_params or {} header_params.update(self.default_headers) if self.cookie: header_params['Cookie'] = self.cookie if header_params: header_params = self.sanitize_for_serialization(header_params) header_params = dict( self.parameters_to_tuples(header_params, collection_formats)) # path parameters if path_params: path_params = self.sanitize_for_serialization(path_params) path_params = self.parameters_to_tuples(path_params, collection_formats) for k, v in path_params: # specified safe chars, encode everything resource_path = resource_path.replace( '{%s}' % k, quote(str(v), safe=config.safe_chars_for_path_param)) # query parameters if query_params: query_params = self.sanitize_for_serialization(query_params) query_params = self.parameters_to_tuples(query_params, collection_formats) # post parameters if post_params or files: post_params = post_params if post_params else [] post_params = self.sanitize_for_serialization(post_params) post_params = self.parameters_to_tuples(post_params, collection_formats) post_params.extend(self.files_parameters(files)) # body if body: body = self.sanitize_for_serialization(body) # auth setting self.update_params_for_auth(header_params, query_params, auth_settings, resource_path, method, body) # request url if _host is None: url = self.configuration.host + resource_path else: # use server/host defined in path or operation instead url = _host + resource_path # perform request and return response response_data = self.request(method, url, query_params=query_params, headers=header_params, post_params=post_params, body=body, _preload_content=_preload_content, _request_timeout=_request_timeout) self.last_response = response_data return_data = response_data if _preload_content: # deserialize response data if response_type: return_data = self.deserialize(response_data, response_type, _check_type) else: return_data = None if _return_http_data_only: return (return_data) else: return (return_data, response_data.status, response_data.getheaders())