Maillol / aiohttp-pydantic

Aiohttp View that validates request body and query sting regarding the annotations declared in the View method
MIT License
67 stars 21 forks source link

Set 'title' for model field #19

Closed eduard-sukharev closed 3 years ago

eduard-sukharev commented 3 years ago

I have a model:

from aiohttp_pydantic import PydanticView, oas
from pydantic import BaseModel

class Task(BaseModel):
    id: str
    type: str
    on_success: Optional[str] # ID for task to run on success
    on_error: Optional[str] # ID for task to run on failure

@routes.view('/task')
class TaskView(PydanticView):
    async def post(self, task: Task):
        // some task logic

And in a Swagger UI (OAS) I get following model description:

id*: string
    title: Id
type*: string
    title: Type
on_success: string
    title: On Success
on_error: string
    title: On Error

What I want is to somehow define the title in pydantic model so that it's not plainly repeat the field itself. How do I do that?

Maillol commented 3 years ago
from pydantic import BaseModel, Field

class Task(BaseModel):
    id: str = Field(title="my title")
    type: str
    on_success: Optional[str] # ID for task to run on success
    on_error: Optional[str] # ID for task to run on failure

You can find more details here:

https://pydantic-docs.helpmanual.io/usage/schema/#field-customisation