-
-
Notifications
You must be signed in to change notification settings - Fork 229
better support for merging properties with allOf #1096
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
dbanty
merged 25 commits into
openapi-generators:main
from
eli-bl:issue-1090-merge-allof
Sep 7, 2024
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
89877d6
fix: better support for merging properties with allOf
eli-bl d9becd0
fix "ommitted" typo
eli-bl 8c4ef97
lint
eli-bl b6d9d0a
fix type hinting problems
eli-bl 1fab8ee
Merge branch 'main' into issue-1090-merge-allof
eli-bl 61e015b
another type hinting fix
eli-bl 8722001
add change description
eli-bl ad77302
fix enum property merge logic
eli-bl 99a0c96
Try to improve type hints
dbanty 90ad18b
Use itertools permutations for merge tests
dbanty d81fd5d
Stop checking string attributes that aren't actually used in generate…
dbanty 4a7f603
Remove exceptions, support comparing internal props of lists.
dbanty 770ebc2
Validate all default types
dbanty 00670a0
Update changeset to new expected end-state
dbanty 2e76e02
Merge branch 'main' into issue-1090-merge-allof
dbanty db90641
allow merge of str with date/file/etc., add end-to-end tests
eli-bl 22361fe
add test for merging lists
eli-bl da76b1c
Merge branch 'main' into issue-1090-merge-allof
eli-bl 9cfe04d
Merge branch 'main' into issue-1090-merge-allof
dbanty 3bfaef9
Mention default in changeset
dbanty 4209fb6
Fix merging inline objects
dbanty 2f185fb
Merge branch 'main' into issue-1090-merge-allof
dbanty 1e995b7
fix default computation for merged properties
eli-bl 4046d03
workarounds for value/Value conversion inconsistencies
eli-bl a4f4a20
Merge branch 'main' into issue-1090-merge-allof
dbanty 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
--- | ||
default: minor | ||
--- | ||
|
||
# Improved property-merging behavior with `allOf` | ||
|
||
When using `allOf` to extend a base object type, `openapi-python-client` is now able to handle some kinds of modifications to an existing property that would have previously caused an error: | ||
|
||
- Overriding attributes that do not affect validation, such as `description`. | ||
- Combining properties that this generator ignores, like `maxLength` or `pattern`. | ||
- Combining a generic numeric type with `int` (resulting in `int`). | ||
- Adding a `format` to a string. | ||
- Combining `any` with a specific type (resulting in that specific type). | ||
- Adding or overriding a `default` | ||
|
||
> [!NOTE] | ||
> `pattern` and `max_length` are no longer fields on `StringProperty`, which may impact custom templates. | ||
|
||
This also fixes a bug where properties of inline objects (as opposed to references) were not using the | ||
merge logic, but were simply overwriting previous definitions of the same property. |
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
112 changes: 112 additions & 0 deletions
112
end_to_end_tests/golden-record/my_test_api_client/models/model_with_merged_properties.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,112 @@ | ||
import datetime | ||
from typing import Any, Dict, List, Type, TypeVar, Union | ||
|
||
from attrs import define as _attrs_define | ||
from attrs import field as _attrs_field | ||
from dateutil.parser import isoparse | ||
|
||
from ..models.model_with_merged_properties_string_to_enum import ModelWithMergedPropertiesStringToEnum | ||
from ..types import UNSET, Unset | ||
|
||
T = TypeVar("T", bound="ModelWithMergedProperties") | ||
|
||
|
||
@_attrs_define | ||
class ModelWithMergedProperties: | ||
""" | ||
Attributes: | ||
simple_string (Union[Unset, str]): extended simpleString description Default: 'new default'. | ||
string_to_enum (Union[Unset, ModelWithMergedPropertiesStringToEnum]): Default: | ||
ModelWithMergedPropertiesStringToEnum.A. | ||
string_to_date (Union[Unset, datetime.date]): | ||
number_to_int (Union[Unset, int]): | ||
any_to_string (Union[Unset, str]): Default: 'x'. | ||
""" | ||
|
||
simple_string: Union[Unset, str] = "new default" | ||
string_to_enum: Union[Unset, ModelWithMergedPropertiesStringToEnum] = ModelWithMergedPropertiesStringToEnum.A | ||
string_to_date: Union[Unset, datetime.date] = UNSET | ||
number_to_int: Union[Unset, int] = UNSET | ||
any_to_string: Union[Unset, str] = "x" | ||
additional_properties: Dict[str, Any] = _attrs_field(init=False, factory=dict) | ||
|
||
def to_dict(self) -> Dict[str, Any]: | ||
simple_string = self.simple_string | ||
|
||
string_to_enum: Union[Unset, str] = UNSET | ||
if not isinstance(self.string_to_enum, Unset): | ||
string_to_enum = self.string_to_enum.value | ||
|
||
string_to_date: Union[Unset, str] = UNSET | ||
if not isinstance(self.string_to_date, Unset): | ||
string_to_date = self.string_to_date.isoformat() | ||
|
||
number_to_int = self.number_to_int | ||
|
||
any_to_string = self.any_to_string | ||
|
||
field_dict: Dict[str, Any] = {} | ||
field_dict.update(self.additional_properties) | ||
field_dict.update({}) | ||
if simple_string is not UNSET: | ||
field_dict["simpleString"] = simple_string | ||
if string_to_enum is not UNSET: | ||
field_dict["stringToEnum"] = string_to_enum | ||
if string_to_date is not UNSET: | ||
field_dict["stringToDate"] = string_to_date | ||
if number_to_int is not UNSET: | ||
field_dict["numberToInt"] = number_to_int | ||
if any_to_string is not UNSET: | ||
field_dict["anyToString"] = any_to_string | ||
|
||
return field_dict | ||
|
||
@classmethod | ||
def from_dict(cls: Type[T], src_dict: Dict[str, Any]) -> T: | ||
d = src_dict.copy() | ||
simple_string = d.pop("simpleString", UNSET) | ||
|
||
_string_to_enum = d.pop("stringToEnum", UNSET) | ||
string_to_enum: Union[Unset, ModelWithMergedPropertiesStringToEnum] | ||
if isinstance(_string_to_enum, Unset): | ||
string_to_enum = UNSET | ||
else: | ||
string_to_enum = ModelWithMergedPropertiesStringToEnum(_string_to_enum) | ||
|
||
_string_to_date = d.pop("stringToDate", UNSET) | ||
string_to_date: Union[Unset, datetime.date] | ||
if isinstance(_string_to_date, Unset): | ||
string_to_date = UNSET | ||
else: | ||
string_to_date = isoparse(_string_to_date).date() | ||
|
||
number_to_int = d.pop("numberToInt", UNSET) | ||
|
||
any_to_string = d.pop("anyToString", UNSET) | ||
|
||
model_with_merged_properties = cls( | ||
simple_string=simple_string, | ||
string_to_enum=string_to_enum, | ||
string_to_date=string_to_date, | ||
number_to_int=number_to_int, | ||
any_to_string=any_to_string, | ||
) | ||
|
||
model_with_merged_properties.additional_properties = d | ||
return model_with_merged_properties | ||
|
||
@property | ||
def additional_keys(self) -> List[str]: | ||
return list(self.additional_properties.keys()) | ||
|
||
def __getitem__(self, key: str) -> Any: | ||
return self.additional_properties[key] | ||
|
||
def __setitem__(self, key: str, value: Any) -> None: | ||
self.additional_properties[key] = value | ||
|
||
def __delitem__(self, key: str) -> None: | ||
del self.additional_properties[key] | ||
|
||
def __contains__(self, key: str) -> bool: | ||
return key in self.additional_properties |
9 changes: 9 additions & 0 deletions
9
...ts/golden-record/my_test_api_client/models/model_with_merged_properties_string_to_enum.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,9 @@ | ||
from enum import Enum | ||
|
||
|
||
class ModelWithMergedPropertiesStringToEnum(str, Enum): | ||
A = "a" | ||
B = "b" | ||
|
||
def __str__(self) -> str: | ||
return str(self.value) |
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
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.