def test_get_pets_invalid_response(self, spec, response_validator): host_url = 'http://petstore.swagger.io/v1' path_pattern = '/v1/pets' query_params = { 'limit': '20', } request = MockRequest( host_url, 'GET', '/pets', path_pattern=path_pattern, args=query_params, ) parameters = request.get_parameters(spec) body = request.get_body(spec) assert parameters == { 'query': { 'limit': 20, 'page': 1, 'search': '', } } assert body is None data_json = { 'data': [ { 'id': 1, 'name': { 'first_name': 'Cat', }, } ], } data = json.dumps(data_json) response = MockResponse(data) response_result = response_validator.validate(request, response) assert response_result.errors == [ InvalidMediaTypeValue( original_exception=InvalidSchemaProperty( property_name='data', original_exception=InvalidSchemaProperty( property_name='name', original_exception=InvalidSchemaValue( msg="Value {value} is not of type {type}", type=SchemaType.STRING, value={'first_name': 'Cat'}, ), ), ), ), ] assert response_result.data is None
def _unmarshal_properties(self, value, one_of_schema=None, custom_formatters=None, strict=True, require_all_props=False): all_props = self.get_all_properties() all_props_names = self.get_all_properties_names() all_req_props_names = self.get_all_required_properties_names() if one_of_schema is not None: all_props.update(one_of_schema.get_all_properties()) all_props_names |= one_of_schema.\ get_all_properties_names() all_req_props_names |= one_of_schema.\ get_all_required_properties_names() value_props_names = value.keys() extra_props = set(value_props_names) - set(all_props_names) extra_props_allowed = self.are_additional_properties_allowed( one_of_schema) if extra_props and not extra_props_allowed: raise UndefinedSchemaProperty(extra_props) properties = {} if self.additional_properties is not True: for prop_name in extra_props: prop_value = value[prop_name] properties[prop_name] = self.additional_properties.unmarshal( prop_value, custom_formatters=custom_formatters, require_all_props=require_all_props) for prop_name, prop in iteritems(all_props): try: prop_value = value[prop_name] except KeyError: if prop_name in all_req_props_names: raise MissingSchemaProperty(prop_name) if not prop.nullable and not prop.default: continue prop_value = prop.default try: properties[prop_name] = prop.unmarshal( prop_value, custom_formatters=custom_formatters, require_all_props=require_all_props) except OpenAPISchemaError as exc: raise InvalidSchemaProperty(prop_name, exc) self._validate_properties(properties, one_of_schema=one_of_schema, custom_formatters=custom_formatters, require_all_props=require_all_props) return properties
def _validate_properties(self, value, one_of_schema=None, custom_formatters=None): all_props = self.get_all_properties() all_props_names = self.get_all_properties_names() all_req_props_names = self.get_all_required_properties_names() if one_of_schema is not None: all_props.update(one_of_schema.get_all_properties()) all_props_names |= one_of_schema.\ get_all_properties_names() all_req_props_names |= one_of_schema.\ get_all_required_properties_names() value_props_names = value.keys() extra_props = set(value_props_names) - set(all_props_names) extra_props_allowed = self.are_additional_properties_allowed( one_of_schema) if extra_props and not extra_props_allowed: raise UndefinedSchemaProperty(extra_props) for prop_name in extra_props: prop_value = value[prop_name] if self.additional_properties is True: continue else: # Schema for extra props self.additional_properties.validate( prop_value, custom_formatters=custom_formatters) for prop_name, prop in iteritems(all_props): try: prop_value = value[prop_name] except KeyError: if prop_name in all_req_props_names: raise MissingSchemaProperty(prop_name) if not prop.nullable and not prop.default: continue prop_value = prop.default try: prop.validate(prop_value, custom_formatters=custom_formatters) except OpenAPISchemaError as exc: raise InvalidSchemaProperty(prop_name, original_exception=exc) return True
def _validate_properties(self, value, one_of_schema=None, custom_formatters=None, read=False, write=False): all_props = self.get_all_properties() all_props_names = self.get_all_properties_names() all_req_props_names = self.get_all_required_properties_names() if one_of_schema is not None: all_props.update(one_of_schema.get_all_properties()) all_props_names |= one_of_schema.\ get_all_properties_names() all_req_props_names |= one_of_schema.\ get_all_required_properties_names() value_props_names = value.keys() extra_props = set(value_props_names) - set(all_props_names) extra_props_allowed = self.are_additional_properties_allowed( one_of_schema) if extra_props and not extra_props_allowed: raise UndefinedSchemaProperty(extra_props) if self.additional_properties is not True: for prop_name in extra_props: prop_value = value[prop_name] self.additional_properties.validate( prop_value, custom_formatters=custom_formatters, read=read, write=write) for prop_name, prop in iteritems(all_props): should_skip = (write and prop.read_only) or (read and prop.write_only) try: prop_value = value[prop_name] if read and prop.write_only: message = "WriteOnly property {prop} defined on read.".format( prop=prop_name) raise UndefinedSchemaProperty(message) if write and prop.read_only: message = "ReadOnly property {prop} defined on write.".format( prop=prop_name) raise UndefinedSchemaProperty(message) except KeyError: if prop_name in all_req_props_names and not should_skip: raise MissingSchemaProperty(prop_name) if (not prop.nullable and not prop.default) or should_skip: continue prop_value = prop.default try: prop.validate(prop_value, custom_formatters=custom_formatters, read=read, write=write) except OpenAPISchemaError as exc: raise InvalidSchemaProperty(prop_name, original_exception=exc) return True