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: Description not set for UUID based path parameters in OpenAPI #2967

Closed
1 of 4 tasks
Alc-Alc opened this issue Jan 9, 2024 · 4 comments · Fixed by #3118
Closed
1 of 4 tasks

Bug: Description not set for UUID based path parameters in OpenAPI #2967

Alc-Alc opened this issue Jan 9, 2024 · 4 comments · Fixed by #3118
Assignees
Labels
Bug 🐛 This is something that is not working as expected

Comments

@Alc-Alc
Copy link
Contributor

Alc-Alc commented Jan 9, 2024

Description

The value set in Parameter(description="UUID ID") is not used for the actual description in the OpenAPI schema if the path parameter is of type UUID. I did some digging and found out this is due to the presence of the following code.

# we only want to transfer values from the `KwargDefinition` to `Schema` if the schema object
# doesn't already have a value for that property. For example, if a field is a constrained date,
# by this point, we have already set the `exclusive_minimum` and/or `exclusive_maximum` fields
# to floating point timestamp values on the schema object. However, the original `date` objects
# that define those constraints on `KwargDefinition` are still `date` objects. We don't want to
# overwrite them here.
if getattr(schema, schema_key, None) is None:
setattr(schema, schema_key, value)

Since description for UUID is already set

UUID: Schema(type=OpenAPIType.STRING, format=OpenAPIFormat.UUID, description="Any UUID string"),
the code above makes it so that description from the user defined Parameter is not set again.

Proposed Fix 1 (Breaking / debatably breaking?)

Remove "description" in

UUID: Schema(type=OpenAPIType.STRING, format=OpenAPIFormat.UUID, description="Any UUID string"),
such that it appears like so

UUID: Schema(type=OpenAPIType.STRING, format=OpenAPIFormat.UUID)

Proposed Fix 2 (Special Casing until next major release then replace with Proposed Fix 1)

Change

if getattr(schema, schema_key, None) is None:
such that it appears like so

if schema_key == "description" or getattr(schema, schema_key, None) is None:

@peterschutt suggested special casing with comments explaining the behavior would be ideal. Thoughts?

URL to code causing the issue

No response

MCVE

from uuid import UUID
from typing_extensions import Annotated
from litestar.params import Parameter
from litestar import get
from litestar.testing import create_test_client

@get("str/{id:str}")
async def str_path(id: Annotated[str, Parameter(description="String ID")]) -> str:
    return id

@get("uuid/{id:uuid}")
async def uuid_path(id: Annotated[UUID, Parameter(description="UUID ID")]) -> UUID:
    return id

with create_test_client([str_path, uuid_path]) as client:
    # correct, currently passes, should pass
    assert client.app.openapi_schema.paths["/str/{id}"].get.parameters[0].description == "String ID"

    # wrong, currently passes, should fail
    assert client.app.openapi_schema.paths["/uuid/{id}"].get.parameters[0].description == "Any UUID string"

    # expected, currently fails, should pass
    assert client.app.openapi_schema.paths["/uuid/{id}"].get.parameters[0].description == "UUID ID"

Steps to reproduce

1. Save as "mcve.py"
2. Run `python mcve.py`
3. Second assert that currently passes, should fail.
4. Third assert that currently fails, should pass.

Screenshots

"![SCREENSHOT_DESCRIPTION](SCREENSHOT_LINK.png)"

Logs

No response

Litestar Version

cc45c11 (main as of issue creation)

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
@Alc-Alc Alc-Alc added the Bug 🐛 This is something that is not working as expected label Jan 9, 2024
@peterschutt
Copy link
Contributor

Proposed Fix 1 (Breaking / debatably breaking?)

If we treat this as a bug and include the fix in a patch release, removing the default description will cause schema generation to change behavior between patch releases - this is why I'm in favor of option 2.

@provinzkraut
Copy link
Member

@Alc-Alc would you want to go ahead and implement a fix for this?

Copy link

This issue has been closed in #3118. The change will be included in the upcoming patch release.

Copy link

github-actions bot commented Mar 2, 2024

A fix for this issue has been released in v2.6.2

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
Status: Closed
Development

Successfully merging a pull request may close this issue.

3 participants