Skip to content
New issue

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

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

Already on GitHub? # to your account

Bug: Swagger and Redoc docs don't work for Piccolo ORM in Litestar>2.11.0 #3762

Closed
1 of 4 tasks
sinisaos opened this issue Sep 28, 2024 · 2 comments
Closed
1 of 4 tasks
Labels
Bug 🐛 This is something that is not working as expected

Comments

@sinisaos
Copy link
Contributor

sinisaos commented Sep 28, 2024

Description

Piccolo ORM has a feature for scaffolding simple ASGI applications for various ASGI frameworks. I notice that
Swagger and Redoc docs do not work with the latest version of Litestar. The latest working version is Litestar==2.11.0.
For scaffolding ASGI apps, we don't use PiccoloDTO but Piccolo's internal tool (create_pydantic_model) to create a Pydantic model from a Piccolo table, which has an extra property, but Litestar Schema does not has a extra key and that causes a ValueError to be raised. I tried two things and after that everything works.

  1. adding an extra key to a Schema like this
class Schema(BaseSchemaObject):
    ...
    extra: dict[str, Any] | None = None
  1. or excluding the extra key from checking here like this.
if not hasattr(schema, schema_key) and schema_key != "extra":
    raise ValueError(
        f"`schema_extra` declares key `{schema_key}` which does not exist in `Schema` object"
    )

I don't know if that's good enough, but these are just ideas. Any solution that will enable Piccolo ORM works with the latest Litestar version will be great. Thanks in advance.

URL to code causing the issue

No response

MCVE

https://github.com/sinisaos/simple-piccolo

Steps to reproduce

1. Clone repository
2. Install requirements
3. Start app with `python litestar_app.py`
4. Go to `http://localhost:8000/schema/swagger` and see error

Screenshots

"![SCREENSHOT_DESCRIPTION](SCREENSHOT_LINK.png)"

Logs

ERROR - 2024-09-28 07:35:34,331 - litestar - config - Uncaught exception (connection_type=http, path=/schema/swagger):
Traceback (most recent call last):
  File "/home/rkl/dev/piccolo_env/lib/python3.11/site-packages/litestar/middleware/_internal/exceptions/middleware.py", line 159, in __call__
    await self.app(scope, receive, capture_response_started)
  File "/home/rkl/dev/piccolo_env/lib/python3.11/site-packages/litestar/_asgi/asgi_router.py", line 100, in __call__
    await asgi_app(scope, receive, send)
  File "/home/rkl/dev/piccolo_env/lib/python3.11/site-packages/litestar/routes/http.py", line 80, in handle
    response = await self._get_response_for_request(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/rkl/dev/piccolo_env/lib/python3.11/site-packages/litestar/routes/http.py", line 132, in _get_response_for_request
    return await self._call_handler_function(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/rkl/dev/piccolo_env/lib/python3.11/site-packages/litestar/routes/http.py", line 152, in _call_handler_function
    response_data, cleanup_group = await self._get_response_data(
                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/rkl/dev/piccolo_env/lib/python3.11/site-packages/litestar/routes/http.py", line 195, in _get_response_data
    data = route_handler.fn(**parsed_kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/rkl/dev/piccolo_env/lib/python3.11/site-packages/litestar/_openapi/plugin.py", line 161, in _handler
    return plugin_.render(request, self.provide_openapi_schema())
                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/rkl/dev/piccolo_env/lib/python3.11/site-packages/litestar/_openapi/plugin.py", line 99, in provide_openapi_schema
    self._openapi_schema = self.provide_openapi().to_schema()
                           ^^^^^^^^^^^^^^^^^^^^^^
  File "/home/rkl/dev/piccolo_env/lib/python3.11/site-packages/litestar/_openapi/plugin.py", line 94, in provide_openapi
    self._openapi = self._build_openapi()
                    ^^^^^^^^^^^^^^^^^^^^^
  File "/home/rkl/dev/piccolo_env/lib/python3.11/site-packages/litestar/_openapi/plugin.py", line 83, in _build_openapi
    path_item = create_path_item_for_route(context, route)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/rkl/dev/piccolo_env/lib/python3.11/site-packages/litestar/_openapi/path_item.py", line 139, in create_path_item_for_route
    return path_item_factory.create_path_item()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/rkl/dev/piccolo_env/lib/python3.11/site-packages/litestar/_openapi/path_item.py", line 44, in create_path_item
    operation = self.create_operation_for_handler_method(route_handler, HttpMethod(http_method))
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/rkl/dev/piccolo_env/lib/python3.11/site-packages/litestar/_openapi/path_item.py", line 68, in create_operation_for_handler_method
    request_body = create_request_body(
                   ^^^^^^^^^^^^^^^^^^^^
  File "/home/rkl/dev/piccolo_env/lib/python3.11/site-packages/litestar/_openapi/request_body.py", line 49, in create_request_body
    schema = schema_creator.for_field_definition(data_field)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/rkl/dev/piccolo_env/lib/python3.11/site-packages/litestar/_openapi/schema_generation/schema.py", line 333, in for_field_definition
    result = self.for_plugin(field_definition, plugin_for_annotation)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/rkl/dev/piccolo_env/lib/python3.11/site-packages/litestar/_openapi/schema_generation/schema.py", line 515, in for_plugin
    schema = plugin.to_openapi_schema(field_definition=field_definition, schema_creator=self)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/rkl/dev/piccolo_env/lib/python3.11/site-packages/litestar/contrib/pydantic/pydantic_schema_plugin.py", line 235, in to_openapi_schema
    return self.for_pydantic_model(field_definition=field_definition, schema_creator=schema_creator)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/rkl/dev/piccolo_env/lib/python3.11/site-packages/litestar/contrib/pydantic/pydantic_schema_plugin.py", line 252, in for_pydantic_model
    return schema_creator.create_component_schema(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/rkl/dev/piccolo_env/lib/python3.11/site-packages/litestar/_openapi/schema_generation/schema.py", line 645, in create_component_schema
    schema.properties = {k: self.for_field_definition(v) for k, v in property_fields.items()}
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/rkl/dev/piccolo_env/lib/python3.11/site-packages/litestar/_openapi/schema_generation/schema.py", line 645, in <dictcomp>
    schema.properties = {k: self.for_field_definition(v) for k, v in property_fields.items()}
                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/rkl/dev/piccolo_env/lib/python3.11/site-packages/litestar/_openapi/schema_generation/schema.py", line 361, in for_field_definition
    return self.process_schema_result(field_definition, result) if isinstance(result, Schema) else result
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/rkl/dev/piccolo_env/lib/python3.11/site-packages/litestar/_openapi/schema_generation/schema.py", line 596, in process_schema_result
    raise ValueError(
ValueError: `schema_extra` declares key `extra` which does not exist in `Schema` object

Litestar Version

2.12.1

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
@sinisaos sinisaos added the Bug 🐛 This is something that is not working as expected label Sep 28, 2024
@provinzkraut
Copy link
Member

This is a bug on Piccolo's side. Litestar is strict about the OpenAPI schema, which does not specify an extra property field as of version 3.1, nor does it allow arbitrary fields, but Piccolo seems to use this to include additional information.

The spec does allow for extensions of the schema, but those fields must be prefixed with an x-: https://spec.openapis.org/oas/v3.1.0.html#specification-extensions

@provinzkraut provinzkraut closed this as not planned Won't fix, can't repro, duplicate, stale Sep 28, 2024
@sinisaos
Copy link
Contributor Author

@provinzkraut OK. Thank you for your time and effort.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Bug 🐛 This is something that is not working as expected
Projects
None yet
Development

No branches or pull requests

2 participants