We can add custom tags to generated OPS

This commit is contained in:
Vincent Maillol
2021-08-22 14:32:19 +02:00
parent cb996860a9
commit fa7e8d914b
9 changed files with 173 additions and 40 deletions

View File

@@ -20,7 +20,6 @@ class ArticleModels(BaseModel):
class ArticleView(PydanticView):
async def post(self, article: ArticleModel):
return web.json_response(article.dict())

View File

@@ -1,5 +1,8 @@
from textwrap import dedent
from aiohttp_pydantic.oas.docstring_parser import (
status_code,
tags,
operation,
_i_extract_block,
LinesIterator,
@@ -13,6 +16,13 @@ def web_handler():
bla bla bla
Tags: tag1, tag2
, tag3,
t a
g
4
Status Codes:
200: line 1
@@ -36,6 +46,19 @@ def web_handler():
"""
def web_handler_2():
"""
bla bla bla
Tags: tag1
Status Codes:
200: line 1
bla bla
"""
def test_lines_iterator():
lines_iterator = LinesIterator("AAAA\nBBBB")
with pytest.raises(StopIteration):
@@ -61,28 +84,72 @@ def test_status_code():
assert status_code(getdoc(web_handler)) == expected
def test_tags():
expected = ["tag1", "tag2", "tag3", "t a g 4"]
assert tags(getdoc(web_handler)) == expected
def test_operation():
expected = "bla bla bla\n\n\nbla bla"
assert operation(getdoc(web_handler)) == expected
assert operation(getdoc(web_handler_2)) == expected
def test_i_extract_block():
lines = LinesIterator(" aaaa\n\n bbbb\n\n cccc\n dddd")
blocks = dedent(
"""
aaaa:
bbbb
cccc
dddd
"""
)
lines = LinesIterator(blocks)
text = "\n".join(_i_extract_block(lines))
assert text == """aaaa\n\n bbbb\n\ncccc"""
assert text == """aaaa:\n\n bbbb\n\n cccc"""
blocks = dedent(
"""
aaaa:
bbbb
cccc
dddd
"""
)
lines = LinesIterator(blocks)
text = "\n".join(_i_extract_block(lines))
assert text == """aaaa:\n\n bbbb\n\n cccc\n"""
blocks = dedent(
"""
aaaa:
bbbb
cccc
"""
)
lines = LinesIterator(blocks)
text = "\n".join(_i_extract_block(lines))
assert text == """aaaa:\n\n bbbb\n\n cccc"""
lines = LinesIterator("")
text = "\n".join(_i_extract_block(lines))
assert text == ""
lines = LinesIterator("aaaa\n bbbb") # the indented block is cut by a new block
lines = LinesIterator("\n")
text = "\n".join(_i_extract_block(lines))
assert text == ""
lines = LinesIterator(" \n ")
lines = LinesIterator("aaaa:")
text = "\n".join(_i_extract_block(lines))
assert text == ""
lines = LinesIterator(" aaaa\n bbbb")
text = "\n".join(_i_extract_block(lines))
assert text == "aaaa\nbbbb"
assert text == "aaaa:"

View File

@@ -119,6 +119,24 @@ def test_paths_operation_requestBody():
}
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_operation_responses():
oas = OpenApiSpec3()
response = oas.paths["/users/{petId}"].get.responses[200]

View File

@@ -35,6 +35,7 @@ class PetCollectionView(PydanticView):
"""
Get a list of pets
Tags: pet
Status Codes:
200: Successful operation
"""
@@ -46,7 +47,13 @@ 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") -> Union[r200[Pet], r404]:
async def get(
self,
id: int,
/,
size: Union[int, Literal["x", "l", "s"]],
day: Union[int, Literal["now"]] = "now",
) -> Union[r200[Pet], r404]:
return web.json_response()
async def put(self, id: int, /, pet: Pet):
@@ -120,6 +127,7 @@ async def test_generated_oas_should_have_pets_paths(generated_oas):
async def test_pets_route_should_have_get_method(generated_oas):
assert generated_oas["paths"]["/pets"]["get"] == {
"description": "Get a list of pets",
"tags": ["pet"],
"parameters": [
{
"in": "query",
@@ -246,20 +254,28 @@ async def test_pets_id_route_should_have_get_method(generated_oas):
"required": True,
"schema": {"title": "id", "type": "integer"},
},
{'in': 'query',
'name': 'size',
'required': True,
'schema': {'anyOf': [{'type': 'integer'},
{'enum': ['x', 'l', 's'],
'type': 'string'}],
'title': 'size'}},
{'in': 'query',
'name': 'day',
'required': False,
'schema': {'anyOf': [{'type': 'integer'},
{'enum': ['now'], 'type': 'string'}],
'default': 'now',
'title': 'day'}}
{
"in": "query",
"name": "size",
"required": True,
"schema": {
"anyOf": [
{"type": "integer"},
{"enum": ["x", "l", "s"], "type": "string"},
],
"title": "size",
},
},
{
"in": "query",
"name": "day",
"required": False,
"schema": {
"anyOf": [{"type": "integer"}, {"enum": ["now"], "type": "string"}],
"default": "now",
"title": "day",
},
},
],
"responses": {
"200": {