swagger-api / swagger-api/swagger-ui

OpenAPI 3.1.0 support: Swagger ui doesn't generate any "Example Value | Schema" with fastapi 0.100.0 (Pydantic 2)

Open
#9,017 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
29k
Forks
9.3k
Avg merge
2d 23h
Merged PRs (30d)
25

Description

Just doesn't generate anything else, besides this (picture1)
Picture 1
image

By the way, it's post endpoint and it works fine with fastapi 0.95.* (picture 2)
Picture 2
image

schema_extra and model_configs don't change the situation:

    class Config:
        extra = "forbid"  # ignore, allow
        schema_extra = {
            "example": {
                "target": 'some'
            }
        }

and

model_config = {
        "json_schema_extra": {
            "examples": [
                {
                    "name": "Foo",
                    "description": "A very nice Item",
                    "price": 35.4,
                    "tax": 3.2,
                }
            ]
        }
    }

nothing changes

code
entities

from __future__ import annotations

from pydantic import BaseModel, Field, Extra


class PydanticConfig(BaseModel):
    class Config:
        extra = "forbid"  # ignore, allow


class RunCommandRequest(BaseModel):
    host: str | None = Field(
        title='host',
        description='Хост для удалённого подключения. Может быть доменом, может ip адресом.',
        default=None
    )
    port: int = Field(default=22)
    user: str = Field(default="root")
    password: str | None = Field(default=None)
    commands: list[str] = Field(examples=["uname", "ls"])
    sudo: bool = Field(default=False, examples=[True])

api

from __future__ import annotations
from concurrent.futures import ThreadPoolExecutor, as_completed
import enum
import sys
import asyncio

from fastapi import APIRouter, HTTPException

from ssh_app.entities import RunCommandRequest, RunCommandResponse
from structlog_config import log
from ssh_app.fabfile import make_a_connection, execute_remote_cmd


# initializing api router
router = APIRouter()


# setting up api urls
class URLs(str, enum.Enum):
    put_config = "ssh_app/v1/put/config"
    get_file = "ssh_app/v1/get/file/{remote_path}/{local_path}"
    put_file = "ssh_app/v1/put/file"
    post_run_command = "ssh_app/v1/post/commands_to_run"


# За тред экзекьютить
@router.post(
    URLs.post_run_command.value,
    response_model=RunCommandResponse,
    name="Выполни команды в терминале на удалённом сервере через SSH",
    summary="Выполнение команд"
)
async def run_command(request: RunCommandRequest):
    # loop = asyncio.get_running_loop()  # where is better to locate it?
    # with ThreadPoolExecutor() as pool:
    #     title = await loop.run_in_executor(pool, parse_a_title, html)
    try:
        with ThreadPoolExecutor() as executor:
            try:
                connection = make_a_connection(
                    host=request.host,
                    port=request.port,
                    user=request.user,
                    password=request.password
                )
                result = executor.submit(execute_remote_cmd, connection, request.commands).result()
                # result = execute_remote_cmd(connection, request.commands)
                # connection.close()
            except Exception as err:
                raise HTTPException(status_code=500, detail=err)
            return RunCommandResponse(
                result=result
            )
    except Exception as err:
        raise HTTPException(status_code=500, detail=err)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue with FastAPI 0.100.0 and Pydantic 2 using the shown RunCommandRequest model and POST endpoint, then inspect the generated OpenAPI document and Swagger UI's Example Value | Schema rendering. Compare it with FastAPI 0.95.*; done means the request schema and examples render in Swagger UI as they did before.

Written by the indexing model from the issue text.

Assessment

Tech stack
fastapi, openapi, python
Domain
api, documentation, frontend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.