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: default not generated for query parameter in openapi spec. #3278

Closed
1 of 4 tasks
globglarb opened this issue Mar 28, 2024 · 9 comments · Fixed by #3280
Closed
1 of 4 tasks

Bug: default not generated for query parameter in openapi spec. #3278

globglarb opened this issue Mar 28, 2024 · 9 comments · Fixed by #3280
Labels
Bug 🐛 This is something that is not working as expected

Comments

@globglarb
Copy link

globglarb commented Mar 28, 2024

Description

Hi,

The default of a query parameter is not properly generated when creating the openapi specification. See MVCE down below.

The generated specification looks like this:

...
"paths": {
    "/query_default": {
      "get": {
        "summary": "QueryDefault",
        "operationId": "QueryDefaultQueryDefault",
        "parameters": [
          {
            "name": "foo",
            "in": "query",
            "schema": {
              "type": "integer"
            },
            "required": false,
            "deprecated": false,
            "allowEmptyValue": false,
            "allowReserved": false
          }
        ],
...

where the default is missing in the schema property.

URL to code causing the issue

No response

MCVE

@get(path="query_default")
def query_default(foo: int = 12) -> None:
    return

app = Litestar(route_handlers=[query_default])

Steps to reproduce

No response

Screenshots

No response

Logs

No response

Litestar Version

Version 2.7.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
@globglarb globglarb added the Bug 🐛 This is something that is not working as expected label Mar 28, 2024
@JacobCoffee
Copy link
Member

Could you confirm that this is also true in 2.7.0?

@globglarb
Copy link
Author

Same result with fixed version 2.7.0

dependencies = [
    "litestar[standard]==2.7.0"
]

@JacobCoffee
Copy link
Member

Thanks!

@guacs
Copy link
Member

guacs commented Mar 29, 2024

I don't think this is a bug. The specification doesn't have a field to indicate a default value for parameters.

@guacs guacs closed this as not planned Won't fix, can't repro, duplicate, stale Mar 29, 2024
@guacs
Copy link
Member

guacs commented Mar 29, 2024

Sorry about that. I was completely wrong. While it's correct that the parameter object as per the spec doesn't allow for defaults, it does allow for a schema which can have a default value.

Us not parsing the default properly is a bug.

@guacs guacs reopened this Mar 29, 2024
@Alc-Alc
Copy link
Contributor

Alc-Alc commented Mar 29, 2024

Just leaving this here in case you need a workaround. You can use Annotated[int, Parameter(default=12)] to get the behavior you expect.

from typing_extensions import Annotated
from litestar import Litestar, get
from litestar.params import Parameter

@get(path="query_default")
async def query_default(foo: Annotated[int, Parameter(default=12)]) -> None:
    return

app = Litestar(route_handlers=[query_default])

@globglarb
Copy link
Author

Thanks for the workaround. I added the working schema snippet for completeness

...
 "paths": {
    "/query_default": {
      "get": {
        "summary": "QueryDefault",
        "operationId": "QueryDefaultQueryDefault",
        "parameters": [
          {
            "name": "foo",
            "in": "query",
            "schema": {
              "type": "integer",
              "default": 12
            },
            "required": false,
            "deprecated": false,
            "allowEmptyValue": false,
            "allowReserved": false
          }
        ],
...

Copy link

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

Copy link

github-actions bot commented Apr 5, 2024

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

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.

4 participants