-
-
Notifications
You must be signed in to change notification settings - Fork 344
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
Enhancement: Only generate example once when Parameter(examples=[...])
is specified for path arg and its schema
#3057
Comments
Parameter(examples=[...])
, for path part arg and its schemaParameter(examples=[...])
, for path arg and its schema
Parameter(examples=[...])
, for path arg and its schemaParameter(examples=[...])
is specified for path arg and its schema
Not really a bug I think but rather an enhancement. We have a few instances where this kind of duplication occurs and it's reasonable to optimise some of these away. Iirc @peterschutt you were discussing a similar case recently regarding DTOs? |
This perhaps-too-eager-example-generation also applies to the response examples created via "components": {
"schemas": {
"Response": {
"properties": {
"text": {
"type": "string",
"examples": {
"text-example-1": { <---- example per field, unnecessary?
"description": "Example text value",
"value": "DwEkQQHiBrmXZcSFtoJx"
}
}
},
"num": {
"type": "integer",
"examples": {
"num-example-1": { <---- example per field, unnecessary?
"description": "Example num value",
"value": 4966
}
}
}
},
"type": "object",
"required": [
"num",
"text"
],
"title": "Response",
"examples": {
"response-example-1": { <---- example per model, this is ok
"description": "Example value",
"value": {
"text": "IgNZYFcagWptUqCwdERi",
"num": 3674
}
}
}
}
}
} I don't know if the GUIs actually need or benefit from field-specific examples, but perhaps the model level one would suffice. |
IMO as long as we don't have more granular settings for turning example generation on/off, generating them everywhere it's supported is the more reasonable approach since it covers more use cases. Say we don't generate examples for the fields but a user wanted examples generated for that, they would have no way of turning this on. This way, we might make the schema a bit more verbose, but it actually doesn't do any harm to have these examples in there. |
That's a fair statement. All off by default, all enabled with However, currently errors do get examples by default, e.g.: "examples": {
"NotAuthorized": {
"value": {
"status_code": 401,
"detail": "Missing or invalid credentials",
"extra": {}
}
}
}
} That's an exception to the rule. And then you allow disabling example generation with I guess there are roughly 4 modes here:
There could be a toggle, but I agree that global on/off should be sufficient, as the OpenAPI schema is really intended as machine-readable and not really for humans. (The other issue #3058 confused me a bit here.) |
Description
Explained below.
MCVE
The generated schema contains:
I would expect one occurrence is sufficient. The example was given for/as OpenAPI path parameter (via
Parameter
) so the latter is correct, and the JSON schema example (former one) can be dropped.Steps to reproduce
1. `python app.py` 2. See the generated schema
Screenshots
No response
Logs
No response
Litestar Version
2.5.1
Platform
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
The text was updated successfully, but these errors were encountered: