From a4f4f0a1e93b28aeb54b6fa7b630ffb0c8e1f491 Mon Sep 17 00:00:00 2001 From: William Fleurant Date: Sat, 5 Aug 2023 22:49:07 +0200 Subject: [PATCH] decorators: hint at ref as component for strict responses --- .../plugins/sanic-ext/openapi/decorators.md | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/en/plugins/sanic-ext/openapi/decorators.md b/src/en/plugins/sanic-ext/openapi/decorators.md index f70825a14f..1b6dd3f17c 100644 --- a/src/en/plugins/sanic-ext/openapi/decorators.md +++ b/src/en/plugins/sanic-ext/openapi/decorators.md @@ -462,19 +462,23 @@ 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") @@ -482,9 +486,16 @@ app = Sanic("test") @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', + 'buz': 'this' + }) ``` :---