feat: add support for operationId in docstring; feat: v1.120.6

This commit is contained in:
Georg K 2022-11-11 10:02:27 +03:00
parent 83739c7c8e
commit ba0530d6b1
4 changed files with 28 additions and 1 deletions

View File

@ -120,6 +120,19 @@ def tags(docstring: str) -> List[str]:
return []
def operation_id(docstring: str) -> str | None:
"""
Extract the "OperationId:" block of the docstring.
"""
iterator = LinesIterator(docstring)
for line in iterator:
if re.fullmatch("operation_?id\\s*:.*", line, re.IGNORECASE):
iterator.rewind()
return line.split(":")[1].strip(' ')
return None
def operation(docstring: str) -> str:
"""
Extract all docstring except the "Status Code:" block.
@ -127,7 +140,7 @@ def operation(docstring: str) -> str:
lines = LinesIterator(docstring)
ret = []
for line in lines:
if re.fullmatch("status\\s+codes?\\s*:|tags\\s*:.*", line, re.IGNORECASE):
if re.fullmatch("status\\s+codes?\\s*:|tags\\s*:.*|operation_?id\\s*:.*", line, re.IGNORECASE):
lines.rewind()
for _ in _i_extract_block(lines):
pass

View File

@ -207,6 +207,17 @@ class OperationObject:
else:
self._spec.pop("tags", None)
@property
def operation_id(self) -> str | None:
return self._spec.get("operationId", None)
@operation_id.setter
def operation_id(self, operation_id: str | None) -> None:
if operation_id:
self._spec["operationId"] = operation_id
else:
self._spec.pop("operationId", None)
class PathItem:
def __init__(self, spec: dict):

View File

@ -87,6 +87,7 @@ def _add_http_method_to_oas(
if description:
oas_operation.description = docstring_parser.operation(description)
oas_operation.tags = docstring_parser.tags(description)
oas_operation.operation_id = docstring_parser.operation_id(description)
status_code_descriptions = docstring_parser.status_code(description)
else:
status_code_descriptions = {}

View File

@ -47,6 +47,7 @@ class PetCollectionView(PydanticView):
Tags: pet
Status Codes:
200: Successful operation
OperationId: createPet
"""
return web.json_response()
@ -147,6 +148,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",
"operationId": "createPet",
"tags": ["pet"],
"parameters": [
{