question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Integer enum query parameters validation always fails

See original GitHub issue

First check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn’t find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google “How to X in FastAPI” and didn’t find any information.
  • I already read and followed all the tutorial in the docs and didn’t find an answer.
  • I already checked if it is not related to FastAPI but to Pydantic.
  • I already checked if it is not related to FastAPI but to Swagger UI.
  • I already checked if it is not related to FastAPI but to ReDoc.
  • After submitting this, I commit to one of:
    • Read open issues with questions until I find 2 issues where I can help someone and add a comment to help there.
    • I already hit the “watch” button in this repository to receive notifications and I commit to help at least 2 people that ask questions in the future.
    • Implement a Pull Request for a confirmed bug.

Example

Here’s a self-contained, minimal, reproducible, example with my use case:

from enum import Enum
from fastapi import FastAPI

app = FastAPI()


class Category(int, Enum):
    MISC = 0
    LIFE = 1
    CODE = 2
    GUIDES = 3
    PROJECTS = 4


@app.get("/stuff")
def read_root(category: Category):
    return {"Hello": "World"}

Run $uvicorn test:app

Description

  • Open the browser and call the endpoint /stuff?category=1.
  • It returns a 422 error "value is not a valid enumeration member; permitted: 0, 1, 2, 3, 4" even though the correct int enum value is provided.
  • Expected: It returns a JSON with {"Hello": "World"}.

Environment

  • OS: macOS
  • FastAPI Version: 0.61.1
  • Python version: 3.8.5

Additional context

I’ve looked through all pydantic issues, found this one, but I don’t know if it’s related, or what it’s current status (whether it has been resolved).

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:5
  • Comments:14 (3 by maintainers)

github_iconTop GitHub Comments

14reactions
AlexKLWScommented, Oct 4, 2020

With IntEnum type validation works as expected

10reactions
acnebscommented, Feb 22, 2021

Sure, using the IntEnum example as above, all you see is this:

category: [0, 1, 2, 3, 4]

But as a consumer of the API, I have no idea what those numbers mean. I need to see their human-readable names in order to understand my options. If the names were listed in the schema, I would know that:

MISC = 0
LIFE = 1
CODE = 2
GUIDES = 3
PROJECTS = 4
Read more comments on GitHub >

github_iconTop Results From Across the Web

Why doesn't FastAPI handle types derived from int and Enum ...
Seems like validators are just hardcoded for IntEnum to be an integer validator + enum validator, and the integer validator converts from string ......
Read more >
Validation for integer enums fails with good values. - Bitbucket
Using 1.4.2 with a definition such as "type": "integer", "enum": [ 5, 10, 30 ] Passing any integer value such as 10 fails...
Read more >
Validations for Enum Types - Baeldung
Learn how to build validations for enums using Java custom annotations.
Read more >
Validating numeric query parameters in NestJS
What? But country is a numeric field! It's even an integer! Unfortunately, not for our application. Let's try to make one step back ......
Read more >
Common Problems Found in RAML 1.0 API Specifications
This error occurs when a facet or data type is defined as having a specific set of properties, yet an API specification defines...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found