def test_openapi_parser_parse_enum_models(): parser = OpenAPIParser(Path(DATA_PATH / 'enum_models.yaml').read_text(), ) expected_dir = EXPECTED_OPEN_API_PATH / 'openapi_parser_parse_enum_models' assert parser.parse() == (expected_dir / 'output_py37.py').read_text() parser = OpenAPIParser( Path(DATA_PATH / 'enum_models.yaml').read_text(), target_python_version=PythonVersion.PY_36, ) assert parser.parse() == (expected_dir / 'output_py36.py').read_text()
def test_openapi_parser_parse_x_enum_varnames(): parser = OpenAPIParser( Path(DATA_PATH / 'x_enum_varnames.yaml'), ) print(parser.parse()) assert ( parser.parse() == '''from __future__ import annotations from enum import Enum class String(Enum): dog = 'dog' cat = 'cat' snake = 'snake' class UnknownTypeString(Enum): dog = 'dog' cat = 'cat' snake = 'snake' class NamedString(Enum): EQ = '=' NE = '!=' GT = '>' LT = '<' GE = '>=' LE = '<=' class NamedNumber(Enum): one = 1 two = 2 three = 3 class Number(Enum): number_1 = 1 number_2 = 2 number_3 = 3 class UnknownTypeNumber(Enum): int_1 = 1 int_2 = 2 int_3 = 3 ''' )
def generate_model_from_schema(schema: str) -> str: """ Given an OpenAPI schema, generate pydantic models from everything defined in the "components/schemas" section :param schema: Content of an OpenAPI spec, plain text :return: Importable python code with generated models """ parser = OpenAPIParser( BaseModel, CustomRootType, DataModelField, base_class="pydantic.BaseModel", custom_template_dir=None, extra_template_data=None, target_python_version=PythonVersion.PY_37, text=schema, dump_resolve_reference_action=None, validation=True, field_constraints=False, snake_case_field=False, strip_default_none=False, aliases=None, ) result = parser.parse() return str(result)
def test_openapi_parser_parse_resolved_models(): parser = OpenAPIParser( BaseModel, CustomRootType, text=Path(DATA_PATH / 'resolved_models.yaml').read_text(), ) assert (parser.parse() == '''from __future__ import annotations from typing import List, Optional from pydantic import BaseModel class Pet(BaseModel): id: int name: str tag: Optional[str] = None class Pets(BaseModel): __root__: List[Pet] class Error(BaseModel): code: int message: str class Resolved(BaseModel): resolved: Optional[List[str]] = None ''')
def test_openapi_parser_parse_allof(): parser = OpenAPIParser(BaseModel, CustomRootType, text=Path(DATA_PATH / 'allof.yaml').read_text()) assert (parser.parse() == (EXPECTED_OPEN_API_PATH / 'openapi_parser_parse_allof' / 'output.py').read_text())
def test_openapi_parser_parse_array_enum(with_import, format_, base_class): parser = OpenAPIParser(source=Path(DATA_PATH / 'array_enum.yaml'), base_class=base_class) expected_file = get_expected_file('openapi_parser_parse_array_enum', with_import, format_, base_class) assert (parser.parse(with_import=with_import, format_=format_) == expected_file.read_text())
def test_openapi_parser_parse_nested_anyof(): parser = OpenAPIParser( BaseModel, CustomRootType, data_model_field_type=DataModelField, text=Path(DATA_PATH / 'nested_anyof.yaml').read_text(), ) assert ( parser.parse() == '''from __future__ import annotations from typing import List, Optional, Union from pydantic import BaseModel class Type1(BaseModel): prop: Optional[str] = None class Type2(BaseModel): prop: Optional[str] = None class Container(BaseModel): contents: List[Union[Type1, Type2]] ''' )
def test_openapi_parser_parse(with_import, format_, base_class, result): parser = OpenAPIParser( BaseModel, CustomRootType, filename=str(DATA_PATH / 'api.yaml'), base_class=base_class, ) assert parser.parse(with_import=with_import, format_=format_) == result
def test_openapi_parser_parse_allof(): parser = OpenAPIParser(Path(DATA_PATH / 'allof.yaml'),) assert ( parser.parse() == ( EXPECTED_OPEN_API_PATH / 'openapi_parser_parse_allof' / 'output.py' ).read_text() )
def test_openapi_parser_parse_any(): parser = OpenAPIParser( data_model_field_type=DataModelFieldBase, source=Path(DATA_PATH / 'any.yaml'), ) assert (parser.parse() == (EXPECTED_OPEN_API_PATH / 'openapi_parser_parse_any' / 'output.py').read_text())
def test_openapi_parser_parse_alias(): parser = OpenAPIParser(Path(DATA_PATH / 'alias.yaml'), ) results = {'/'.join(p): r for p, r in parser.parse().items()} openapi_parser_parse_alias_dir = (EXPECTED_OPEN_API_PATH / 'openapi_parser_parse_alias') for path in openapi_parser_parse_alias_dir.rglob('*.py'): key = str(path.relative_to(openapi_parser_parse_alias_dir)) assert results.pop(key).body == path.read_text()
def test_openapi_parser_parse(with_import, format_, base_class, result): parser = OpenAPIParser( BaseModel, CustomRootType, text=Path(DATA_PATH / 'api.yaml').read_text(), base_class=base_class, ) assert parser.parse(with_import=with_import, format_=format_) == result
def test_openapi_parser_parse_allof(): parser = OpenAPIParser(BaseModel, CustomRootType, filename=str(DATA_PATH / 'allof.yaml')) assert (parser.parse() == """from __future__ import annotations from typing import List, Optional from pydantic import BaseModel class Pet(BaseModel): id: int name: str tag: Optional[str] = None class Car(BaseModel): number: str class AllOfref(Pet, Car): pass class AllOfobj(BaseModel): name: Optional[str] = None number: Optional[str] = None class AnyOfCombine(Pet, Car): age: Optional[str] = None class item(Pet, Car): age: Optional[str] = None class AnyOfCombineInObject(BaseModel): item: Optional[item] = None class AnyOfCombineInArrayItem(Pet, Car): age: Optional[str] = None class AnyOfCombineInArray(BaseModel): __root__: List[AnyOfCombineInArrayItem] class AnyOfCombineInRoot(Pet, Car): age: Optional[str] = None class Error(BaseModel): code: int message: str """)
def test_openapi_parser_parse_resolved_models(): parser = OpenAPIParser( BaseModel, CustomRootType, text=Path(DATA_PATH / 'resolved_models.yaml').read_text(), ) assert (parser.parse() == (EXPECTED_OPEN_API_PATH / 'openapi_parser_parse_resolved_models' / 'output.py').read_text())
def test_openapi_parser_parse_anyof(): parser = OpenAPIParser( BaseModel, CustomRootType, text=Path(DATA_PATH / 'anyof.yaml').read_text() ) assert ( parser.parse() == '''from __future__ import annotations from datetime import date from typing import List, Optional, Union from pydantic import BaseModel class Pet(BaseModel): id: int name: str tag: Optional[str] = None class Car(BaseModel): id: int name: str tag: Optional[str] = None class AnyOfItemItem(BaseModel): name: Optional[str] = None class AnyOfItem(BaseModel): __root__: Union[Pet, Car, AnyOfItemItem] class itemItem(BaseModel): name: Optional[str] = None class AnyOfobj(BaseModel): item: Optional[Union[Pet, Car, itemItem]] = None class AnyOfArrayItem(BaseModel): name: Optional[str] = None birthday: Optional[date] = None class AnyOfArray(BaseModel): __root__: List[Union[Pet, Car, AnyOfArrayItem]] class Error(BaseModel): code: int message: str ''' )
def test_openapi_parser_parse_nested_anyof(): parser = OpenAPIParser( BaseModel, CustomRootType, data_model_field_type=DataModelField, text=Path(DATA_PATH / 'nested_anyof.yaml').read_text(), ) assert (parser.parse() == (EXPECTED_OPEN_API_PATH / 'openapi_parser_parse_nested_anyof' / 'output.py').read_text())
def test_openapi_parser_parse_duplicate_models(): parser = OpenAPIParser(Path(DATA_PATH / 'duplicate_models.yaml'),) assert ( parser.parse() == ( EXPECTED_OPEN_API_PATH / 'openapi_parser_parse_duplicate_models' / 'output.py' ).read_text() )
def test_openapi_parser_parse_remote_ref(with_import, format_, base_class): parser = OpenAPIParser( data_model_field_type=DataModelFieldBase, base_class=base_class, source=(DATA_PATH / 'refs.yaml').read_text(), ) expected_file = get_expected_file('openapi_parser_parse_remote_ref', with_import, format_, base_class) assert (parser.parse(with_import=with_import, format_=format_) == expected_file.read_text())
def test_openapi_parser_parse_array_enum(with_import, format_, base_class): parser = OpenAPIParser( BaseModel, CustomRootType, text=Path(DATA_PATH / 'array_enum.yaml').read_text(), base_class=base_class, ) expected_file = get_expected_file('openapi_parser_parse_array_enum', with_import, format_, base_class) assert (parser.parse(with_import=with_import, format_=format_) == expected_file.read_text())
def test_openapi_parser_parse_alias(): parser = OpenAPIParser(Path(DATA_PATH / 'alias.yaml'), ) if platform.system() == 'Windows': delimiter = '\\' else: delimiter = '/' results = {delimiter.join(p): r for p, r in parser.parse().items()} openapi_parser_parse_alias_dir = (EXPECTED_OPEN_API_PATH / 'openapi_parser_parse_alias') for path in openapi_parser_parse_alias_dir.rglob('*.py'): key = str(path.relative_to(openapi_parser_parse_alias_dir)) assert results.pop(key).body == path.read_text()
def test_openapi_parser_parse_modular(with_import, format_, base_class): parser = OpenAPIParser( Path(DATA_PATH / 'modular.yaml'), base_class=base_class, data_model_field_type=DataModelFieldBase, ) modules = parser.parse(with_import=with_import, format_=format_) main_modular_dir = EXPECTED_OPEN_API_PATH / 'openapi_parser_parse_modular' for paths, result in modules.items(): expected = main_modular_dir.joinpath(*paths).read_text() assert result.body == expected
def test_openapi_parser_parse(with_import, format_, base_class): parser = OpenAPIParser( data_model_field_type=DataModelFieldBase, source=Path(DATA_PATH / 'api.yaml'), base_class=base_class, ) expected_file = get_expected_file('openapi_parser_parse', with_import, format_, base_class) assert (parser.parse( with_import=with_import, format_=format_, settings_path=DATA_PATH.parent) == expected_file.read_text())
def test_openapi_parser_parse_remote_ref(with_import, format_, base_class): parser = OpenAPIParser( BaseModel, CustomRootType, base_class=base_class, text=(DATA_PATH / 'refs.yaml').read_text(), ) expected_file = get_expected_file('openapi_parser_parse_remote_ref', with_import, format_, base_class) assert (parser.parse(with_import=with_import, format_=format_) == expected_file.read_text())
def test_openapi_parser_parse_modular(with_import, format_, base_class): parser = OpenAPIParser( BaseModel, CustomRootType, text=Path(DATA_PATH / 'modular.yaml').read_text(), base_class=base_class, ) modules = parser.parse(with_import=with_import, format_=format_) main_modular_dir = EXPECTED_OPEN_API_PATH / 'openapi_parser_parse_modular' for paths, result in modules.items(): expected = main_modular_dir.joinpath(*paths).read_text() assert result == expected
def test_openapi_parser_parse_alias(): parser = OpenAPIParser( BaseModel, CustomRootType, data_model_field_type=DataModelField, text=Path(DATA_PATH / 'alias.yaml').read_text(), ) results = {'/'.join(p): r for p, r in parser.parse().items()} openapi_parser_parse_alias_dir = (EXPECTED_OPEN_API_PATH / 'openapi_parser_parse_alias') for path in openapi_parser_parse_alias_dir.rglob('*.py'): key = str(path.relative_to(openapi_parser_parse_alias_dir)) assert results.pop(key) == path.read_text()
def test_openapi_parser_parse_anyof(): parser = OpenAPIParser(BaseModel, CustomRootType, filename=str(DATA_PATH / 'anyof.yaml')) assert (parser.parse() == """from __future__ import annotations from typing import List, Optional, Union from pydantic import BaseModel class Pet(BaseModel): id: int name: str tag: Optional[str] = None class Car(BaseModel): id: int name: str tag: Optional[str] = None class AnyOfItemItem(BaseModel): name: Optional[str] = None class AnyOfItem(BaseModel): __root__: Union[Pet, Car, AnyOfItemItem] class itemItem(BaseModel): name: Optional[str] = None class AnyOfobj(BaseModel): item: Optional[Union[Pet, Car, itemItem]] = None class AnyOfArrayItem(BaseModel): name: Optional[str] = None class AnyOfArray(BaseModel): __root__: List[Union[Pet, Car, AnyOfArrayItem]] class Error(BaseModel): code: int message: str """)
def test_openapi_parser_parse_additional_properties(with_import, format_, base_class): parser = OpenAPIParser( Path(DATA_PATH / 'additional_properties.yaml').read_text(), base_class=base_class, data_model_field_type=DataModelFieldBase, ) assert (parser.parse(with_import=with_import, format_=format_, settings_path=DATA_PATH.parent) == get_expected_file( 'openapi_parser_parse_additional_properties', with_import, format_, base_class, ).read_text())
def test_openapi_parser_parse_additional_properties(with_import, format_, base_class): parser = OpenAPIParser( BaseModel, CustomRootType, text=Path(DATA_PATH / 'additional_properties.yaml').read_text(), base_class=base_class, ) assert (parser.parse(with_import=with_import, format_=format_) == get_expected_file( 'openapi_parser_parse_additional_properties', with_import, format_, base_class, ).read_text())
def test_openapi_parser_parse_lazy_resolved_models(): parser = OpenAPIParser( Path(DATA_PATH / 'lazy_resolved_models.yaml'), ) assert ( parser.parse() == '''from __future__ import annotations from typing import List, Optional from pydantic import BaseModel class Pet(BaseModel): id: int name: str tag: Optional[str] = None class Pets(BaseModel): __root__: List[Pet] class Error(BaseModel): code: int message: str class Event(BaseModel): name: Optional[str] = None event: Optional[Event] = None class Events(BaseModel): __root__: List[Event] class Results(BaseModel): envets: Optional[List[Events]] = None event: Optional[List[Event]] = None ''' )
def test_openapi_parser_parse_alias(): parser = OpenAPIParser(BaseModel, CustomRootType, text=Path(DATA_PATH / 'alias.yaml').read_text()) assert (parser.parse() == '''from __future__ import annotations from enum import Enum from pydantic import BaseModel class Pet(Enum): ca_t = 'ca-t' dog_ = 'dog*' class Error(BaseModel): code: int message: str ''')