Fastapi: Adding response parameter to endpoint function breaks Swagger UI

Created on 23 Jul 2020  路  3Comments  路  Source: tiangolo/fastapi

First check

  • [x] I added a very descriptive title to this issue.
  • [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.
  • [x] 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. (not sure how to determine this)
  • [ ] I already checked if it is not related to FastAPI but to Swagger UI. (not sure how to determine this)
  • [ ] I already checked if it is not related to FastAPI but to ReDoc. (not sure how to determine this)
  • [x] 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.

Problem

Following tutorial here: https://fastapi.tiangolo.com/advanced/response-change-status-code/

Adding response as a parameter to an endpoint completely breaks swagger ui, server throws

...
  File "/Users/atartanian/Development/carmera/carmera_take_home/.venv/lib/python3.8/site-packages/fastapi/openapi/utils.py", line 101, in get_openapi_operation_parameters
    "schema": field_schema(
  File "pydantic/schema.py", line 177, in pydantic.schema.field_schema
  File "pydantic/schema.py", line 712, in pydantic.schema.encode_default
  File "pydantic/json.py", line 62, in pydantic.json.pydantic_encoder
TypeError: Object of type 'type' is not JSON serializable

minimally reproducible with the following code:

from fastapi import FastAPI, Response

app = FastAPI()

@app.get("/test", status_code=200)
async def test(
    response=Response
):
    return {"message": "tada"}

Environment

  • OS: macOS
  • FastAPI version: 0.60.1
  • Python version 3.8.2

Additional context

Screen Shot 2020-07-23 at 5 21 55 PM

question

Most helpful comment

Fix: response=Response -> response: Response.

from fastapi import FastAPI, Response

app = FastAPI()

@app.get("/test", status_code=200)
async def test(
    response: Response
):
    return {"message": "tada"}

All 3 comments

Fix: response=Response -> response: Response.

from fastapi import FastAPI, Response

app = FastAPI()

@app.get("/test", status_code=200)
async def test(
    response: Response
):
    return {"message": "tada"}

image

Thanks for the help here @Kludex ! :clap: :bow:

Thanks for reporting back and closing the issue @atartanian :+1: ...that pic was very funny :joy:

Was this page helpful?
0 / 5 - 0 ratings