Replies: 2 comments 2 replies
-
We can see from the openapi.json "/tropical": {
"get": {
"summary": "I Want A Tropical Fruit",
"operationId": "i_want_a_tropical_fruit_tropical_get",
"parameters": [
{
"name": "tropical_fruit",
"in": "query",
"required": true,
"schema": {
"const": "pineapple",
"title": "Tropical Fruit"
}
}
],
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {}
}
}
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
}
}
}
}
}
} That a literal of length one is converted to a const. I think they should still remain a swagger enum. |
Beta Was this translation helpful? Give feedback.
2 replies
-
Here is a little workaround to recursively replace all occurences of the import copy
import json
from fastapi import FastAPI
from fastapi.openapi.utils import get_openapi
api = FastAPI()
def walk_and_replace(node):
for item in node.values():
if isinstance(item, dict):
if "const" in item:
item["enum"] = [item["const"]]
item["type"] = "string"
del item["const"]
walk_and_replace(item)
def custom_openapi():
if api.openapi_schema:
return api.openapi_schema
openapi_schema = get_openapi(
title=api.title,
version=api.version,
openapi_version="3.0.2",
description=api.description,
routes=api.routes,
)
walk_and_replace(openapi_schema)
api.openapi_schema = openapi_schema
return api.openapi_schema
api.openapi = custom_openapi |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
First Check
Commit to Help
Example Code
Description
Expected :
Current :
Operating System
Linux, Windows, macOS
Operating System Details
No response
FastAPI Version
0.100.1
Python Version
3.11.3
Additional Context
No response
Beta Was this translation helpful? Give feedback.
All reactions