Skip to content

Commit

Permalink
🐛 Fix issue with rendering OpenAPI response body.
Browse files Browse the repository at this point in the history
. Updated OpenAPI docs for the response spec.
  • Loading branch information
tarsil committed Nov 5, 2022
1 parent 5e01e35 commit c707b06
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 4 deletions.
24 changes: 24 additions & 0 deletions docs/responses.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,30 @@ uses the `TemplateResponse`.
{!> ../docs_src/responses/template.py !}
```

## OpenAPI Responses

This is a special attribute that is used for OpenAPI spec purposes and can be created and added to a specific handler.

```python
from typing import Union

from esmerald import post
from esmerald.openapi.datastructures import ResponseSpec
from pydantic import BaseModel


class ItemOut(BaseModel):
sku: str
description: str


@post(path='/create', summary="Creates an item", responses={200: ResponseSpec(model=TaskIn, description=...)})
async def create() -> Union[None, ItemOut]:
...
```

This will add an extra response description and details to your OpenAPI spec handler definition.

## Other responses

There are other responses you can have that does not necessessarily have to be the ones provided here. Every case is
Expand Down
2 changes: 1 addition & 1 deletion docs/routing/handlers.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ class.
* **security** - Security definition of the application. Used for OpenAPI.
* **operation_id** - Internal unique identifier of the handler. Used for OpenAPI.
* **response_description** - Description of the response. Used for OpenAPI.
* **responses** - The available responses of the handler. Used for OpenAPI.
* **responses** - The available responses of the handler. Used for OpenAPI. [More details](../responses.md#openapi-responses).

## HTTP handler summary

Expand Down
3 changes: 2 additions & 1 deletion esmerald/config/openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
Server,
Tag,
)
from openapi_schemas_pydantic.v3_1_0.path_item import PathItem
from pydantic import AnyUrl, BaseModel
from typing_extensions import Literal

Expand Down Expand Up @@ -129,7 +130,7 @@ def parse_route(app, prefix=""):
schema.paths[path] = {}
if not verb in schema.paths[path]:
schema.paths[path][verb] = {}
schema.paths[path][verb] = path_item
schema.paths[path][verb] = getattr(path_item, verb, None)
continue

route_app = getattr(route, "app", None)
Expand Down
1 change: 1 addition & 0 deletions esmerald/openapi/apiview.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ def render_swagger_ui(self, request: Request) -> str:
schema_copy.json(by_alias=True, exclude_none=True),
option=OPT_INDENT_2,
).decode("utf-8")

head = f"""
<head>
<title>{schema.info.title}</title>
Expand Down
4 changes: 2 additions & 2 deletions esmerald/openapi/path_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def get_description_for_handler(
return description


def extract_layered_values(
def extract_level_values(
handler: "HTTPHandler",
) -> Tuple[Optional[List[str]], Optional[List[Dict[str, List[str]]]]]:
tags: List[str] = []
Expand Down Expand Up @@ -71,7 +71,7 @@ def create_path_item(
field=handler_fields["data"], create_examples=create_examples
)

tags, security = extract_layered_values(handler=handler)
tags, security = extract_level_values(handler=handler)
operation = Operation(
operationId=handler.operation_id or handler_name,
tags=tags,
Expand Down

0 comments on commit c707b06

Please sign in to comment.