-
-
Notifications
You must be signed in to change notification settings - Fork 9k
Problem with JSON strings format in the swagger UI generated docs #4514
Description
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
from fastapi import FastAPI
from pydantic import BaseModel
class Model(BaseModel):
json_field: str
app = FastAPI()
#A simple storage, just to illustrate
database = []
@app.post("/")
def create_entry(data: Model):
database.append(data)
return dataDescription
Suppose that in my post request I want to inject data that follows a pydantic model in which one of the fields is a json string (see the example code).
In the Swagger UI generated docs, if I want to pass a json that represents a dictionnary I'm obliged to pass the json string field in the format "{'key1': value1, 'key2': value2, ...}".
If I request the database from javascript, for example, and I try to parse the json using JSON.parse, I will get the following error:
SyntaxError: Unexpected token ' in JSON at position 1
because this javascript method supposes that all internal quotes in the json are double quotes.
It's not a serious issue for me, because I can correct the problem by doing the post request through python. But it would be handy for tests if the post request through Swagger UI supported a json string in the format '{"key1": value1, "key2": value2, ...}'.
Is it achievable through FastAPI ? Or is it a Swagger UI issue ? I'm a bit lost
Operating System
Linux
Operating System Details
No response
FastAPI Version
0.73.0
Python Version
3.9.5
Additional Context
No response