def generate_oas(apps: List[Application], version_spec: Optional[str] = None, title_spec: Optional[str] = None) -> dict: """ Generate and return Open Api Specification from PydanticView in application. """ oas = OpenApiSpec3() if version_spec is not None: oas.info.version = version_spec if title_spec is not None: oas.info.title = title_spec for app in apps: for resources in app.router.resources(): for resource_route in resources: if not is_pydantic_view(resource_route.handler): continue view: Type[PydanticView] = resource_route.handler info = resource_route.get_info() path = oas.paths[info.get("path", info.get("formatter"))] if resource_route.method == "*": for method_name in view.allowed_methods: _add_http_method_to_oas(oas, path, method_name, view) else: _add_http_method_to_oas(oas, path, resource_route.method, view) return oas.spec
def test_paths_operation_parameters(): oas = OpenApiSpec3() operation = oas.paths["/users/{petId}"].get parameter = operation.parameters[0] parameter.name = "petId" parameter.description = "ID of pet that needs to be updated" parameter.in_ = "path" parameter.required = True assert oas.spec == { "openapi": "3.0.0", "info": { "title": "Aiohttp pydantic application", "version": "1.0.0" }, "paths": { "/users/{petId}": { "get": { "parameters": [{ "description": "ID of pet that needs to be updated", "in": "path", "name": "petId", "required": True, }] } } }, }
def test_paths_operation_tags(): oas = OpenApiSpec3() operation = oas.paths["/users/{petId}"].get assert operation.tags == [] operation.tags = ["pets"] assert oas.spec["paths"]["/users/{petId}"] == {"get": {"tags": ["pets"]}} operation.tags = [] assert oas.spec["paths"]["/users/{petId}"] == {"get": {}}
def test_paths_get(): oas = OpenApiSpec3() oas.paths["/users/{id}"].get assert oas.spec == { "openapi": "3.0.0", "paths": { "/users/{id}": { "get": {} } } }
def test_paths_description(): oas = OpenApiSpec3() oas.paths["/users/{id}"].description = "This route ..." assert oas.spec == { "openapi": "3.0.0", "paths": { "/users/{id}": { "description": "This route ..." } }, }
def test_info_terms_of_service(): oas = OpenApiSpec3() assert oas.info.terms_of_service is None oas.info.terms_of_service = "http://example.com/terms/" assert oas.info.terms_of_service == "http://example.com/terms/" assert oas.spec == { "info": { "termsOfService": "http://example.com/terms/" }, "openapi": "3.0.0", }
def test_info_description(): oas = OpenApiSpec3() assert oas.info.description is None oas.info.description = "info description" assert oas.info.description == "info description" assert oas.spec == { "info": { "description": "info description" }, "openapi": "3.0.0" }
def test_sever_url(): oas = OpenApiSpec3() oas.servers[0].url = "https://development.gigantic-server.com/v1" oas.servers[1].url = "https://development.gigantic-server.com/v2" assert oas.spec == { "openapi": "3.0.0", "info": {"title": "Aiohttp pydantic application", "version": "1.0.0"}, "servers": [ {"url": "https://development.gigantic-server.com/v1"}, {"url": "https://development.gigantic-server.com/v2"}, ], }
def test_info_version(): oas = OpenApiSpec3() assert oas.info.version == "1.0.0" oas.info.version = "3.14" assert oas.info.version == "3.14" assert oas.spec == { "info": { "version": "3.14", "title": "Aiohttp pydantic application" }, "openapi": "3.0.0", }
def test_info_title(): oas = OpenApiSpec3() assert oas.info.title == "Aiohttp pydantic application" oas.info.title = "Info Title" assert oas.info.title == "Info Title" assert oas.spec == { "info": { "title": "Info Title", "version": "1.0.0", }, "openapi": "3.0.0", }
def test_sever_description(): oas = OpenApiSpec3() oas.servers[0].url = "https://development.gigantic-server.com/v1" oas.servers[0].description = "Development server" assert oas.spec == { "openapi": "3.0.0", "servers": [{ "url": "https://development.gigantic-server.com/v1", "description": "Development server", }], }
def test_info_description(): oas = OpenApiSpec3() assert oas.info.description is None oas.info.description = "info description" assert oas.info.description == "info description" assert oas.spec == { "info": { "description": "info description", "title": "Aiohttp pydantic application", "version": "1.0.0", }, "openapi": "3.0.0", }
def test_info_license(): oas = OpenApiSpec3() oas.info.license.name = "Apache 2.0" oas.info.license.url = "https://www.apache.org/licenses/LICENSE-2.0.html" assert oas.spec == { "info": { "license": { "name": "Apache 2.0", "url": "https://www.apache.org/licenses/LICENSE-2.0.html", } }, "openapi": "3.0.0", }
def test_info_terms_of_service(): oas = OpenApiSpec3() assert oas.info.terms_of_service is None oas.info.terms_of_service = "http://example.com/terms/" assert oas.info.terms_of_service == "http://example.com/terms/" assert oas.spec == { "info": { "title": "Aiohttp pydantic application", "version": "1.0.0", "termsOfService": "http://example.com/terms/", }, "openapi": "3.0.0", }
def test_paths_operation_summary(): oas = OpenApiSpec3() operation = oas.paths["/users/{id}"].get operation.summary = "Updates a pet in the store with form data" assert oas.spec == { "openapi": "3.0.0", "paths": { "/users/{id}": { "get": { "summary": "Updates a pet in the store with form data" } } }, }
def test_paths_operation_description(): oas = OpenApiSpec3() operation = oas.paths["/users/{id}"].get operation.description = "Long descriptions ..." assert oas.spec == { "openapi": "3.0.0", "paths": { "/users/{id}": { "get": { "description": "Long descriptions ..." } } }, }
def test_paths_operation_responses(): oas = OpenApiSpec3() response = oas.paths["/users/{petId}"].get.responses[200] response.description = "A complex object array response" response.content = { "application/json": { "schema": { "type": "array", "items": { "$ref": "#/components/schemas/VeryComplexType" }, } } }
def test_sever_description(): oas = OpenApiSpec3() oas.servers[0].url = "https://development.gigantic-server.com/v1" oas.servers[0].description = "Development server" assert oas.spec == { "openapi": "3.0.0", "info": {"title": "Aiohttp pydantic application", "version": "1.0.0"}, "servers": [ { "url": "https://development.gigantic-server.com/v1", "description": "Development server", } ], }
def test_paths_operation_requestBody(): oas = OpenApiSpec3() request_body = oas.paths["/users/{petId}"].get.request_body request_body.description = "user to add to the system" request_body.content = { "application/json": { "schema": { "$ref": "#/components/schemas/User" }, "examples": { "user": { "summary": "User Example", "externalValue": "http://foo.bar/examples/user-example.json", } }, } } request_body.required = True assert oas.spec == { "openapi": "3.0.0", "info": { "title": "Aiohttp pydantic application", "version": "1.0.0" }, "paths": { "/users/{petId}": { "get": { "requestBody": { "content": { "application/json": { "examples": { "user": { "externalValue": "http://foo.bar/examples/user-example.json", "summary": "User Example", } }, "schema": { "$ref": "#/components/schemas/User" }, } }, "description": "user to add to the system", "required": True, } } } }, }
def test_paths_description(): oas = OpenApiSpec3() oas.paths["/users/{id}"].description = "This route ..." assert oas.spec == { "openapi": "3.0.0", "info": { "title": "Aiohttp pydantic application", "version": "1.0.0" }, "paths": { "/users/{id}": { "description": "This route ..." } }, }
def test_paths_get(): oas = OpenApiSpec3() oas.paths["/users/{id}"].get assert oas.spec == { "openapi": "3.0.0", "info": { "title": "Aiohttp pydantic application", "version": "1.0.0" }, "paths": { "/users/{id}": { "get": {} } }, }
def test_sever_url(): oas = OpenApiSpec3() oas.servers[0].url = "https://development.gigantic-server.com/v1" oas.servers[1].url = "https://development.gigantic-server.com/v2" assert oas.spec == { "openapi": "3.0.0", "servers": [ { "url": "https://development.gigantic-server.com/v1" }, { "url": "https://development.gigantic-server.com/v2" }, ], }
def test_paths_operation_summary(): oas = OpenApiSpec3() operation = oas.paths["/users/{id}"].get operation.summary = "Updates a pet in the store with form data" assert oas.spec == { "openapi": "3.0.0", "info": { "title": "Aiohttp pydantic application", "version": "1.0.0" }, "paths": { "/users/{id}": { "get": { "summary": "Updates a pet in the store with form data" } } }, }
def test_paths_operation_description(): oas = OpenApiSpec3() operation = oas.paths["/users/{id}"].get operation.description = "Long descriptions ..." assert oas.spec == { "openapi": "3.0.0", "info": { "title": "Aiohttp pydantic application", "version": "1.0.0" }, "paths": { "/users/{id}": { "get": { "description": "Long descriptions ..." } } }, }
def test_info_title(): oas = OpenApiSpec3() assert oas.info.title is None oas.info.title = "Info Title" assert oas.info.title == "Info Title" assert oas.spec == {"info": {"title": "Info Title"}, "openapi": "3.0.0"}
def test_sever_variables(): oas = OpenApiSpec3()
def test_info_version(): oas = OpenApiSpec3() assert oas.info.version is None oas.info.version = "3.14" assert oas.info.version == "3.14" assert oas.spec == {"info": {"version": "3.14"}, "openapi": "3.0.0"}