Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Empty (Optional) Enums result in validation errors #1261

dries007 started this conversation in Feature request
Discussion options

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:

image

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 :)

You must be logged in to vote

Replies: 1 comment 1 reply

Comment options

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?

You must be logged in to vote
1 reply
Comment options

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants

AltStyle によって変換されたページ (->オリジナル) /