-
-
Notifications
You must be signed in to change notification settings - Fork 263
How to properly create a oneof in Python? #1200
-
Hi,
I have some issues creating a oneof in Python.
I'd like to implement a oneof with three options:
get:
operationId: doSomething
x-bidistream: true
requestBody:
content:
application/json:
schema:
oneOf:
- $ref: '../components/schemas/Option1.yaml'
- $ref: '../components/schemas/Option2.yaml'
- $ref: '../components/schemas/Option3.yaml'
The generator correctly generates a request for this comprising all these options:
class doSomethingRequest(BaseModel):
oneof_schema_1_validator: Optional[Option1] = None
oneof_schema_2_validator: Optional[Option2] = None
oneof_schema_3_validator: Optional[Option3] = None
The websocket endpoint looks like this:
async def do_something(self, client_request_generator: Callable[[AsyncGenerator[DoSomethingResponse, None]], AsyncGenerator[DoSomethingRequest, None]]) -> None: # noqa: E501
My async generator looks like this:
async def faulty_request_generator(
response_generator: AsyncGenerator[Models.DoSomethingResponse, None],
request: Union[Models.Option1, Models.Option2, Models.Option3],
) -> AsyncGenerator[Models.DoSomethingRequest, None]:
yield Models.DoSomethingRequest(request)
This doesn't seem to work, because if I wrap my request in Models.DoSomethingRequest in the yield command, the json that is sent seems to ignore the oneof-character of this message. Somewhere pydantics model_dump_json is called on this message which will also include oneof_schema_1_validator etc.
If I just call yield request, everything works, but MyPy complains that the types don't match. I could disable this warning, but somehow it feels wrong.
I noticed that there is a to_json method of the DoSomethingRequest-class, which seems to do the correct thing, but it doesn't seem to get called. Do I have to setup something to make this work?
Thanks and best regards
oz
Beta Was this translation helpful? Give feedback.