Skip to content

How to write comments for model properties in generated OpenApi doc (using pydantic model)? #3632

@FrederikNygaardSvendsen

Description

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn't find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google "How to X in FastAPI" and didn't find any information.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to FastAPI but to Pydantic.
  • I already checked if it is not related to FastAPI but to Swagger UI.
  • I already checked if it is not related to FastAPI but to ReDoc.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

from typing import List, Optional
from pydantic import BaseModel


class Log(BaseModel):
    level: str
    message: str
    timestamp: int
    properties: dict = None

class CreateLogRequestDto(BaseModel):
    loggroup: str
    stream: Optional[str] = None # I would like to add a comment for this property (stream) in the generated OpenAPi spec
    logs: List[Log]

Description

Hi Everyone!

I just started using FastApi a couple of days ago, and already have a microservice running in production. My question is regarding the auto generated OpenApi spec. I am using the pydantic BaseModel to declare a class, that represent the expected input (request). In the router function, i typehint that classtype for the input argument, and the generated OpenApi doc looks as expected.

However, i would love to be able to write some comments for some of the properties in the generated OpenApi spec for the request. Please see uploaded picture for reference.

image

Is it possible to do so, and how?

Regards,
Frederik

Operating System

macOS

Operating System Details

No response

FastAPI Version

0.66.1

Python Version

3.8.3

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions