Repository navigation
Share Python legacy model helpers across generated models #24730
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
wing328
merged 4 commits into
OpenAPITools:master
from
Abhinav0905:agent/share-python-legacy-model-helpers
Oct 19, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c62a19e
Share Python legacy model helpers
0c1bee9
Merge remote-tracking branch 'upstream/master' into agent/share-pytho…
4413060
Merge remote-tracking branch 'upstream/master' into agent/share-pytho…
c100a24
Fix legacy helper generation edge cases
Abhinav0905 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
5 changes: 5 additions & 0 deletions
5
modules/openapi-generator/src/main/resources/python/_legacy_model_helpers.mustache
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| {{>partial_header}} | ||
|
|
||
| from typing import Any | ||
|
|
||
| {{>_legacy_model_dict_helpers}} |
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
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
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
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
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
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
80 changes: 80 additions & 0 deletions
80
...others/python-legacy-model-dictionaries/legacy_model_dict_client/_legacy_model_helpers.py
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| """ | ||
| Legacy model dictionaries | ||
|
|
||
| No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator) | ||
|
|
||
| The version of the OpenAPI document: 1.0.0 | ||
| Generated by OpenAPI Generator (https://openapi-generator.tech) | ||
|
|
||
| Do not edit the class manually. | ||
| """ # noqa: E501 | ||
|
|
||
|
|
||
| from typing import Any | ||
|
|
||
| _OPENAPI_GENERATOR_TO_DICT = "_openapi_generator_to_dict" | ||
|
|
||
|
|
||
| def _get_openapi_to_dict(value: Any) -> Any: | ||
| # Reciprocal function references preserve inherited generated methods | ||
| # without reserving model member names. Checking both directions also | ||
| # rejects overrides whose decorators copy function attributes. | ||
| to_dict = getattr(value, "to_dict", None) | ||
| type_to_dict = getattr(type(value), "to_dict", None) | ||
| if ( | ||
| not callable(to_dict) | ||
| or getattr(to_dict, "__func__", None) is not type_to_dict | ||
| ): | ||
| return None | ||
|
|
||
| openapi_to_dict = getattr( | ||
| type_to_dict, _OPENAPI_GENERATOR_TO_DICT, None | ||
| ) | ||
| if ( | ||
| not callable(openapi_to_dict) | ||
| or getattr( | ||
| openapi_to_dict, | ||
| _OPENAPI_GENERATOR_TO_DICT, | ||
| None, | ||
| ) is not type_to_dict | ||
| ): | ||
| return None | ||
| return openapi_to_dict | ||
|
|
||
|
|
||
| def _to_legacy_item(value: Any, serialize: bool) -> Any: | ||
| to_dict = getattr(value, "to_dict", None) | ||
| if _get_openapi_to_dict(value) is not None and callable(to_dict): | ||
| return to_dict(serialize=serialize) | ||
| if callable(to_dict): | ||
| return to_dict() | ||
| return value | ||
|
|
||
|
|
||
| def _to_legacy_value(value: Any, serialize: bool) -> Any: | ||
| # python-legacy converted only immediate list elements or dictionary values: | ||
| # https://github.com/OpenAPITools/openapi-generator/blob/c84b949df1a9ec04ba75989cb49b45c940c2f694/modules/openapi-generator/src/main/resources/python-legacy/model.mustache#L203-L231 | ||
| if isinstance(value, list): | ||
| return [_to_legacy_item(item, serialize) for item in value] | ||
| if isinstance(value, dict): | ||
| return { | ||
| key: _to_legacy_item(item, serialize) | ||
| for key, item in value.items() | ||
| } | ||
| return _to_legacy_item(value, serialize) | ||
|
|
||
|
|
||
| def _to_openapi_value(value: Any) -> Any: | ||
| if isinstance(value, list): | ||
| return [_to_openapi_value(item) for item in value] | ||
| if isinstance(value, dict): | ||
| return {key: _to_openapi_value(item) for key, item in value.items()} | ||
|
|
||
| to_openapi_dict = _get_openapi_to_dict(value) | ||
| if to_openapi_dict is not None: | ||
| return to_openapi_dict(value) | ||
|
|
||
| to_dict = getattr(value, "to_dict", None) | ||
| if callable(to_dict): | ||
| return to_dict() | ||
| return value |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.