From fba7493042cde54c059989ffb2ccccde65c51293 Mon Sep 17 00:00:00 2001 From: Marcelo Trylesinski Date: Tue, 10 Jan 2023 13:45:18 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Ignore=20Response=20classes=20on?= =?UTF-8?q?=20return=20annotation=20(#5855)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- fastapi/routing.py | 7 ++- ...est_response_model_as_return_annotation.py | 47 +++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/fastapi/routing.py b/fastapi/routing.py index 8c73b954f..f131fa903 100644 --- a/fastapi/routing.py +++ b/fastapi/routing.py @@ -42,6 +42,7 @@ from fastapi.utils import ( from pydantic import BaseModel from pydantic.error_wrappers import ErrorWrapper, ValidationError from pydantic.fields import ModelField, Undefined +from pydantic.utils import lenient_issubclass from starlette import routing from starlette.concurrency import run_in_threadpool from starlette.exceptions import HTTPException @@ -356,7 +357,11 @@ class APIRoute(routing.Route): self.path = path self.endpoint = endpoint if isinstance(response_model, DefaultPlaceholder): - response_model = get_typed_return_annotation(endpoint) + return_annotation = get_typed_return_annotation(endpoint) + if lenient_issubclass(return_annotation, Response): + response_model = None + else: + response_model = return_annotation self.response_model = response_model self.summary = summary self.response_description = response_description diff --git a/tests/test_response_model_as_return_annotation.py b/tests/test_response_model_as_return_annotation.py index f2056fecd..5d347ec34 100644 --- a/tests/test_response_model_as_return_annotation.py +++ b/tests/test_response_model_as_return_annotation.py @@ -2,6 +2,7 @@ from typing import List, Union import pytest from fastapi import FastAPI +from fastapi.responses import JSONResponse, Response from fastapi.testclient import TestClient from pydantic import BaseModel, ValidationError @@ -237,6 +238,16 @@ def no_response_model_annotation_union_return_model2() -> Union[User, Item]: return Item(name="Foo", price=42.0) +@app.get("/no_response_model-annotation_response_class") +def no_response_model_annotation_response_class() -> Response: + return Response(content="Foo") + + +@app.get("/no_response_model-annotation_json_response_class") +def no_response_model_annotation_json_response_class() -> JSONResponse: + return JSONResponse(content={"foo": "bar"}) + + openapi_schema = { "openapi": "3.0.2", "info": {"title": "FastAPI", "version": "0.1.0"}, @@ -789,6 +800,30 @@ openapi_schema = { }, } }, + "/no_response_model-annotation_response_class": { + "get": { + "summary": "No Response Model Annotation Response Class", + "operationId": "no_response_model_annotation_response_class_no_response_model_annotation_response_class_get", + "responses": { + "200": { + "description": "Successful Response", + "content": {"application/json": {"schema": {}}}, + } + }, + } + }, + "/no_response_model-annotation_json_response_class": { + "get": { + "summary": "No Response Model Annotation Json Response Class", + "operationId": "no_response_model_annotation_json_response_class_no_response_model_annotation_json_response_class_get", + "responses": { + "200": { + "description": "Successful Response", + "content": {"application/json": {"schema": {}}}, + } + }, + } + }, }, "components": { "schemas": { @@ -1049,3 +1084,15 @@ def test_no_response_model_annotation_union_return_model2(): response = client.get("/no_response_model-annotation_union-return_model2") assert response.status_code == 200, response.text assert response.json() == {"name": "Foo", "price": 42.0} + + +def test_no_response_model_annotation_return_class(): + response = client.get("/no_response_model-annotation_response_class") + assert response.status_code == 200, response.text + assert response.text == "Foo" + + +def test_no_response_model_annotation_json_response_class(): + response = client.get("/no_response_model-annotation_json_response_class") + assert response.status_code == 200, response.text + assert response.json() == {"foo": "bar"}