Svoboda | Graniru | BBC Russia | Golosameriki | Facebook
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: missing computed_fields for pydantic dto models #3656

Open
4 tasks
atom-andrew opened this issue Aug 3, 2024 · 0 comments
Open
4 tasks

Bug: missing computed_fields for pydantic dto models #3656

atom-andrew opened this issue Aug 3, 2024 · 0 comments
Labels
Bug 🐛 This is something that is not working as expected

Comments

@atom-andrew
Copy link

atom-andrew commented Aug 3, 2024

Description

I'm using the PydanticDTO wrapper and I'm not seeing computed fields in either the generated openapi schema or the results. I've added the PydanticPlug to the application. Possibly unrelated, I'm also still seeing nodes marked as "exclude" in the pydantic showing up in the schema/results. Also, serialization alias generation from pydantic doesn't appear to affect the openapi schema.

The PR #2797 was intended to add computed fields. It introduces some code at https://github.com/litestar-org/litestar/blame/18d84d846b782ccaab06f550a4defce22b3082e8/litestar/contrib/pydantic/pydantic_schema_plugin.py#L312 that added the computed field definitions. Tracing through the execution, I believe I'm seeing the lack of similar code in the PydanticDTO. generate_field_definitions method to include these field definitions at in the pydantic dto factory. I would expect to see it at https://github.com/litestar-org/litestar/blame/18d84d846b782ccaab06f550a4defce22b3082e8/litestar/contrib/pydantic/pydantic_dto_factory.py#L109 ).
The result seems to be that we generated backend that doesn't spit out the computed fields. I'll work on creating an example, but I suspect the issue might be obvious to someone familiar with the code.

URL to code causing the issue

No response

MCVE

import json

from litestar import Controller, Litestar, get
from litestar.contrib.pydantic import PydanticDTO
from litestar.dto import DTOConfig
from pydantic import BaseModel,computed_field, Field
from litestar.testing import TestClient


class MyModel(BaseModel):
    hide_me: int = Field(456, exclude=True, serialization_alias="for_real")

    @computed_field
    def foo(self) -> int:
        return 123


class Test(Controller):
    @get(path="/test", return_dto=PydanticDTO[MyModel])
    async def test(self) -> MyModel:
        return MyModel.model_validate({})


if __name__ == "__main__":
    app = Litestar(
        route_handlers=[Test],
    )
    print(json.dumps(app.openapi_schema.to_schema(), indent=4))
    with TestClient(app=app) as client:
        print(client.get("/test").text)

Steps to reproduce

Run the code above

Logs

/opt/homebrew/Caskroom/miniconda/base/envs/raven-env/bin/python /Users/andrew/Library/Application Support/JetBrains/PyCharm2024.1/scratches/scratch_121.py 
{
    "info": {
        "title": "Litestar API",
        "version": "1.0.0"
    },
    "openapi": "3.1.0",
    "servers": [
        {
            "url": "/"
        }
    ],
    "paths": {
        "/test": {
            "get": {
                "summary": "Test",
                "operationId": "TestTest",
                "responses": {
                    "200": {
                        "description": "Request fulfilled, document follows",
                        "headers": {},
                        "content": {
                            "application/json": {
                                "schema": {
                                    "$ref": "#/components/schemas/TestMyModelResponseBody"
                                }
                            }
                        }
                    }
                },
                "deprecated": false
            }
        }
    },
    "components": {
        "schemas": {
            "TestMyModelResponseBody": {
                "properties": {
                    "hide_me": {
                        "type": "integer",
                        "default": 456
                    }
                },
                "type": "object",
                "required": [],
                "title": "TestMyModelResponseBody"
            }
        }
    }
}
{"hide_me":456}
INFO - 2024-08-03 14:04:54,649 - httpx - _client - HTTP Request: GET http://testserver.local/test "HTTP/1.1 200 OK"

Process finished with exit code 0

Litestar Version

2.10.0

Platform

  • Linux
  • Mac
  • Windows
  • Other (Please specify in the description above)

Note

While we are open for sponsoring on GitHub Sponsors and
OpenCollective, we also utilize Polar.sh to engage in pledge-based sponsorship.

Check out all issues funded or available for funding on our Polar.sh dashboard

  • If you would like to see an issue prioritized, make a pledge towards it!
  • We receive the pledge once the issue is completed & verified
  • This, along with engagement in the community, helps us know which features are a priority to our users.
Fund with Polar
@atom-andrew atom-andrew added the Bug 🐛 This is something that is not working as expected label Aug 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug 🐛 This is something that is not working as expected
Projects
None yet
Development

No branches or pull requests

1 participant