Using openapi generator for python, I get a bunch of generated models, which I can instantiate like so:
> yoda = Jedi(name="Yoda", light_saber=LightSaber(colour="green"))
I can convert these to a dict nice and easily using the .to_dict()
method:
> yoda.to_dict()
{"name": "Yoda", "light_saber": {"colour": "green"}}
However I'm not sure how I can deserialise from a dict back to the appropriate model.
I've tried the following and get a type error:
> Jedi(**yoda_dict)
...
my_client.exceptions.ApiTypeError: Invalid type for variable 'light_saber'.
Required value type is LightSaber and passed type was dict at ['light_saber']
I also get the same error with Jedi._from_openapi_data(**yoda_dict)
Is there a way to convert from a dict to a typed openapi generated model?
Turns out openapi only tries to convert types if you pass in the _configuration
property. I was able to deserialise successfully with
from my_client.configuration import Configuration
Jedi(**yoda_dict, _configuration=Configuration())
Note that I also hit this bug as I'm using allOf
in my schemas. So also had to downgrade to openapi generator 5.1.1.