Skip to content

FastAPI app with separate_input_output_schemas disabled excludes computed fields on Pydantic models from OpenAPI output #14431

@tiangolo

Description

@tiangolo

Adding the issue for completeness.

Solved in #13207

Discussed in #10342

Originally posted by ldthorne September 27, 2023

First Check

  • I added a very descriptive title here.
  • I used the GitHub search to find a similar question 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

import pytest
from fastapi import FastAPI
from starlette.testclient import TestClient


@pytest.fixture(name="client")
def get_client():
    app = FastAPI(separate_input_output_schemas=False)

    from pydantic import BaseModel, computed_field

    class Rectangle(BaseModel):
        width: int
        length: int

        @computed_field
        @property
        def area(self) -> int:
            return self.width * self.length

    @app.get("/")
    def read_root() -> Rectangle:
        return Rectangle(width=3, length=4)

    client = TestClient(app)
    return client


def test_get(client: TestClient):
    response = client.get("/openapi.json")
    data = response.json()

    rectangle_model = data["components"]["schemas"]["Rectangle"]["properties"]

    assert "area" in rectangle_model.keys()

Description

I expect that even with separate_input_output_schemas set to False the OpenAPI schema includes computed fields declared on a Pydantic model

Operating System

macOS

Operating System Details

No response

FastAPI Version

0.103.1

Pydantic Version

2.4.1

Python Version

3.8.16

Additional Context

I think it may come from here

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions