Skip to content

Feat/detect discriminator bnch 49688 #143

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 15 commits into from
Oct 6, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions openapi_python_client/parser/properties/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,11 @@ def build_model_property(
optional_properties.append(prop)
relative_imports.update(prop.get_imports(prefix=".."))

discriminator_mappings: Dict[str, Property] = {}
if data.discriminator is not None:
for k, v in (data.discriminator.mapping if data.discriminator else {}).items():

Choose a reason for hiding this comment

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

Can we do this since the above line checks data.discriminator is not None?

Suggested change
for k, v in (data.discriminator.mapping if data.discriminator else {}).items():
for k, v in data.discriminator.mapping.items():

Copy link
Author

Choose a reason for hiding this comment

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

oh, good catch, thanks!

Copy link
Author

@straz straz Sep 20, 2022

Choose a reason for hiding this comment

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

oh, good catch, thanks!

Well, hmm. I think I had that there because data.discriminator might be {"mapping": None}.

Copy link

Choose a reason for hiding this comment

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

If {"mapping": None} then this will still return None since the check is on data.discriminator right?
May be worth a closer look.

Copy link
Author

@straz straz Sep 21, 2022

Choose a reason for hiding this comment

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

I have something like this in mind:

from openapi_python_client.schema import Discriminator, Schema

data = Schema(discriminator=Discriminator(propertyName="resourceType"))

>>> data.discriminator is None
False
>>> data.discriminator.mapping is None
True
>>> [(k,v) for k,v in data.discriminator.mapping.items()]
*** AttributeError: 'NoneType' object has no attribute 'items'

Copy link

@dtkav dtkav Sep 21, 2022

Choose a reason for hiding this comment

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

Your intent makes sense.
To make your code match your intent I think you need to change the code from:

(data.discriminator.mapping if data.discriminator else {}).items():

to:

(data.discriminator.mapping if data.discriminator.mapping is not None else {}).items():

or:

(data.discriminator.mapping or {}).items():

alternatively you could replace both checks with EAFP

try:
    for k, v in data.discriminator.mapping.items():
        discriminator_mappings[k] = Reference.from_ref(v)
except AttributeError:
    # Either data.discriminator or data.discriminator.mapping was not set
    pass

Copy link
Author

Choose a reason for hiding this comment

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

You are right indeed, thanks! hopefully fixed this.

discriminator_mappings[k] = Reference.from_ref(v)

additional_properties: Union[bool, Property, PropertyError]
if data.additionalProperties is None:
additional_properties = True
Expand Down Expand Up @@ -347,6 +352,8 @@ def build_model_property(
description=data.description or "",
default=None,
nullable=data.nullable,
discriminator_property=data.discriminator.propertyName if data.discriminator else None,
discriminator_mappings=discriminator_mappings,
required=required,
name=name,
additional_properties=additional_properties,
Expand Down
8 changes: 7 additions & 1 deletion openapi_python_client/parser/properties/model_property.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

from collections.abc import Iterable
from typing import TYPE_CHECKING, ClassVar, Dict, List, Set, Union
from typing import TYPE_CHECKING, ClassVar, Dict, List, Optional, Set, Union

import attr

Expand All @@ -22,6 +22,9 @@ class ModelProperty(Property):
references: List[oai.Reference]
required_properties: List[Property]
optional_properties: List[Property]
discriminator_property: Optional[str]
discriminator_mappings: Dict[str, Property]

description: str
relative_imports: Set[str]
additional_properties: Union[bool, Property]
Expand Down Expand Up @@ -71,6 +74,9 @@ def resolve_references(
self.optional_properties.append(prop)
self.relative_imports.update(prop.get_imports(prefix=".."))

for _, value in self.discriminator_mappings.items():
self.relative_imports.add(f"from ..models.{value.module_name} import {value.class_name}")

return schemas

def get_base_type_string(self) -> str:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "openapi-python-client"
# Our versions have diverged and have no relation to upstream code changes
# Henceforth, openapi-python-package will be maintained internally
version = "1.0.1"
version = "1.1.0"
description = "Generate modern Python clients from OpenAPI"
repository = "https://github.com/triaxtec/openapi-python-client"
license = "MIT"
Expand Down