Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

Validation Error when using response_model and Dynamic Key type #988

Closed
merc opened this issue Feb 16, 2020 · 3 comments
Closed

Validation Error when using response_model and Dynamic Key type #988

merc opened this issue Feb 16, 2020 · 3 comments

Comments

@merc
Copy link

merc commented Feb 16, 2020

Describe the bug

Hi guys, seem that when using a Dynamic Key type of model with response_model will raise ValidationError on the server side and returns 500 Internal Server Error to the client.

I have the following example code, I believe I am using it correctly?

could this be relater to #911 ?

I also realize I can substitute response_model=PetsByName with response_model=Dict[str, str], but with the later I am not able to include an example in schema_extra, is there other way to do so? many thanks!

To Reproduce

Steps to reproduce the behavior with a minimum self-contained file.

Replace each part with your own scenario:

  1. Create a file with:

main.py

from pydantic import BaseModel
from fastapi import FastAPI
from typing import Dict

app = FastAPI()

class PetsByName(BaseModel):
    __root__: Dict[str, str]

    class Config:
        schema_extra = {
                'example': {'Otis': 'dog', 'Milo': 'cat'}
            }

@app.get("/animal/", response_model=PetsByName)
async def animal():
    return {'Otis': 'dog', 'Milo': 'cat'}
  1. Using curl to test the endpoint:
$ curl -L localhost:8000/animal
Internal Server Error
  1. It returns a JSON with Internal Server Error.

  2. But I expected it to return {'Otis': 'dog', 'Milo': 'cat'}.

  3. Terminal output of the server:

...
  File "/home/merc/.virtualenvs/py38/lib/python3.8/site-packages/starlette/routing.py", line 41, in app
    response = await func(request)
  File "/home/merc/.virtualenvs/py38/lib/python3.8/site-packages/fastapi/routing.py", line 140, in app
    response_data = await serialize_response(
  File "/home/merc/.virtualenvs/py38/lib/python3.8/site-packages/fastapi/routing.py", line 78, in serialize_response
    raise ValidationError(errors, field.type_)
pydantic.error_wrappers.ValidationError: 1 validation error for PetsByName
response -> __root__
  field required (type=value_error.missing)

Expected behavior

Add a clear and concise description of what you expected to happen.

Screenshots

If applicable, add screenshots to help explain your problem.

Environment

  • OS: Ubuntu 19.04
  • FastAPI Version: 0.48.0
  • Python version: 3.8.1

Additional context

SwaggerUI renders the PetsByName model along with the example correctly, no issue there.

@merc merc added the bug Something isn't working label Feb 16, 2020
@merc
Copy link
Author

merc commented Feb 16, 2020

After reading #911 it definitely is related to that. Will close this one and move myself over to #911 for discussion. Many thanks!

@merc merc closed this as completed Feb 16, 2020
@tiangolo
Copy link
Owner

tiangolo commented Apr 6, 2020

Thanks for reporting back and closing the issue. Let's continue on the other one. 👍

@tiangolo tiangolo added question Question or problem answered reviewed and removed bug Something isn't working labels Feb 22, 2023
@tiangolo tiangolo changed the title [BUG] Validation Error when using response_model and Dynamic Key type Validation Error when using response_model and Dynamic Key type Feb 24, 2023
@tiangolo tiangolo reopened this Feb 28, 2023
@github-actions
Copy link
Contributor

Assuming the original need was handled, this will be automatically closed now. But feel free to add more comments or create new issues or PRs.

@tiangolo tiangolo reopened this Feb 28, 2023
Repository owner locked and limited conversation to collaborators Feb 28, 2023
@tiangolo tiangolo converted this issue into discussion #7784 Feb 28, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Projects
None yet
Development

No branches or pull requests

2 participants