Skip to content
This repository has been archived by the owner on Dec 31, 2023. It is now read-only.

decorators: hint at ref as component for strict responses #172

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions src/en/plugins/sanic-ext/openapi/decorators.md
Original file line number Diff line number Diff line change
Expand Up @@ -462,29 +462,40 @@ Do not forget to use `add_security_scheme`. See [security](./security.md) for mo
## Integration with Pydantic

Pydantic models have the ability to [generate OpenAPI schema](https://pydantic-docs.helpmanual.io/usage/schema/).

OpenAPI Schema can be defined in-line with `Test.schema()`, or defined and referenced as a component `openapi.Component(Test)`.
---:1
To take advantage of Pydantic model schema generation, pass the output in place of the schema.
:--:1
```python
from sanic import Sanic, json
from sanic_ext import validate, openapi
from typing import Literal
from pydantic import BaseModel, Field

class Test(BaseModel):
foo: str = Field(description="Foo Description", example="FOOO")
bar: str = "test"

biz: str = "default"
buz: Literal['this', 'that'] = Field(...,
description="... is a required",
example="this"
)

app = Sanic("test")

@app.get("/")
@openapi.definition(
body={'application/json': Test.schema()},
)
@openapi.response(200,
{"application/json": openapi.Component(Test)}
)
@validate(json=Test)
async def get(request):
return json({})
return json({
'foo': 'biz',
'bar': 'baz',
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be biz: ..

'buz': 'this'
})
```
:---

Expand Down