Fastapi: [QUESTION]

Created on 2 Jun 2020  路  3Comments  路  Source: tiangolo/fastapi

First check

  • [x] I used the GitHub search to find a similar issue and didn't find it.
  • [x] I searched the FastAPI documentation, with the integrated search.
  • [x] I already searched in Google "How to X in FastAPI" and didn't find any information.

Description

How can I define Enum into request model?

class Processors(enum.Enum):
    Python = 'python'


class PostRepository(BaseModel):
    file: str
    processor: Processors

I have such endpoint:

@app.post('/repositories', status_code=201)
async def post_repositories(request: PostRepository):
    pass

After starting application, FastAPI don't take Enum field:

image

question

All 3 comments

The SwaggerUI I find often has trouble displaying Enums. Try the redoc docs at /redoc instead of /docs to see if it's displayed. You can also check the /openapi.json document directly to make sure your Enum is included.

You can still test the endpoint using your enum value, SwaggerUI just doesn't include it in the body for some reason. Might be fixed once Pydantic starts declaring enums as schemas in the next version? Not sure.

Could you try this?

class Processors(str, enum.Enum):
    Python = 'python'


class PostRepository(BaseModel):
    file: str
    processor: Processors

Thanks for the help here everyone! :clap: :bow:

I imagine you solved your problem, so thanks for closing the issue :+1:

Was this page helpful?
0 / 5 - 0 ratings