-
-
Notifications
You must be signed in to change notification settings - Fork 263
Empty (Optional) Enums result in validation errors #1261
-
Hi,
The following snippet from my openapi file results in errors, while it's handled correctly by the docgen tools I've used so far:
{
"name": "template",
"in": "query",
"required": false,
"schema": {
"title": "Template",
"enum": [
],
"description": "Which template to render (if not a JSON request)."
},
"description": "Which template to render (if not a JSON request)."
},Errors:
paths.<snip>.get.parameters.0.other.schema.other.enum
List should have at least 1 item after validation, not 0 [type=too_short, input_value=[], input_type=list]
For further information visit https://errors.pydantic.dev/2.11/v/too_short
This is what it looks like in Swagger's ui thing:
I could add a special case to the generator code to avoid this parameter when the list of options is empty, but I'd rather keep it generic if at all possible.
My expectation for the generated code would be that it makes an empty enum, something like this:
class Template(enum.StrEnum): pass
Omitting the option from the parameter list would also be OK I guess, but leaves less room for forward comparability.
I wouldn't mind making an MR, but I would like to avoid spending the effort if it has no chance of getting accepted.
Thanks :)
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 1 comment 1 reply
-
According to JSONSchema, the enum "SHOULD" have at least one value—doesn't say "MUST", so I suppose we could do something with an empty enum array rather than erroring. Wouldn't that create impossible Python code, though? If a parameter takes in a Template, but there are no instances of Template... nothing can be passed there?
Beta Was this translation helpful? Give feedback.
All reactions
-
The only valid case I suspect is if the field is optional, so None would allowed.
EDIT: Actually, I guess that depends on how none values are handled in the API. (I'm not very familiar yet with this project, sorry)
Beta Was this translation helpful? Give feedback.