* Added a wrapper for get_oas to throw spec info (#12) * Added tests generate_oas * Moved params to Application Co-authored-by: Спиненко Иван ispinenko@ussc.ru <ispinenko@ussc.ru>
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
from importlib import resources
|
||||
from typing import Iterable
|
||||
from typing import Iterable, Optional
|
||||
|
||||
import jinja2
|
||||
from aiohttp import web
|
||||
@@ -13,6 +13,8 @@ def setup(
|
||||
apps_to_expose: Iterable[web.Application] = (),
|
||||
url_prefix: str = "/oas",
|
||||
enable: bool = True,
|
||||
version_spec: Optional[str] = None,
|
||||
title_spec: Optional[str] = None,
|
||||
):
|
||||
if enable:
|
||||
oas_app = web.Application()
|
||||
@@ -20,6 +22,9 @@ def setup(
|
||||
oas_app["index template"] = jinja2.Template(
|
||||
resources.read_text("aiohttp_pydantic.oas", "index.j2")
|
||||
)
|
||||
oas_app["version_spec"] = version_spec
|
||||
oas_app["title_spec"] = title_spec
|
||||
|
||||
oas_app.router.add_get("/spec", get_oas, name="spec")
|
||||
oas_app.router.add_static("/static", swagger_ui_path, name="static")
|
||||
oas_app.router.add_get("", oas_ui, name="index")
|
||||
|
||||
@@ -305,7 +305,7 @@ class Components:
|
||||
|
||||
class OpenApiSpec3:
|
||||
def __init__(self):
|
||||
self._spec = {"openapi": "3.0.0"}
|
||||
self._spec = {"openapi": "3.0.0", "info": {"version": "1.0.0", "title": "Aiohttp pydantic application"}}
|
||||
|
||||
@property
|
||||
def info(self) -> Info:
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import typing
|
||||
from inspect import getdoc
|
||||
from itertools import count
|
||||
from typing import List, Type
|
||||
from typing import List, Type, Optional, Dict
|
||||
|
||||
from aiohttp.web import Response, json_response
|
||||
from aiohttp.web_app import Application
|
||||
@@ -147,11 +147,18 @@ def _add_http_method_to_oas(
|
||||
)
|
||||
|
||||
|
||||
def generate_oas(apps: List[Application]) -> dict:
|
||||
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:
|
||||
@@ -175,7 +182,9 @@ async def get_oas(request):
|
||||
View to generate the Open Api Specification from PydanticView in application.
|
||||
"""
|
||||
apps = request.app["apps to expose"]
|
||||
return json_response(generate_oas(apps))
|
||||
version_spec = request.app["version_spec"]
|
||||
title_spec = request.app["title_spec"]
|
||||
return json_response(generate_oas(apps, version_spec, title_spec))
|
||||
|
||||
|
||||
async def oas_ui(request):
|
||||
|
||||
Reference in New Issue
Block a user