Skip to content

Bug: @computed_field properties are missing in generated OpenAPI schemas #8169

@jontmy

Description

@jontmy

Expected Behaviour

Define a Pydantic model with a @computed_field and use it as the response object, etc.

class EntityWithComputedField(BaseModel):
    foo: int = 0

    @computed_field
    @property
    def foo_plus_one(self) -> int:
        return self.foo + 1

The generated OpenAPI schema should include the field foo_plus_one, for example in this snippet of an OpenAPI schema:

"components": {
    "schemas": {
      "EntityWithComputedField": {
        "properties": {
          "foo": {
            "type": "integer",
            "title": "Foo",
            "default": 0
          },
          "foo_plus_one": {
            "type": "integer",
            "title": "Foo Plus One",
            "readOnly": true
          }
        },
        "type": "object",
        "required": [
          "foo_plus_one"
        ],
        "title": "EntityWithComputedField"
      },

Similar discussion in FastAPI (now supported): fastapi/fastapi#9856.

Current Behaviour

The generated OpenAPI schema does not include the field foo_plus_one.

Snippet of the generated schema:

"components": {
    "schemas": {
      "EntityWithComputedField": {
        "properties": {
          "foo": {
            "type": "integer",
            "title": "Foo",
            "default": 0
          }
        },
        "type": "object",
        "title": "EntityWithComputedField"
      },

Code snippet

from aws_lambda_powertools.event_handler import HttpResolverLocal
from aws_lambda_powertools.utilities.typing import LambdaContext
from pydantic import BaseModel, computed_field

app = HttpResolverLocal(enable_validation=True, debug=False)
app.enable_swagger(path="/swagger")


class EntityWithComputedField(BaseModel):
    foo: int = 0

    @computed_field
    @property
    def foo_plus_one(self) -> int:
        return self.foo + 1


def lambda_handler(event: dict, context: LambdaContext) -> dict:
    return app.resolve(event, context)


@app.get("/")
def hello() -> EntityWithComputedField:
    return EntityWithComputedField().model_dump()

Possible Solution

Powertools should set the JSON schema mode during schema generation to "serialization" instead of "validation" (which is the default).

Reference: https://pydantic.dev/docs/validation/latest/concepts/json_schema/

Steps to Reproduce

In a terminal, run:

mkdir openapi-computed-field
cd openapi-computed-field
uv init --build-backend uv
uv add aws-lambda-powertools pydantic uvicorn
mkdir -p src/openapi_computed_field
touch src/openapi_computed_field/main.py

Update the created src/openapi_computed_field/main.py file with the following:

from aws_lambda_powertools.event_handler import HttpResolverLocal
from aws_lambda_powertools.utilities.typing import LambdaContext
from pydantic import BaseModel, computed_field

app = HttpResolverLocal(enable_validation=True, debug=False)
app.enable_swagger(path="/swagger")


class EntityWithComputedField(BaseModel):
    foo: int = 0

    @computed_field
    @property
    def foo_plus_one(self) -> int:
        return self.foo + 1


def lambda_handler(event: dict, context: LambdaContext) -> dict:
    return app.resolve(event, context)


@app.get("/")
def hello() -> EntityWithComputedField:
    return EntityWithComputedField().model_dump()

Back in the terminal, run:

uv run uvicorn openapi_computed_field.main:app

Navigate to http://127.0.0.1:8000/swagger and notice how the EntityWithComputedField schema does not include the computed field foo_plus_one.

Powertools for AWS Lambda (Python) version

latest

AWS Lambda function runtime

3.14

Packaging format used

PyPi

Debugging logs

Metadata

Metadata

Labels

bugSomething isn't working

Type

No type

Projects

Status

Pending review

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions