Compare commits

5 Commits

Author SHA1 Message Date
Georg K
1dd98d2752 feat: add support for definition in query param 2022-08-04 23:01:04 +03:00
Georg K
207204fe53 feat: allow to specify custom jinja template 2022-07-28 03:25:37 +03:00
Georg K
7618066b7f feat: use swagger-4-ui-bundle (https://github.com/bartsanchez/swagger_ui_bundle) 2022-07-28 03:15:26 +03:00
Georg K
554e76ce51 fix: git tag match package version 2022-07-28 02:29:42 +03:00
Georg K
2c51e9d929 fix: sed doublequoted to env var subst 2022-07-28 02:26:05 +03:00
7 changed files with 76 additions and 41 deletions

View File

@@ -5,8 +5,7 @@ publish-pypi:
stage: package
image: python:3.10
script:
- sed -i -e 's/1.12.1/${CI_COMMIT_TAG}/g' aiohttp_pydantic/__init__.py
- cat aiohttp_pydantic/__init__.py
- sed -i -e "s/1.12.1/${CI_COMMIT_TAG:1}/g" aiohttp_pydantic/__init__.py
- pip install -U setuptools wheel pip; pip install invoke
- invoke upload --pypi-user ${PYPI_REPO_USER} --pypi-password ${PYPI_REPO_PASSWORD} --pypi-url ${PYPI_REPO_URL}
only:

View File

@@ -14,12 +14,13 @@ def setup(
url_prefix: str = "/oas",
enable: bool = True,
version_spec: Optional[str] = None,
title_spec: Optional[str] = None
title_spec: Optional[str] = None,
custom_template: Optional[jinja2.Template] = None
):
if enable:
oas_app = web.Application()
oas_app["apps to expose"] = tuple(apps_to_expose) or (app,)
oas_app["index template"] = jinja2.Template(
oas_app["index template"] = custom_template or jinja2.Template(
resources.read_text("aiohttp_pydantic.oas", "index.j2")
)
oas_app["version_spec"] = version_spec

View File

@@ -1,45 +1,27 @@
{# This updated file is part of swagger_ui_bundle (https://github.com/dtkav/swagger_ui_bundle) #}
{# This updated file is part of swagger_ui_bundle (https://github.com/bartsanchez/swagger_ui_bundle) #}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{{ title | default('Swagger UI') }}</title>
<link rel="stylesheet" type="text/css" href="{{ static_url | trim('/') }}/swagger-ui.css" >
<link rel="stylesheet" type="text/css" href="{{ static_url | trim('/') }}/swagger-ui.css" />
<link rel="stylesheet" type="text/css" href="{{ static_url | trim('/') }}/index.css" />
<link rel="icon" type="image/png" href="{{ static_url | trim('/') }}/favicon-32x32.png" sizes="32x32" />
<link rel="icon" type="image/png" href="{{ static_url | trim('/') }}/favicon-16x16.png" sizes="16x16" />
<style>
html
{
box-sizing: border-box;
overflow: -moz-scrollbars-vertical;
overflow-y: scroll;
}
*,
*:before,
*:after
{
box-sizing: inherit;
}
body
{
margin:0;
background: #fafafa;
}
</style>
</head>
<body>
<div id="swagger-ui"></div>
<script src="{{ static_url | trim('/') }}/swagger-ui-bundle.js"> </script>
<script src="{{ static_url | trim('/') }}/swagger-ui-standalone-preset.js"> </script>
<script src="{{ static_url | trim('/') }}/swagger-ui-bundle.js" charset="UTF-8"> </script>
<script src="{{ static_url | trim('/') }}/swagger-ui-standalone-preset.js" charset="UTF-8"> </script>
<script>
window.onload = function() {
// Begin Swagger UI call region
const ui = SwaggerUIBundle({
url: "{{ openapi_spec_url }}",
{% if urls is defined %}
urls: {{ urls|tojson|safe }},
{% endif %}
validatorUrl: {{ validatorUrl | default('null') }},
{% if configUrl is defined %}
configUrl: "{{ configUrl }}",
@@ -54,16 +36,15 @@
SwaggerUIBundle.plugins.DownloadUrl
],
layout: "StandaloneLayout"
})
});
{% if initOAuth is defined %}
ui.initOAuth(
{{ initOAuth|tojson|safe }}
)
{% endif %}
// End Swagger UI call region
window.ui = ui
}
window.ui = ui;
};
</script>
</body>
</html>

View File

@@ -126,6 +126,10 @@ def _add_http_method_to_oas(
ref_template="#/components/schemas/{model}"
)
# move definitions
if def_sub_schemas := oas_operation.parameters[i].schema.pop("definitions", None):
oas.components.schemas.update(def_sub_schemas)
return_type = get_type_hints(handler).get("return")
if return_type is not None:
_OASResponseBuilder(oas, oas_operation, status_code_descriptions).build(

View File

@@ -32,7 +32,7 @@ python_requires = >=3.8
install_requires =
aiohttp
pydantic>=1.7
swagger-ui-bundle
swagger-4-ui-bundle
[options.extras_require]
test =

View File

@@ -20,6 +20,11 @@ class Color(str, Enum):
PINK = "pink"
class Lang(str, Enum):
EN = 'en'
FR = 'fr'
class Toy(BaseModel):
name: str
color: Color
@@ -33,7 +38,7 @@ class Pet(BaseModel):
class PetCollectionView(PydanticView):
async def get(
self, format: str, name: Optional[str] = None, *, promo: Optional[UUID] = None
self, format: str, lang: Lang = Lang.EN, name: Optional[str] = None, *, promo: Optional[UUID] = None
) -> r200[List[Pet]]:
"""
Get a list of pets
@@ -51,11 +56,11 @@ class PetCollectionView(PydanticView):
class PetItemView(PydanticView):
async def get(
self,
id: int,
/,
size: Union[int, Literal["x", "l", "s"]],
day: Union[int, Literal["now"]] = "now",
self,
id: int,
/,
size: Union[int, Literal["x", "l", "s"]],
day: Union[int, Literal["now"]] = "now",
) -> Union[r200[Pet], r404]:
return web.json_response()
@@ -116,6 +121,12 @@ async def test_generated_oas_should_have_components_schemas(generated_oas):
"title": "Color",
"type": "string",
},
'Lang': {
'description': 'An enumeration.',
'enum': ['en', 'fr'],
'title': 'Lang',
'type': 'string'
},
"Toy": {
"properties": {
"color": {"$ref": "#/components/schemas/Color"},
@@ -143,6 +154,16 @@ async def test_pets_route_should_have_get_method(generated_oas):
"required": True,
"schema": {"title": "format", "type": "string"},
},
{
'in': 'query',
'name': 'lang',
'required': False,
'schema': {
'allOf': [{'$ref': '#/components/schemas/Lang'}],
'default': 'en',
'title': 'lang'
}
},
{
"in": "query",
"name": "name",

View File

@@ -1,5 +1,6 @@
from __future__ import annotations
from enum import Enum
from typing import Optional, List
from pydantic import Field
from aiohttp import web
@@ -54,6 +55,20 @@ class ArticleViewWithPaginationGroup(PydanticView):
)
class Lang(str, Enum):
EN = 'en'
FR = 'fr'
class ArticleViewWithEnumInQuery(PydanticView):
async def get(self, lang: Lang):
return web.json_response(
{
"lang": lang
}
)
async def test_get_article_without_required_qs_should_return_an_error_message(
aiohttp_client, event_loop
):
@@ -236,6 +251,20 @@ async def test_get_article_with_page_and_page_size(aiohttp_client, event_loop):
assert resp.content_type == "application/json"
async def test_get_article_with_enum_in_query(aiohttp_client, event_loop):
app = web.Application()
app.router.add_view("/article", ArticleViewWithEnumInQuery)
client = await aiohttp_client(app)
resp = await client.get(
"/article", params={"lang": Lang.EN.value}
)
assert await resp.json() == {'lang': Lang.EN}
assert resp.status == 200
assert resp.content_type == "application/json"
async def test_get_article_with_page_and_wrong_page_size(aiohttp_client, event_loop):
app = web.Application()
app.router.add_view("/article", ArticleViewWithPaginationGroup)