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

null values are discarded from OpenAPI examples #5559

Closed
9 tasks done
WilliamDEdwards opened this issue Oct 30, 2022 · 0 comments
Closed
9 tasks done

null values are discarded from OpenAPI examples #5559

WilliamDEdwards opened this issue Oct 30, 2022 · 0 comments

Comments

@WilliamDEdwards
Copy link

WilliamDEdwards commented Oct 30, 2022

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn't find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google "How to X in FastAPI" and didn't find any information.
  • 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.
  • I already checked if it is not related to FastAPI but to Swagger UI.
  • I already checked if it is not related to FastAPI but to ReDoc.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

Taken from https://fastapi.tiangolo.com/advanced/additional-responses/

from fastapi import FastAPI

import json

app = FastAPI()

@app.get("/", responses={200: {"content": {"application/json": {"example": {"absent": None, "present": "value"}}}}})
def read_root():
    return {"absent": None, "present": "value"}

print(json.dumps(app.openapi()))

Description

  • Add an example to the OpenAPI spec (using example or examples)
  • Set the value to None
  • The example is absent

The code above outputs:

{"openapi": "3.0.2", "info": {"title": "FastAPI", "version": "0.1.0"}, "paths": {"/": {"get": {"summary": "Read Root", "operationId": "read_root__get", "responses": {"200": {"description": "Successful Response", "content": {"application/json": {"schema": {}, "example": {"present": "value"}}}}}}}}}

As you can see, the absent example is absent.

Operating System

Linux

Operating System Details

No response

FastAPI Version

0.73.0

Python Version

3.8.9

Additional Context

openapi.utils.get_openapi has this line:

return jsonable_encoder(OpenAPI(**output), by_alias=True, exclude_none=True)  # type: ignore

exclude_none could cause the issue.

When setting it to True, OpenAPI generation fails with the following error:

n is null
@WilliamDEdwards WilliamDEdwards added the question Question or problem label Oct 30, 2022
Repository owner locked and limited conversation to collaborators Feb 28, 2023
@tiangolo tiangolo converted this issue into discussion #8401 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