def test_register_schema_for_enum(): docs = OpenAPIHandler(info=Info("Example", "0.0.1")) docs.register_schema_for_type(FooLevel) assert docs.components.schemas is not None assert len(docs.components.schemas) == 1 schema = docs.components.schemas["FooLevel"] assert isinstance(schema, Schema) assert schema is not None assert schema.type == ValueType.INTEGER assert schema.enum == [x.value for x in FooLevel]
def test_register_schema_handles_repeated_calls(): docs = OpenAPIHandler(info=Info("Example", "0.0.1")) docs.register_schema_for_type(Foo) docs.register_schema_for_type(Foo) docs.register_schema_for_type(Foo) assert docs.components.schemas is not None assert len(docs.components.schemas) == 1 foo_schema = docs.components.schemas["Foo"] assert "Foo1" not in docs.components.schemas assert foo_schema is not None assert isinstance(foo_schema, Schema) assert "a" in foo_schema.properties
def on_polymorph_example_docs_created_pydantic( docs: OpenAPIHandler, operation: Operation ) -> None: docs.register_schema_for_type(PetModel) pet_schema = docs.components.schemas["Pet"] assert isinstance(pet_schema, Schema) pet_schema.discriminator = Discriminator("type", {"cat": "CatPet", "dog": "DogPet"}) cat_ref = docs.register_schema_for_type(CatPetModel) dog_ref = docs.register_schema_for_type(DogPetModel) operation.responses["200"] = ResponseDoc( "Polymorph example", content={ "application/json": MediaType(schema=Schema(any_of=[cat_ref, dog_ref])) }, )
def test_handles_forward_refs(): docs = OpenAPIHandler(info=Info("Example", "0.0.1")) @dataclass class Friend: foo: "Foo" docs.register_schema_for_type(Foo) docs.register_schema_for_type(Friend) assert docs.components.schemas is not None assert len(docs.components.schemas) == 2 friend_schema = docs.components.schemas["Friend"] assert friend_schema is not None assert isinstance(friend_schema, Schema) assert friend_schema.properties["foo"] == Reference(ref="#/components/schemas/Foo")
def test_register_schema_can_handle_classes_with_same_name(): docs = OpenAPIHandler(info=Info("Example", "0.0.1")) @dataclass class FooX: x: str FooX.__name__ = "Foo" docs.register_schema_for_type(Foo) docs.register_schema_for_type(FooX) foo_schema = docs.components.schemas["Foo"] foox_schema = docs.components.schemas["Foo1"] assert foo_schema is not None assert foox_schema is not None assert isinstance(foo_schema, Schema) assert isinstance(foox_schema, Schema) assert "x" in foox_schema.properties assert "a" in foo_schema.properties