Skip to content

Commit

Permalink
fix classes order (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
koxudaxi authored Sep 13, 2019
1 parent 9dba574 commit 2666433
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
4 changes: 3 additions & 1 deletion datamodel_code_generator/parser/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ def add(
if version_compatible:
self._version_compatibles.append(name)

def add_unresolved_name(self, name: str) -> None:
self._unresolved_class_names.append(name)

def extend(self, class_name: 'ClassNames') -> None:
self._class_names.extend(class_name.class_names)
self._unresolved_class_names.extend(class_name.unresolved_class_names)
Expand Down Expand Up @@ -119,7 +122,6 @@ def sort_data_models(
require_update_action_models.append(model.name)
else:
unresolved_references.append(model)
# sorted_data_models[model.name] = model
if unresolved_references:
try:
return sort_data_models(
Expand Down
20 changes: 13 additions & 7 deletions datamodel_code_generator/parser/openapi.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from collections import OrderedDict
from typing import Callable, Dict, List, Optional, Set, Tuple, Type

from datamodel_code_generator import PythonVersion
Expand Down Expand Up @@ -65,7 +64,7 @@ def parse_any_of(self, name: str, obj: JsonSchemaObject) -> ClassNames:
else:
singular_name = get_singular_name(name)
self.parse_object(singular_name, any_of_item)
any_of_item_names.add(singular_name, version_compatible=True)
any_of_item_names.add(singular_name, ref=True, version_compatible=True)
return any_of_item_names

def parse_all_of(self, name: str, obj: JsonSchemaObject) -> ClassNames:
Expand Down Expand Up @@ -102,7 +101,7 @@ def parse_all_of(self, name: str, obj: JsonSchemaObject) -> ClassNames:
all_of_item_names.extend(base_classes)
self.append_result(data_model_type)

all_of_name.add(name, version_compatible=True)
all_of_name.add(name, ref=True, version_compatible=True)
return all_of_name

def parse_object_fields(
Expand All @@ -114,7 +113,9 @@ def parse_object_fields(
for field_name, filed in obj.properties.items(): # type: ignore
field_class_names: ClassNames = ClassNames(self.target_python_version)
if filed.ref:
field_class_names.add(filed.ref_object_name, version_compatible=True)
field_class_names.add(
filed.ref_object_name, ref=True, version_compatible=True
)
field_type_hint = field_class_names.get_type()
elif filed.is_array:
class_name = self.get_class_name(field_name)
Expand All @@ -126,11 +127,13 @@ def parse_object_fields(
elif filed.is_object:
class_name = self.get_class_name(field_name)
self.parse_object(class_name, filed)
field_class_names.add(class_name, version_compatible=True)
field_class_names.add(class_name, ref=True, version_compatible=True)
field_type_hint = field_class_names.get_type()
elif filed.enum:
self.parse_enum(field_name, filed)
field_class_names.add(self.results[-1].name, version_compatible=True)
field_class_names.add(
self.results[-1].name, ref=True, version_compatible=True
)
field_type_hint = field_class_names.get_type()
elif filed.anyOf:
any_of_item_names = self.parse_any_of(field_name, filed)
Expand Down Expand Up @@ -184,18 +187,21 @@ def parse_array_fields(
elif isinstance(item, JsonSchemaObject) and item.properties:
singular_name = get_singular_name(name)
self.parse_object(singular_name, item)
item_obj_names.add(singular_name, version_compatible=True)
item_obj_names.add(singular_name, ref=True, version_compatible=True)
elif item.anyOf:
any_of_item_names = self.parse_any_of(name, item)
item_obj_names.add(any_of_item_names.get_union_type())
item_obj_names.add_unresolved_name(name)
elif item.allOf:
singular_name = get_singular_name(name)
all_of_item_names = self.parse_all_of(singular_name, item)
item_obj_names.add(all_of_item_names.get_type())
item_obj_names.add_unresolved_name(singular_name)
else:
data_type = self.get_data_type(item)
item_obj_names.add(data_type.type_hint)
self.imports.append(data_type.import_)

field = DataModelField(type_hint=item_obj_names.get_list_type(), required=True)
return [field], item_obj_names

Expand Down

0 comments on commit 2666433

Please sign in to comment.