Skip to content

Improvements to schema and enum naming #31

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 4 commits into from
Apr 25, 2020

Conversation

acgray
Copy link

@acgray acgray commented Apr 12, 2020

  • name schemas using the declaring property name if no "title" property is available. Currently the generator breaks when processing cases such as :

    CoolFunModel:
        type: object
        properties: {...}
    

    This will use CoolFunModel as the schema name in these cases.

  • Conversely, for enum properties, if a title property is available in the property schema then ths this is used to name the enum. This also addresses Allow defining Enums ahead of time #21 - in the case where repeated property enums are derived from a single enum in a typed language (e.g. Java), if they have the same title they will be merged back into a single Python enum in the client.

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, thank you! A couple comments to address then some exciting improvements to merge 🥳

Comment on lines +169 to +180
if "properties" in d:
for key, value in d["properties"].items():
required = key in required_set
p = property_from_dict(name=key, required=required, data=value)
if required:
required_properties.append(p)
else:
optional_properties.append(p)
if isinstance(p, (ReferenceListProperty, EnumListProperty, RefProperty, EnumProperty)) and p.reference:
# don't add an import for self-referencing schemas
if p.reference.class_name != ref.class_name:
relative_imports.add(import_string_from_reference(p.reference))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Just a personal preference, I try to avoid multiple layers as much as possible. Something about "cognitive complexity" I read in a book 🧠🤓. Feel free to disagree and leave as is.

Suggested change
if "properties" in d:
for key, value in d["properties"].items():
required = key in required_set
p = property_from_dict(name=key, required=required, data=value)
if required:
required_properties.append(p)
else:
optional_properties.append(p)
if isinstance(p, (ReferenceListProperty, EnumListProperty, RefProperty, EnumProperty)) and p.reference:
# don't add an import for self-referencing schemas
if p.reference.class_name != ref.class_name:
relative_imports.add(import_string_from_reference(p.reference))
for key, value in d.get("properties", {}).items():
required = key in required_set
p = property_from_dict(name=key, required=required, data=value)
if required:
required_properties.append(p)
else:
optional_properties.append(p)
if (
isinstance(p, (ReferenceListProperty, EnumListProperty, RefProperty, EnumProperty))
and p.reference
and p.reference.class_name != ref.class_name # don't add an import for self-referencing schemas
):
relative_imports.add(import_string_from_reference(p.reference))


if class_name in class_overrides:
return class_overrides[class_name]

return Reference(class_name=class_name, module_name=stringcase.snakecase(ref_value),)
return Reference(class_name=class_name, module_name=stringcase.snakecase(class_name),)
Copy link
Collaborator

Choose a reason for hiding this comment

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

This will conflict with #29 most likely, so whichever one gets merged first will trigger an update in the other. Just to be aware.

Comment on lines -280 to +284
models_dir.__truediv__.side_effect = lambda x: module_paths[x]

def models_dir_get(x):
return module_paths[x]

models_dir.__truediv__.side_effect = models_dir_get
Copy link
Collaborator

Choose a reason for hiding this comment

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

I assume this change was because it was difficult to read. Maybe I should be using itemgetter for this, would that be better / more Pythonic you think?

Pretty sure I do this in several tests.

@codecov
Copy link

codecov bot commented Apr 25, 2020

Codecov Report

Merging #31 into master will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##            master       #31   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            8         9    +1     
  Lines          532       573   +41     
=========================================
+ Hits           532       573   +41     
Impacted Files Coverage Δ
openapi_python_client/__init__.py 100.00% <100.00%> (ø)
openapi_python_client/openapi_parser/openapi.py 100.00% <100.00%> (ø)
openapi_python_client/openapi_parser/properties.py 100.00% <100.00%> (ø)
openapi_python_client/openapi_parser/reference.py 100.00% <100.00%> (ø)
openapi_python_client/openapi_parser/responses.py 100.00% <100.00%> (ø)
openapi_python_client/utils.py 100.00% <100.00%> (ø)

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 5583d86...640d111. Read the comment docs.

@dbanty dbanty merged commit 84c8c23 into openapi-generators:master Apr 25, 2020
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