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: Optionally typed parameters do not render in OpenAPI docs properly #2546

Closed
1 of 4 tasks
JacobCoffee opened this issue Oct 27, 2023 · 2 comments · Fixed by #2550
Closed
1 of 4 tasks

Bug: Optionally typed parameters do not render in OpenAPI docs properly #2546

JacobCoffee opened this issue Oct 27, 2023 · 2 comments · Fixed by #2550
Assignees
Labels
Bug 🐛 This is something that is not working as expected

Comments

@JacobCoffee
Copy link
Member

JacobCoffee commented Oct 27, 2023

Description

When using an arbitrary enum as a type (or Literal assumedly), it generates a dropdown in the OpenAPI docs.

environment: Environment = Parameter(
        title="Environment",
        description="The environment to filter by.",
        default=None,
        required=False,
    ),

but hitting this endpoint gives a validation error:

 File ".venv/lib/python3.10/site-packages/litestar/_signature/model.py", line 190, in parse_values_from_connection_kwargs
    return convert(kwargs, cls, strict=False, dec_hook=deserializer, str_keys=True).to_dict()
msgspec.ValidationError: Expected `str`, got `null` - at `$.environment`

This makes sense, but the fix of adding | None moves the dropdown field to just a plain textfield.

URL to code causing the issue

No response

MCVE

from enum import StrEnum
from litestar import Litestar, get
from litestar.params import Parameter

class Environment(StrEnum):
    DEVELOPMENT = "dev"
    TEST = "test"
    QA = "qa"

@get(path="/")
async def test(
    environment: Environment
    | None = Parameter(
        title="Environment",
        description="The environment to filter by.",
        default=None,
        required=False,
    ),
) -> None:
    return None

app = Litestar([test])

Steps to reproduce

1. Write mcve.py
2. Browse to OpenAPI schema
3. Observe dropdown fields
4. Execute on the test route
5. See error
6. Apply fix `Environment | None`
7. Reload OpenAPI schema
8. See no dropdown, just text field
9. Execute on the test route
10. See it work, but be deeply saddened that there is no longer a dropdown box and contemplate deleting your entire application.

Screenshots

: Environment
image

: Environment | None
image

Logs

No response

Litestar Version

2.x

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 Litestar 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
@JacobCoffee JacobCoffee added Bug 🐛 This is something that is not working as expected Triage Required 🏥 This requires triage labels Oct 27, 2023
@JacobCoffee
Copy link
Member Author

Without looking, this is probably expected behavior.. but it sucks

@peterschutt
Copy link
Contributor

    environment: Environment
    | None = Parameter(

"rough fmt" amiright?

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

Successfully merging a pull request may close this issue.

2 participants