Skip to content

Bug: Response[None] results in a different OpenAPI response specificiation than None #4598

@spietras

Description

@spietras

Description

Following up after a long time on my other two issues about Response[None] return type annotation (#2914, #3640), I have found another issue.

If you use None as the return type, then the OpenAPI response specification will not include content, which is the proper thing to do in case of responses with no content, which is usually what you want to achieve when using None as the return type.

But when you use Response[None] as the return type, then the OpenAPI response specification will include content with a JSON schema describing null value. Which is simply not true, as the actual behaviour is not to respond with null, but respond with no data.

I believe simply this piece of code didn't take using Response[None] into consideration:

https://github.com/litestar-org/litestar/blob/v2.20.0/litestar/_openapi/responses.py#L121-L122

URL to code causing the issue

No response

MCVE

import json

from litestar import Litestar, Response, get


@get("/foo", status_code=204)
async def foo() -> None:
    return None


@get("/bar", status_code=204)
async def bar() -> Response[None]:
    return Response(None)


app = Litestar(
    [foo, bar],
    on_startup=[
        lambda app: print(json.dumps(app.openapi_schema.to_schema(), indent=2))
    ],
)

Steps to reproduce

Run the above and observe the output (shortened here):

{
  "openapi": "3.1.0",
  "paths": {
    "/foo": {
      "get": {
        "responses": {
          "204": {
            "description": "Request fulfilled, nothing follows"
          }
        }
      }
    },
    "/bar": {
      "get": {
        "responses": {
          "204": {
            "description": "Request fulfilled, nothing follows",
            "content": {
              "application/json": {
                "schema": {
                  "type": "null"
                }
              }
            }
          }
        }
      }
    }
  }
}

Screenshots

No response

Logs

No response

Litestar Version

2.20.0

Platform

  • Linux
  • Mac
  • Windows
  • Other (Please specify in the description above)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Bug 🐛This is something that is not working as expected

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions