Skip to content

Fixes to union properties #241

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
merged 6 commits into from
Nov 18, 2020

Conversation

packyg
Copy link
Contributor

@packyg packyg commented Nov 13, 2020

  • Fix indentation of transforms on inner_propertys of the UnionProperty
    • Was overindented, causing error in generated models
  • Update transform on all templates to take in a declare_type kwarg (default True) that determines if the first declaration of the property should include typedef
    • Used by union_property.pyi's transform to prevent mypy issues when calling the transform of its inner_properties
  • Update construct on all templates to take a initial_value kwarg (defaults as appropriate, usually None, [] for lists)
    • Used by union_property.pyi's construct to prevent invalidly assigning None to the inner_property
  • Update the _parse_<inner_property> function in union_property's construct to use the passed subresource instead of the top level dict from the outer scope

@packyg
Copy link
Contributor Author

packyg commented Nov 13, 2020

CC @dbanty

Also the test failures are coming from the golden-record-custom tests, which seem to be well out of date

@codecov
Copy link

codecov bot commented Nov 13, 2020

Codecov Report

Merging #241 (15f1284) into main (c858ee9) will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff            @@
##              main      #241   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           46        46           
  Lines         1302      1302           
=========================================
  Hits          1302      1302           

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update c858ee9...15f1284. Read the comment docs.

Copy link
Collaborator

@dbanty dbanty left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome work, thanks! Those template changes should help along our eventual goal of having a stable template "API" for custom templates.

There's one change in the enum template that I think needs to be reverted.

As for the custom-e2e stuff, it doesn't get regenerated with the normal poetry run task regen, I made it a separate poetry run task regen_custom when testing the initial custom template stuff. Though there's really no reason that can't just be part of regen. If you run poetry run task re it will regen both and run the e2e test.

Comment on lines +20 to +23
elif isinstance(self.a_property, AnEnum):
a_property = UNSET
if not isinstance(self.a_property, Unset):
a_property = self.a_property
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is weird, I'll try to find the place in the template that causes it. Obviously no need to double check isinstance on the same property, we already know it's AnEnum.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see where your changes would have caused this, so it was probably an existing issue with Unions/UNSET (I know there is at least one issue related to that). The inner_property is probably being set as not required which makes the inner constructor redo the check. I can handle that in a future version.

@staticmethod
def from_dict(d: Dict[str, Any]) -> "ModelWithUnionProperty":
def _parse_a_property(data: Any) -> Union[Unset, AnEnum, AnIntEnum]:
data = None if isinstance(data, Unset) else data
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why None this instead of checking for Unset further down? Less template code when nullables are involved maybe?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this was to be able to reuse construct from template for the inner property without further modifying.

Most of the constructs just check if {{ source }} is not None and don't account for UNSETs.

I'll look a bit more to see if there is a better way.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there's not an easy fix it's not a huge deal. If you want to just regen the custom template e2e test so the checks pass I'll merge this.

Comment on lines +22 to 25
{{ destination }} = {{ source }} if {{ source }} else None
{% else %}
{{ destination }} = {{ source }}.value
{{ destination }} = {{ source }}
{% endif %}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the .values here are still necessary when converting this to JSON, unless HTTPX automatically converts Enums to their values?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was a typing issue.

Mypy complained when I generated the model_with_union_property

a_property: Union[Unset, AnEnum, AnIntEnum]
try:
	a_property = UNSET
	if data is not None:
		a_property = AnEnum(data).value
# ...

There was a type error, saying I was providing a str to a var typed as Union[Unset, AnEnum, AnIntEnum]

FWIW, json.dumps does convert these enums to their primitive values (both (str, Enum) and IntEnum types). I'm assuming that's what httpx uses

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I forgot that we inherit from (str, Enum) instead of just Enum for this exact reason. So this is great then!

@dbanty dbanty added this to the 0.7.0 milestone Nov 14, 2020
@packyg packyg requested a review from dbanty November 18, 2020 18:12
@dbanty dbanty merged commit 103acab into openapi-generators:main Nov 18, 2020
dbanty added a commit that referenced this pull request Nov 18, 2020
@eli-bl eli-bl deleted the union-property-fixes branch November 26, 2024 22:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants