-
|
Hey, I'm not very well versed in OpenApi and swagger, redoc, so I have a question. I used FastApi in a project, it's a pretty awesome tool. But suppose I have a class like this that I use in a response: class EnumBasedClass(str, Enum): class OtherGenericType(BaseModel): class ExampleClass(BaseModel): The resultant docs looks something like this: ResponseSchema And Example response looks like this: Would it be possible to substitute "property name*" with actual values from "EnumBasedClass" |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments
-
|
Check this (
https://gitlab.com/euri10/euri10_fastapi_base/blob/master/backend/app/models/users_settings.py#L40
)
if I got correctly what you want to do you need use_enum_values
Le ven. 6 sept. 2019 à 12:28 PM, mjedrzejewski <notifications@github.com> a
écrit :
… Hey,
I'm not very well versed in OpenApi and swagger, redoc, so I have a
question.
I used FastApi in a project, it's a pretty awesome tool. But suppose I
have a class like this that I use in a response:
class EnumBasedClass(str, Enum):
ENUM1: str = "ENUM1"
ENUM2: str = "ENUM2"
ENUM3: str = "ENUM3"
class OtherGenericType(BaseModel):
field1: str = "field1"
field2: str = "field2"
field3: str = "field3"
class ExampleClass(BaseModel):
some_field: Dict[EnumBasedClass, OtherGenericType]
The resultant docs looks something like this:
ResponseSchema
- property name* object ( OtherGenericType )
- field1 string
- field2 string
- field3 string
And Example response looks like this:
{
"property1": {},
"property2": {}
}
Would it be possible to substitute "property name*" with actual values
from "EnumBasedClass"
or would this be some abuse of OpenApi. Or is there some better type to
represent this case?
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#509>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAINSPW65ALVCQ33DFM3IC3QIIWFNANCNFSM4IUHP4YA>
.
|
Beta Was this translation helpful? Give feedback.
-
|
Thanks for the response.This does not solve my issue. Please see attached script for a minimum example. If you run the script with # uvicorn main:app --reload (please remember to rename txt to py), Is it possible to fix the documentation page so that it shows actual enum values instead of "property1" in the examples? I am attaching a screenshot to hopefully illustrate the issue. |
Beta Was this translation helpful? Give feedback.
-
|
@malsumis What I understand you want to achieve is to define that the keys of a JSON object (a That's not really supported by OpenAPI (nor JSON Schema, I think). What I imagine you want to achieve could be done like: from fastapi import FastAPI
from starlette.responses import JSONResponse, Response
from starlette.status import *
from typing import Set, Dict, List
from enum import Enum
from pydantic import BaseModel
class EnumBasedInfo(str, Enum):
ENUM1: str = 'ENUM1'
ENUM2: str = 'ENUM2'
ENUM3: str = 'ENUM3'
ENUM4: str = 'ENUM4'
class ProcessingRequest(BaseModel):
request1: str
request2: str
enums: Set[EnumBasedInfo]
class OtherClassInfo(BaseModel):
otherinfo1: str
otherinfo2: str
otherinfo3: str
otherinfo4: str
class CompoundReturn(BaseModel):
ENUM1: OtherClassInfo = None
ENUM2: OtherClassInfo = None
ENUM3: OtherClassInfo = None
ENUM4: OtherClassInfo = None
class ResultInfo(BaseModel):
info1: int
info2: int
info3: int
compound_return_value: CompoundReturn = None
class ProcessingResult(BaseModel):
result_msg: str
result: List[ResultInfo] = None
app = FastAPI(redoc_url='/documentation')
@app.post("/", response_model=ProcessingResult, # type: ignore
responses={HTTP_200_OK: {"model": ProcessingResult, "description": "Some Description"}})
def process_request(processing_request: ProcessingRequest) -> JSONResponse:
return JSONResponse(status_code=HTTP_200_OK) |
Beta Was this translation helpful? Give feedback.
-
|
Assuming the original issue was solved, it will be automatically closed now. But feel free to add more comments or create new issues. |
Beta Was this translation helpful? Give feedback.

@malsumis What I understand you want to achieve is to define that the keys of a JSON object (a
dict) are one of the values in the enum, right?That's not really supported by OpenAPI (nor JSON Schema, I think).
What I imagine you want to achieve could be done like: