[Python] Added Ability to Initialize Models by Passing in Dictionaries - #11125
Merged
spacether merged 7 commits intoMar 9, 2022
Conversation
ckoegel
marked this pull request as ready for review
December 14, 2021 21:28
ckoegel
marked this pull request as draft
December 15, 2021 16:35
ckoegel
marked this pull request as ready for review
December 15, 2021 18:27
6 tasks
spacether
approved these changes
Mar 9, 2022
spacether
left a comment
Contributor
There was a problem hiding this comment.
This looks good. Thank you for your PR!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Python models created from openapi schema do not accept positional objects as arguments. More information about what the goal of this PR is can be found at this issue. This was mostly caused by the
invalid_pos_args.mustachefile instantly raising an error if positional arguments were passed in. This has been updated to accept dictionaries as a valid positional argument. The dictionaries are merged into the kwargs dictionary that the function uses to initialize. This allows users to pass in dictionaries to create models, which was previously allowed with the**operator, but this allows for an alternative method of creating these objects.The main function of this PR allows for the creation of nested objects. The
setattrfunction called when initializing the model validates the type of each kwarg before setting the attribute, meaning if nested dictionaries are passed in, thevalidate_and_convert_typesfunction will raise a type error if those nested dictionaries need to be used in the creation of another object since their types do not match. The solution for this was already partially implemented, since thevalidate_and_convert_typesfunction has logic to attempt to convert non-matching attribute types, but this needed to be modified to allow for attempting to convert dictionaries. Once the logic was added to allow a dictionary to be converted, the logic withinattempt_convert_itemautomatically uses the dictionary to initialize a new instance of its respective model. This allows for any amount of nested dictionaries to be passed into a model initialization and their corresponding models be created. Errors will still be thrown if the positional arguments are incorrectly formatted, or if the model that needs to be created from a dictionary does not match the dictionary's contents.This can be validated by using this spec and this command:
java -jar ./modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -g python -i messaging.json -o sdk/in the root of the project to generate an SDK. Then running the FastApi app below and making a POST to the endpoint with the body below should successfully create both a BandwidthCallbackMessage and a BandwidthMessage, which can be seen in the output of the FastApi app.FastApi App:
Request Body:
{ "time": "2021-12-14T19:48:29.806Z", "type": "message-received", "to": "+19195551234", "description": "Incoming message received", "message": { "id": "id-abcd", "owner": "+19195551234", "applicationId": "app-id-abcd", "time": "2021-12-14T19:48:29.641Z", "segmentCount": 1, "direction": "in", "to": [ "+19195551234" ], "from": "+19195554321", "text": "Hello World" } }PR checklist
This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
These must match the expectations made by your contribution.
You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example
./bin/generate-samples.sh bin/configs/java*.For Windows users, please run the script in Git BASH.
master(5.3.0),6.0.x@taxpon (2017/07) @frol (2017/07) @mbohlool (2017/07) @cbornet (2017/09) @kenjones-cisco (2017/11) @tomplus (2018/10) @Jyhess (2019/01) @arun-nalla (2019/11) @spacether (2019/11)