def test_openapi_can_render_fields_with_different_request_and_response_names(): # Given that I have a schema that has different names based on whether it's in the request or response @schema class A: x: int = field(request_name="X", response_name="Y") def index(a: A) -> A: pass # And an app app = App(routes=[Route("/", index)]) # When I generate a document document = generate_openapi_document( app, Metadata("example", "an example", "0.0.0"), []) # Then the schema should mark that field as writeOnly response_schema = document["components"]["schemas"][ "tests.openapi.test_openapi.A"] assert response_schema["properties"] == { "X": { "type": "integer", "format": "int64", "writeOnly": True, }, "Y": { "type": "integer", "format": "int64", "readOnly": True, }, }
def test_openapi_can_render_request_only_fields(): # Given that I have a schema that has request-only fields @schema class A: x: int = field(request_only=True) def index(a: A) -> A: pass # And an app app = App(routes=[Route("/", index)]) # When I generate a document document = generate_openapi_document( app, Metadata("example", "an example", "0.0.0"), []) # Then the schema should mark that field as writeOnly response_schema = document["components"]["schemas"][ "tests.openapi.test_openapi.A"] assert response_schema["properties"] == { "x": { "type": "integer", "format": "int64", "writeOnly": True, }, }
def test_openapi_can_render_api_key_security_schemes_correctly(): # Given that I have an APIKeySecurityScheme security_scheme = APIKeySecurityScheme( name="api-key", param_name="x-api-key", in_="header", ) # When I generate a document with that security scheme document = generate_openapi_document( App(), Metadata( "example", "an example", "0.0.0", ), security_schemes=[security_scheme], default_security_scheme="api-key", ) # Then I should get back a valid, non-ambiguous, OpenAPI document assert document["components"] == { "schemas": {}, "securitySchemes": { "api-key": { "name": "x-api-key", "in": "header", "type": "apiKey", }, }, }
def test_openapi_can_render_documents_with_method_handlers(): # Given that I have a resource class @schema class User: username: str class Users: def get_users(self) -> User: pass # And an app that uses that an instance of that resource users = Users() app = App(routes=[Route("/users", users.get_users)]) # When I generate a document document = generate_openapi_document( app, Metadata("example", "an example", "0.0.0"), []) # Then I should get back a valid document assert document
def test_openapi_can_render_lists_of_x(fields, expected): # Given that I have a schema that has a list of something in it A = type("A", (object, ), fields) A.__annotations__ = fields A = schema(A) def index() -> A: pass # And an app app = App(routes=[Route("/", index)]) # When I generate a document document = generate_openapi_document( app, Metadata("example", "an example", "0.0.0"), []) # Then the return schema should have an array of that thing response_schema = document["components"]["schemas"][ "tests.openapi.test_openapi.A"] assert response_schema["properties"] == expected
def test_openapi_can_render_documents_with_union_types(): # Given that I have a schema that uses union types @schema class A: x: int @schema class B: x: str @schema class C: x: Union[A, B, int] def index() -> C: pass # And an app that uses that an instance of that resource app = App(routes=[Route("/", index)]) # When I generate a document document = generate_openapi_document( app, Metadata("example", "an example", "0.0.0"), []) # Then I should get back a valid document assert document["components"]["schemas"] == { "tests.openapi.test_openapi.A": { "type": "object", "required": ["x"], "properties": { "x": { "type": "integer", "format": "int64" }, }, }, "tests.openapi.test_openapi.B": { "type": "object", "required": ["x"], "properties": { "x": { "type": "string" }, }, }, "tests.openapi.test_openapi.C": { "type": "object", "required": ["x"], "properties": { "x": { "anyOf": [ { "$ref": "#/components/schemas/tests.openapi.test_openapi.A" }, { "$ref": "#/components/schemas/tests.openapi.test_openapi.B" }, { "type": "integer", "format": "int64" }, ], }, } } }