def test_parse_object_with_required_and_or_default(): r = make_data_type( { "x-name": "Obj", "type": "object", "properties": { "a": {"type": "string", "default": "a"}, "b": {"type": "string", "default": "b"}, "c": {"type": "string"}, "d": {"type": "string"}, }, "required": ["a", "c"], } ) assert r == ObjectDataType( python_type="Obj", members=[ DataType( python_type="str", member_name="a", member_value="'a'", ), DataType(python_type="str", member_name="b", member_value="'b'"), DataType(python_type="str", member_name="c"), DataType( python_type="str", member_name="d", member_value="None", is_optional_type=True, ), ], )
def test_parse_complex_object(): assert make_data_type({ "x-name": "Obj1", "type": "object", "properties": { "field1": { "type": "integer" }, "field2": { "type": "string", "default": "some-value" }, }, }) == ObjectDataType( python_type="Obj1", members=[ DataType( python_type="int", member_name="field1", member_value="None", is_optional_type=True, ), DataType(python_type="str", member_name="field2", member_value="'some-value'"), ], )
def test_parse_object_with_alias_field(): assert make_data_type( { "x-name": "Obj", "type": "object", "properties": {"fields": {"type": "string", "default": "yes"}}, } ) == ObjectDataType( python_type="Obj", members=[ DataType( python_type="str", member_name="fields_", member_value="pydantic.Field('yes', alias=\"fields\")", ), ], ) assert make_data_type( { "x-name": "Obj", "type": "object", "properties": {"fields": {"type": "integer"}}, } ) == ObjectDataType( python_type="Obj", members=[ DataType( python_type="int", member_name="fields_", member_value='pydantic.Field(None, alias="fields")', ), ], )
def _render_endpoint(self, api_dir: str, endpoint: EndpointDescription): imports_block = render_template(self._endpoint_imports_template, {}) data_types_block = [] data_types_to_render: List[DataType] = [] if endpoint.body_request: data_types_to_render.extend( DataType.get_object_members(endpoint.body_request.data_type)) for response in endpoint.responses: data_types_to_render.extend( DataType.get_object_members(response.data_type)) _rendered_types: Set[str] = set() for data_type in data_types_to_render: if data_type.python_type not in _rendered_types: data_types_block.append( render_template(self._endpoint_dto_template, {"type": data_type})) _rendered_types.add(data_type.python_type) endpoint_block = render_template(self._endpoint_template, {"endpoint": endpoint}) endpoint_content = "\n".join( [imports_block, *data_types_block, endpoint_block]) endpoint_content = self._post_process(endpoint_content) (api_dir / endpoint.name).with_suffix(".py").write_text(endpoint_content)
def test_parse_enum(): data_type = make_data_type({"enum": ["one", "two"]}) assert data_type == DataType(python_type="str") data_type = make_data_type({ "title": "PriceType", "enum": [1], "type": "integer" }) assert data_type == DataType(python_type="int")
def test_parse_string(): assert make_data_type({"type": "string", "format": "binary"}) == DataType(python_type="bytes") assert make_data_type({"type": "string", "format": "date-time"}) == DataType( python_type="datetime.datetime" ) assert make_data_type({"type": "string", "format": "date"}) == DataType( python_type="datetime.date" ) assert make_data_type({"type": "string", "format": "time"}) == DataType(python_type="time.time") assert make_data_type({"type": "string"}) == DataType(python_type="str")
def test_parse_object(): assert make_data_type({"type": "object"}) == DataType(python_type="typing.Dict") assert make_data_type({ "type": "object", "additionalProperties": { "type": "boolean" } }) == DataType(python_type="typing.Dict[str, bool]", members=[DataType(python_type="bool")])
def test_parse_array(): assert make_data_type({ "type": "array", "items": {} }) == DataType(python_type="typing.List") assert make_data_type({ "type": "array", "items": { "type": "integer" } }) == DataType(python_type="typing.List[int]", members=[DataType(python_type="int")])
def test_parse_nested_object(): data_type = make_data_type( { "x-name": "top_level_model", "type": "object", "properties": { "string_property": {"type": "string"}, "nested_property": { "type": "object", "properties": { "number_property": { "type": "number", }, }, }, }, }, ) assert data_type == ObjectDataType( python_type="top_level_model", members=[ DataType( python_type="str", member_name="string_property", member_value="None", is_optional_type=True, is_recursive=False, ), ObjectDataType( python_type="top_level_modelNested_property", member_name="nested_property", member_value="None", is_optional_type=True, is_recursive=False, members=[ DataType( python_type="float", member_name="number_property", is_optional_type=True, member_value="None", ) ], ), ], )
def test_parse_all_of_any_of_one_of(): int_schema = {"type": "integer"} object_schema = { "type": "object", "properties": {"name": {"type": "string"}}, "required": ["name"], } fields = ("oneOf", "allOf", "anyOf") for field in fields: data_type = make_data_type({field: [int_schema]}) assert data_type == DataType(python_type="int", members=[DataType(python_type="int")]) data_type = make_data_type({field: [int_schema, {}]}) assert data_type == DataType( python_type="typing.Union[int, None]", members=[DataType(python_type="int"), DataType(python_type="None")], ) data_type = make_data_type({field: [object_schema]}) assert data_type == DataType( python_type="None", members=[ ObjectDataType( python_type=None, members=[ DataType( python_type="str", member_name="name", ) ], ) ], )
def test_parse_allof_oneof_anyof_without_names(): schema = { "type": "object", "properties": { "source": { "allOf": [ { "type": "object", "properties": { "prop": { "type": "string", } }, }, { "type": "object", "properties": { "prop": { "type": "integer", }, }, }, ] } }, } assert make_data_type(schema) == ObjectDataType( python_type=None, members=[ DataType( python_type="typing.Union[Source1, Source2]", member_name="source", member_value="None", members=[ ObjectDataType( python_type="Source1", is_recursive=True, ), ObjectDataType( python_type="Source2", is_recursive=True, ), ], is_optional_type=True, ) ], )
def test_parse_complex_object_read_only(): assert make_data_type( { "x-name": "Obj1", "type": "object", "properties": { "field1": {"type": "integer", "readOnly": True}, "field2": {"type": "string", "default": "some-value"}, }, }, for_writes=True, ) == ObjectDataType( python_type="Obj1", members=[ DataType(python_type="str", member_name="field2", member_value="'some-value'"), ], )
def test_parse_additional_properties(): false_properties = make_data_type({"type": "object", "additionalProperties": False}) true_properties = make_data_type({"type": "object", "additionalProperties": True}) assert false_properties == DataType(python_type="typing.Dict") assert true_properties == DataType(python_type="typing.Dict")
def test_parse_primitive_types(): assert make_data_type({"type": "integer"}) == DataType(python_type="int") assert make_data_type({"type": "number"}) == DataType(python_type="float") assert make_data_type({"type": "boolean"}) == DataType(python_type="bool") assert make_data_type({"type": "null"}) == DataType(python_type="None")
def test_parse_missing_schema(): data_type = make_data_type({"description": "string"}) assert data_type == DataType(python_type="typing.Any")
def test_parse_empty_schema(): data_type = make_data_type({}) assert data_type == DataType(python_type="None")
def test_parse_enum(): data_type = make_data_type({"enum": ["one", "two"]}) assert data_type == DataType(python_type="str")