def test_deserialize_animal(self): """ deserialize Animal to a Dog instance Animal uses a discriminator which has a map built of child classes that inherrit from Animal This is the swagger (v2) way of doing something like oneOf composition """ from petstore_api.model import animal, dog _response_for_200 = api_client.OpenApiResponse(content={ self.json_content_type: api_client.MediaType(schema=animal.Animal), }, ) data = { 'className': 'Dog', 'color': 'white', 'breed': 'Jack Russel Terrier' } response = self.__response(data) deserialized = _response_for_200.deserialize(response, self.configuration) body = deserialized.body self.assertTrue(isinstance(body, dog.Dog)) self.assertEqual(body.className, 'Dog') self.assertEqual(body.color, 'white') self.assertEqual(body.breed, 'Jack Russel Terrier')
def test_deserialize_float_value(self): """ Deserialize floating point values. """ from petstore_api.model import banana _response_for_200 = api_client.OpenApiResponse(content={ self.json_content_type: api_client.MediaType(schema=banana.Banana), }, ) data = {'lengthCm': 3.1415} response = self.__response(data) deserialized = _response_for_200.deserialize(response, self.configuration) body = deserialized.body self.assertTrue(isinstance(body, banana.Banana)) self.assertTrue(isinstance(body.lengthCm, Decimal)) self.assertEqual(body.lengthCm, 3.1415) """ Float value is serialized without decimal point The client receive it as an integer, which work because Banana.lengthCm is type number without format Which accepts int AND float """ data = {'lengthCm': 3} response = self.__response(data) deserialized = _response_for_200.deserialize(response, self.configuration) body = deserialized.body self.assertTrue(isinstance(body, banana.Banana)) self.assertTrue(isinstance(body.lengthCm, Decimal)) self.assertEqual(body.lengthCm, 3)
def test_deserialize_with_additional_properties_and_reference(self): """ Deserialize data with schemas that has the additionalProperties keyword and the schema is specified as a reference ($ref). """ from petstore_api.model import drawing _response_for_200 = api_client.OpenApiResponse(content={ self.json_content_type: api_client.MediaType(schema=drawing.Drawing), }, ) data = { 'mainShape': { 'shapeType': 'Triangle', 'triangleType': 'EquilateralTriangle', }, 'shapes': [ { 'shapeType': 'Triangle', 'triangleType': 'IsoscelesTriangle', }, { 'shapeType': 'Quadrilateral', 'quadrilateralType': 'ComplexQuadrilateral', }, ], 'an_additional_prop': { 'lengthCm': 4, 'color': 'yellow' } } response = self.__response(data) _response_for_200.deserialize(response, self.configuration)
def test_deserialize_fruit_null_value(self): """ deserialize fruit with null value. fruitReq is a oneOf composed schema model with discriminator, including 'null' type. """ from petstore_api.model import fruit_req _response_for_200 = api_client.OpenApiResponse(content={ self.json_content_type: api_client.MediaType(schema=fruit_req.FruitReq), }, ) data = None response = self.__response(data) deserialized = _response_for_200.deserialize(response, self.configuration) self.assertTrue(isinstance(deserialized.body, fruit_req.FruitReq)) self.assertTrue(isinstance(deserialized.body, NoneClass))
def test_deserialize_shape(self): """ deserialize Shape to an instance of: - EquilateralTriangle - IsoscelesTriangle - IsoscelesTriangle - ScaleneTriangle - ComplexQuadrilateral - SimpleQuadrilateral by traveling through 2 discriminators """ from petstore_api.model import shape, equilateral_triangle _response_for_200 = api_client.OpenApiResponse(content={ self.json_content_type: api_client.MediaType(schema=shape.Shape), }, ) data = { 'shapeType': 'Triangle', 'triangleType': 'EquilateralTriangle', } response = self.__response(data) deserialized = _response_for_200.deserialize(response, self.configuration) body = deserialized.body self.assertTrue( isinstance(body, equilateral_triangle.EquilateralTriangle)) self.assertEqual(body.shapeType, 'Triangle') self.assertEqual(body.triangleType, 'EquilateralTriangle') # invalid quadrilateralType, second discriminator value data = { 'shapeType': 'Quadrilateral', 'quadrilateralType': 'Triangle', } response = self.__response(data) err_msg = ( r"Invalid discriminator value was passed in to Quadrilateral.quadrilateralType Only the values " r"\['ComplexQuadrilateral', 'SimpleQuadrilateral'\] are allowed at \('args\[0\]', 'quadrilateralType'\)" ) with self.assertRaisesRegex(petstore_api.ApiValueError, err_msg): _response_for_200.deserialize(response, self.configuration)
def test_deserialize_mammal(self): """ deserialize mammal mammal is a oneOf composed schema model with discriminator """ # whale test from petstore_api.model import mammal, zebra, whale _response_for_200 = api_client.OpenApiResponse(content={ self.json_content_type: api_client.MediaType(schema=mammal.Mammal), }, ) has_baleen = True has_teeth = False class_name = 'whale' data = { 'hasBaleen': has_baleen, 'hasTeeth': has_teeth, 'className': class_name } response = self.__response(data) deserialized = _response_for_200.deserialize(response, self.configuration) body = deserialized.body self.assertTrue(isinstance(body, whale.Whale)) self.assertEqual(bool(body.hasBaleen), has_baleen) self.assertEqual(bool(body.hasTeeth), has_teeth) self.assertEqual(body.className, class_name) # zebra test zebra_type = 'plains' class_name = 'zebra' data = {'type': zebra_type, 'className': class_name} response = self.__response(data) deserialized = _response_for_200.deserialize(response, self.configuration) body = deserialized.body self.assertTrue(isinstance(body, zebra.Zebra)) self.assertEqual(body.type, zebra_type) self.assertEqual(body.className, class_name)
}, required=True, ) _path = '/user/createWithArray' _method = 'POST' @dataclass class ApiResponseForDefault(api_client.ApiResponse): response: urllib3.HTTPResponse body: Unset = unset headers: Unset = unset _response_for_default = api_client.OpenApiResponse( response_cls=ApiResponseForDefault, ) _status_code_to_response = { 'default': _response_for_default, } class CreateUsersWithArrayInput(api_client.Api): def create_users_with_array_input( self: api_client.Api, body: typing.Union[SchemaForRequestBodyApplicationJson], content_type: str = 'application/json', stream: bool = False, timeout: typing.Optional[typing.Union[int, typing.Tuple]] = None, skip_deserialization: bool = False,
}, required=True, ) _path = '/user/{username}' _method = 'PUT' @dataclass class ApiResponseFor400(api_client.ApiResponse): response: urllib3.HTTPResponse body: Unset = unset headers: Unset = unset _response_for_400 = api_client.OpenApiResponse( response_cls=ApiResponseFor400, ) @dataclass class ApiResponseFor404(api_client.ApiResponse): response: urllib3.HTTPResponse body: Unset = unset headers: Unset = unset _response_for_404 = api_client.OpenApiResponse( response_cls=ApiResponseFor404, ) _status_code_to_response = { '400': _response_for_400,
_path = '/pet/{petId}' _method = 'POST' _auth = [ 'petstore_auth', ] @dataclass class ApiResponseFor405(api_client.ApiResponse): response: urllib3.HTTPResponse body: Unset = unset headers: Unset = unset _response_for_405 = api_client.OpenApiResponse( response_cls=ApiResponseFor405, ) _status_code_to_response = { '405': _response_for_405, } class UpdatePetWithForm(api_client.Api): def update_pet_with_form( self: api_client.Api, body: typing.Union[SchemaForRequestBodyApplicationXWwwFormUrlencoded, Unset] = unset, path_params: RequestPathParams = frozendict(), content_type: str = 'application/x-www-form-urlencoded', stream: bool = False, timeout: typing.Optional[typing.Union[int, typing.Tuple]] = None,
SchemaFor200ResponseBodyApplicationOctetStream = BinarySchema @dataclass class ApiResponseFor200(api_client.ApiResponse): response: urllib3.HTTPResponse body: typing.Union[ SchemaFor200ResponseBodyApplicationOctetStream, ] headers: Unset = unset _response_for_200 = api_client.OpenApiResponse( response_cls=ApiResponseFor200, content={ 'application/octet-stream': api_client.MediaType( schema=SchemaFor200ResponseBodyApplicationOctetStream), }, ) _status_code_to_response = { '200': _response_for_200, } _all_accept_content_types = ( 'application/octet-stream', ) class UploadDownloadFile(api_client.Api): def upload_download_file( self: api_client.Api,
@dataclass class ApiResponseFor200(api_client.ApiResponse): response: urllib3.HTTPResponse body: typing.Union[SchemaFor200ResponseBodyApplicationXml, SchemaFor200ResponseBodyApplicationJson, ] headers: ResponseHeadersFor200 _response_for_200 = api_client.OpenApiResponse( response_cls=ApiResponseFor200, content={ 'application/xml': api_client.MediaType(schema=SchemaFor200ResponseBodyApplicationXml), 'application/json': api_client.MediaType(schema=SchemaFor200ResponseBodyApplicationJson), }, headers=[ x_rate_limit_parameter, x_expires_after_parameter, ]) @dataclass class ApiResponseFor400(api_client.ApiResponse): response: urllib3.HTTPResponse body: Unset = unset headers: Unset = unset _response_for_400 = api_client.OpenApiResponse(
@dataclass class ApiResponseFor200(api_client.ApiResponse): response: urllib3.HTTPResponse body: typing.Union[ SchemaFor200ResponseBodyApplicationJson, SchemaFor200ResponseBodyMultipartFormData, ] headers: Unset = unset _response_for_200 = api_client.OpenApiResponse( response_cls=ApiResponseFor200, content={ 'application/json': api_client.MediaType( schema=SchemaFor200ResponseBodyApplicationJson), 'multipart/form-data': api_client.MediaType( schema=SchemaFor200ResponseBodyMultipartFormData), }, ) _status_code_to_response = { '200': _response_for_200, } _all_accept_content_types = ( 'application/json', 'multipart/form-data', ) class InlineComposition(api_client.Api):
def test_multiple_of_deserialization(self): data = { 'byte': '3', 'date': '1970-01-01', 'password': "******", 'integer': 30, 'number': 65.0, 'float': 62.4, } from petstore_api.model import format_test _response_for_200 = api_client.OpenApiResponse(content={ self.json_content_type: api_client.MediaType(schema=format_test.FormatTest), }, ) response = self.__response(data) deserialized = _response_for_200.deserialize(response, self.configuration) self.assertTrue(isinstance(deserialized.body, format_test.FormatTest)) with self.assertRaisesRegex( petstore_api.exceptions.ApiValueError, r"Invalid value `31`, value must be a multiple of `2` at \('args\[0\]', 'integer'\)" ): data = { 'byte': '3', 'date': '1970-01-01', 'password': "******", 'integer': 31, # Value is supposed to be multiple of '2'. An error must be raised 'number': 65.0, 'float': 62.4, } response = self.__response(data) _response_for_200.deserialize(response, self.configuration) # Disable JSON schema validation. No error should be raised during deserialization. configuration = petstore_api.Configuration() configuration.disabled_client_side_validations = "multipleOf" data = { 'byte': '3', 'date': '1970-01-01', 'password': "******", 'integer': 31, # Value is supposed to be multiple of '2' 'number': 65.0, 'float': 62.4, } response = self.__response(data) deserialized = _response_for_200.deserialize(response, configuration) self.assertTrue(isinstance(deserialized.body, format_test.FormatTest)) # Disable JSON schema validation but for a different keyword. # An error should be raised during deserialization. configuration = petstore_api.Configuration() configuration.disabled_client_side_validations = "maxItems" with self.assertRaisesRegex( petstore_api.exceptions.ApiValueError, r"Invalid value `31`, value must be a multiple of `2` at \('args\[0\]', 'integer'\)" ): data = { 'byte': '3', 'date': '1970-01-01', 'password': "******", 'integer': 31, # Value is supposed to be multiple of '2' 'number': 65.0, 'float': 62.4, } response = self.__response(data) _response_for_200.deserialize(response, configuration)
def test_deserialize_with_additional_properties(self): """ Deserialize data with schemas that have the additionalProperties keyword. Test conditions when additional properties are allowed, not allowed, have specific types... """ # Dog is allOf with two child schemas. # The OAS document for Dog does not specify the 'additionalProperties' keyword, # which means that by default, the Dog schema must allow undeclared properties. # The additionalProperties keyword is used to control the handling of extra stuff, # that is, properties whose names are not listed in the properties keyword. # By default any additional properties are allowed. from petstore_api.model import dog, mammal, zebra, banana_req data = { 'className': 'Dog', 'color': 'brown', 'breed': 'golden retriever', # Below are additional, undeclared properties. 'group': 'Terrier Group', 'size': 'medium', } response = self.__response(data) _response_for_200 = api_client.OpenApiResponse(content={ self.json_content_type: api_client.MediaType(schema=dog.Dog), }, ) deserialized = _response_for_200.deserialize(response, self.configuration) body = deserialized.body self.assertTrue(isinstance(body, dog.Dog)) self.assertEqual(body.className, 'Dog') self.assertEqual(body.color, 'brown') self.assertEqual(body.breed, 'golden retriever') self.assertEqual(body.group, 'Terrier Group') self.assertEqual(body.size, 'medium') # The 'zebra' schema allows additional properties by explicitly setting # additionalProperties: true. # This is equivalent to 'additionalProperties' not being present. data = { 'className': 'zebra', 'type': 'plains', # Below are additional, undeclared properties 'group': 'abc', 'size': 3, 'p1': True, 'p2': ['a', 'b', 123], } response = self.__response(data) _response_for_200 = api_client.OpenApiResponse(content={ self.json_content_type: api_client.MediaType(schema=mammal.Mammal), }, ) deserialized = _response_for_200.deserialize(response, self.configuration) body = deserialized.body self.assertTrue(isinstance(body, zebra.Zebra)) self.assertEqual(body.className, 'zebra') self.assertEqual(body.type, 'plains') self.assertEqual(bool(body.p1), True) # The 'bananaReq' schema disallows additional properties by explicitly setting # additionalProperties: false _response_for_200 = api_client.OpenApiResponse(content={ self.json_content_type: api_client.MediaType(schema=banana_req.BananaReq), }, ) with self.assertRaisesRegex( petstore_api.exceptions.ApiTypeError, r"BananaReq was passed 1 invalid argument: \['unknown-group'\]" ): data = { 'lengthCm': 21.2, 'sweet': False, # Below are additional, undeclared properties. They are not allowed, # an exception must be raised. 'unknown-group': 'abc', } response = self.__response(data) _response_for_200.deserialize(response, self.configuration)
style=api_client.ParameterStyle.DEEP_OBJECT, schema=MapBeanSchema, explode=True, ) _path = '/fake/objInQuery' _method = 'GET' @dataclass class ApiResponseFor200(api_client.ApiResponse): response: urllib3.HTTPResponse body: Unset = unset headers: Unset = unset _response_for_200 = api_client.OpenApiResponse( response_cls=ApiResponseFor200, ) _status_code_to_response = { '200': _response_for_200, } class ObjectInQuery(api_client.Api): def object_in_query( self: api_client.Api, query_params: RequestQueryParams = frozendict(), stream: bool = False, timeout: typing.Optional[typing.Union[int, typing.Tuple]] = None, skip_deserialization: bool = False, ) -> typing.Union[ApiResponseFor200, api_client.ApiResponseWithoutDeserialization]: """
_path = '/fake/health' _method = 'GET' SchemaFor200ResponseBodyApplicationJson = HealthCheckResult @dataclass class ApiResponseFor200(api_client.ApiResponse): response: urllib3.HTTPResponse body: typing.Union[SchemaFor200ResponseBodyApplicationJson, ] headers: Unset = unset _response_for_200 = api_client.OpenApiResponse( response_cls=ApiResponseFor200, content={ 'application/json': api_client.MediaType(schema=SchemaFor200ResponseBodyApplicationJson), }, ) _status_code_to_response = { '200': _response_for_200, } _all_accept_content_types = ('application/json', ) class FakeHealthGet(api_client.Api): def fake_health_get( self: api_client.Api, accept_content_types: typing.Tuple[str] = _all_accept_content_types, stream: bool = False, timeout: typing.Optional[typing.Union[int, typing.Tuple]] = None,
_path = '/fake/responseWithoutSchema' _method = 'GET' @dataclass class ApiResponseFor200(api_client.ApiResponse): response: urllib3.HTTPResponse body: typing.Union[Unset, Unset, ] headers: Unset = unset _response_for_200 = api_client.OpenApiResponse( response_cls=ApiResponseFor200, content={ 'application/json': api_client.MediaType(), 'application/xml': api_client.MediaType(), }, ) _status_code_to_response = { '200': _response_for_200, } _all_accept_content_types = ( 'application/json', 'application/xml', ) class ResponseWithoutSchema(api_client.Api): def response_without_schema( self: api_client.Api,