-
-
Notifications
You must be signed in to change notification settings - Fork 229
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -154,24 +154,32 @@ class Schema: | |||||||||||||||||||||||||||||||||||||||||||||||||||
relative_imports: Set[str] | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
@staticmethod | ||||||||||||||||||||||||||||||||||||||||||||||||||||
def from_dict(d: Dict[str, Any], /) -> Schema: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
""" A single Schema from its dict representation """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||
def from_dict(d: Dict[str, Any], /, name: str = None) -> Schema: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
""" A single Schema from its dict representation | ||||||||||||||||||||||||||||||||||||||||||||||||||||
:param d: Dict representation of the schema | ||||||||||||||||||||||||||||||||||||||||||||||||||||
:param name: Name by which the schema is referenced, such as a model name. Used to infer the type name if a `title` property is not available. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||
required_set = set(d.get("required", [])) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
required_properties: List[Property] = [] | ||||||||||||||||||||||||||||||||||||||||||||||||||||
optional_properties: List[Property] = [] | ||||||||||||||||||||||||||||||||||||||||||||||||||||
relative_imports: Set[str] = set() | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
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: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
relative_imports.add(import_string_from_reference(p.reference)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
ref = Reference.from_ref(d.get("title", name)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
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)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+169
to
+180
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
schema = Schema( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
reference=Reference.from_ref(d["title"]), | ||||||||||||||||||||||||||||||||||||||||||||||||||||
reference=ref, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
required_properties=required_properties, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
optional_properties=optional_properties, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
relative_imports=relative_imports, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -183,8 +191,8 @@ def from_dict(d: Dict[str, Any], /) -> Schema: | |||||||||||||||||||||||||||||||||||||||||||||||||||
def dict(d: Dict[str, Dict[str, Any]], /) -> Dict[str, Schema]: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
""" Get a list of Schemas from an OpenAPI dict """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||
result = {} | ||||||||||||||||||||||||||||||||||||||||||||||||||||
for data in d.values(): | ||||||||||||||||||||||||||||||||||||||||||||||||||||
s = Schema.from_dict(data) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
for name, data in d.items(): | ||||||||||||||||||||||||||||||||||||||||||||||||||||
s = Schema.from_dict(data, name=name) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
result[s.reference.class_name] = s | ||||||||||||||||||||||||||||||||||||||||||||||||||||
return result | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,9 +21,10 @@ class Reference: | |
def from_ref(ref: str) -> Reference: | ||
""" Get a Reference from the openapi #/schemas/blahblah string """ | ||
ref_value = ref.split("/")[-1] | ||
class_name = stringcase.pascalcase(ref_value) | ||
# ugly hack to avoid stringcase ugly pascalcase output when ref_value isn't snake case | ||
class_name = stringcase.pascalcase(ref_value.replace(" ", "")) | ||
|
||
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),) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -274,10 +274,14 @@ def test__build_models(self, mocker): | |
"__init__.py": models_init, | ||
f"{schema_1.reference.module_name}.py": schema_1_module_path, | ||
f"{schema_2.reference.module_name}.py": schema_2_module_path, | ||
f"{enum_1.name}.py": enum_1_module_path, | ||
f"{enum_2.name}.py": enum_2_module_path, | ||
f"{enum_1.reference.module_name}.py": enum_1_module_path, | ||
f"{enum_2.reference.module_name}.py": enum_2_module_path, | ||
} | ||
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 | ||
Comment on lines
-280
to
+284
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
project.package_dir.__truediv__.return_value = models_dir | ||
model_render_1 = mocker.MagicMock() | ||
model_render_2 = mocker.MagicMock() | ||
|
Uh oh!
There was an error while loading. Please reload this page.