Skip to content

[python] Fix circular imports on AllOf generation #18272

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 9 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -1002,6 +1002,11 @@ private ModelsMap postProcessModelsMap(ModelsMap objs) {
// set the extensions if the key is absent
model.getVendorExtensions().putIfAbsent("x-py-readonly", readOnlyFields);

// remove the items of postponedModelImports in modelImports to avoid circular imports error
if (!modelImports.isEmpty() && !postponedModelImports.isEmpty()){
modelImports.removeAll(postponedModelImports);
}

// import models one by one
if (!modelImports.isEmpty()) {
Set<String> modelsToImport = new TreeSet<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1004,6 +1004,11 @@ private ModelsMap postProcessModelsMap(ModelsMap objs) {
model.getVendorExtensions().putIfAbsent("x-py-datetime-imports", datetimeImports);
model.getVendorExtensions().putIfAbsent("x-py-readonly", readOnlyFields);

// remove the items of postponedModelImports in modelImports to avoid circular imports error
if (!modelImports.isEmpty() && !postponedModelImports.isEmpty()){
modelImports.removeAll(postponedModelImports);
}

// import models one by one
if (!modelImports.isEmpty()) {
Set<String> modelsToImport = new TreeSet<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2711,3 +2711,26 @@ components:
properties:
field:
type: string
CircularAllOfRef:
allOf:
- $ref: '#/components/schemas/AllOfSuperModel'
- properties:
secondCircularAllOfRef:
items:
$ref: '#/components/schemas/SecondCircularAllOfRef'
type: array
type: object
SecondCircularAllOfRef:
allOf:
- $ref: '#/components/schemas/AllOfSuperModel'
- properties:
circularAllOfRef:
items:
$ref: '#/components/schemas/CircularAllOfRef'
type: array
type: object
AllOfSuperModel:
properties:
_name:
type: string
type: object
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ docs/AdditionalPropertiesAnyType.md
docs/AdditionalPropertiesClass.md
docs/AdditionalPropertiesObject.md
docs/AdditionalPropertiesWithDescriptionOnly.md
docs/AllOfSuperModel.md
docs/AllOfWithSingleRef.md
docs/Animal.md
docs/AnotherFakeApi.md
Expand All @@ -21,6 +22,7 @@ docs/Bathing.md
docs/Capitalization.md
docs/Cat.md
docs/Category.md
docs/CircularAllOfRef.md
docs/CircularReferenceModel.md
docs/ClassModel.md
docs/Client.md
Expand Down Expand Up @@ -85,6 +87,7 @@ docs/PoopCleaning.md
docs/PropertyMap.md
docs/PropertyNameCollision.md
docs/ReadOnlyFirst.md
docs/SecondCircularAllOfRef.md
docs/SecondRef.md
docs/SelfReferenceModel.md
docs/SingleRefType.md
Expand Down Expand Up @@ -125,6 +128,7 @@ petstore_api/models/additional_properties_any_type.py
petstore_api/models/additional_properties_class.py
petstore_api/models/additional_properties_object.py
petstore_api/models/additional_properties_with_description_only.py
petstore_api/models/all_of_super_model.py
petstore_api/models/all_of_with_single_ref.py
petstore_api/models/animal.py
petstore_api/models/any_of_color.py
Expand All @@ -138,6 +142,7 @@ petstore_api/models/bathing.py
petstore_api/models/capitalization.py
petstore_api/models/cat.py
petstore_api/models/category.py
petstore_api/models/circular_all_of_ref.py
petstore_api/models/circular_reference_model.py
petstore_api/models/class_model.py
petstore_api/models/client.py
Expand Down Expand Up @@ -197,6 +202,7 @@ petstore_api/models/poop_cleaning.py
petstore_api/models/property_map.py
petstore_api/models/property_name_collision.py
petstore_api/models/read_only_first.py
petstore_api/models/second_circular_all_of_ref.py
petstore_api/models/second_ref.py
petstore_api/models/self_reference_model.py
petstore_api/models/single_ref_type.py
Expand Down
3 changes: 3 additions & 0 deletions samples/openapi3/client/petstore/python-aiohttp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ Class | Method | HTTP request | Description
- [AdditionalPropertiesClass](docs/AdditionalPropertiesClass.md)
- [AdditionalPropertiesObject](docs/AdditionalPropertiesObject.md)
- [AdditionalPropertiesWithDescriptionOnly](docs/AdditionalPropertiesWithDescriptionOnly.md)
- [AllOfSuperModel](docs/AllOfSuperModel.md)
- [AllOfWithSingleRef](docs/AllOfWithSingleRef.md)
- [Animal](docs/Animal.md)
- [AnyOfColor](docs/AnyOfColor.md)
Expand All @@ -167,6 +168,7 @@ Class | Method | HTTP request | Description
- [Capitalization](docs/Capitalization.md)
- [Cat](docs/Cat.md)
- [Category](docs/Category.md)
- [CircularAllOfRef](docs/CircularAllOfRef.md)
- [CircularReferenceModel](docs/CircularReferenceModel.md)
- [ClassModel](docs/ClassModel.md)
- [Client](docs/Client.md)
Expand Down Expand Up @@ -226,6 +228,7 @@ Class | Method | HTTP request | Description
- [PropertyMap](docs/PropertyMap.md)
- [PropertyNameCollision](docs/PropertyNameCollision.md)
- [ReadOnlyFirst](docs/ReadOnlyFirst.md)
- [SecondCircularAllOfRef](docs/SecondCircularAllOfRef.md)
- [SecondRef](docs/SecondRef.md)
- [SelfReferenceModel](docs/SelfReferenceModel.md)
- [SingleRefType](docs/SingleRefType.md)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# AllOfSuperModel


## Properties

Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**name** | **str** | | [optional]

## Example

```python
from petstore_api.models.all_of_super_model import AllOfSuperModel

# TODO update the JSON string below
json = "{}"
# create an instance of AllOfSuperModel from a JSON string
all_of_super_model_instance = AllOfSuperModel.from_json(json)
# print the JSON string representation of the object
print(AllOfSuperModel.to_json())

# convert the object into a dict
all_of_super_model_dict = all_of_super_model_instance.to_dict()
# create an instance of AllOfSuperModel from a dict
all_of_super_model_form_dict = all_of_super_model.from_dict(all_of_super_model_dict)
```
[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)


Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# CircularAllOfRef


## Properties

Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**name** | **str** | | [optional]
**second_circular_all_of_ref** | [**List[SecondCircularAllOfRef]**](SecondCircularAllOfRef.md) | | [optional]

## Example

```python
from petstore_api.models.circular_all_of_ref import CircularAllOfRef

# TODO update the JSON string below
json = "{}"
# create an instance of CircularAllOfRef from a JSON string
circular_all_of_ref_instance = CircularAllOfRef.from_json(json)
# print the JSON string representation of the object
print(CircularAllOfRef.to_json())

# convert the object into a dict
circular_all_of_ref_dict = circular_all_of_ref_instance.to_dict()
# create an instance of CircularAllOfRef from a dict
circular_all_of_ref_form_dict = circular_all_of_ref.from_dict(circular_all_of_ref_dict)
```
[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)


Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# SecondCircularAllOfRef


## Properties

Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**name** | **str** | | [optional]
**circular_all_of_ref** | [**List[CircularAllOfRef]**](CircularAllOfRef.md) | | [optional]

## Example

```python
from petstore_api.models.second_circular_all_of_ref import SecondCircularAllOfRef

# TODO update the JSON string below
json = "{}"
# create an instance of SecondCircularAllOfRef from a JSON string
second_circular_all_of_ref_instance = SecondCircularAllOfRef.from_json(json)
# print the JSON string representation of the object
print(SecondCircularAllOfRef.to_json())

# convert the object into a dict
second_circular_all_of_ref_dict = second_circular_all_of_ref_instance.to_dict()
# create an instance of SecondCircularAllOfRef from a dict
second_circular_all_of_ref_form_dict = second_circular_all_of_ref.from_dict(second_circular_all_of_ref_dict)
```
[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)


Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
from petstore_api.models.additional_properties_class import AdditionalPropertiesClass
from petstore_api.models.additional_properties_object import AdditionalPropertiesObject
from petstore_api.models.additional_properties_with_description_only import AdditionalPropertiesWithDescriptionOnly
from petstore_api.models.all_of_super_model import AllOfSuperModel
from petstore_api.models.all_of_with_single_ref import AllOfWithSingleRef
from petstore_api.models.animal import Animal
from petstore_api.models.any_of_color import AnyOfColor
Expand All @@ -56,6 +57,7 @@
from petstore_api.models.capitalization import Capitalization
from petstore_api.models.cat import Cat
from petstore_api.models.category import Category
from petstore_api.models.circular_all_of_ref import CircularAllOfRef
from petstore_api.models.circular_reference_model import CircularReferenceModel
from petstore_api.models.class_model import ClassModel
from petstore_api.models.client import Client
Expand Down Expand Up @@ -115,6 +117,7 @@
from petstore_api.models.property_map import PropertyMap
from petstore_api.models.property_name_collision import PropertyNameCollision
from petstore_api.models.read_only_first import ReadOnlyFirst
from petstore_api.models.second_circular_all_of_ref import SecondCircularAllOfRef
from petstore_api.models.second_ref import SecondRef
from petstore_api.models.self_reference_model import SelfReferenceModel
from petstore_api.models.single_ref_type import SingleRefType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from petstore_api.models.additional_properties_class import AdditionalPropertiesClass
from petstore_api.models.additional_properties_object import AdditionalPropertiesObject
from petstore_api.models.additional_properties_with_description_only import AdditionalPropertiesWithDescriptionOnly
from petstore_api.models.all_of_super_model import AllOfSuperModel
from petstore_api.models.all_of_with_single_ref import AllOfWithSingleRef
from petstore_api.models.animal import Animal
from petstore_api.models.any_of_color import AnyOfColor
Expand All @@ -31,6 +32,7 @@
from petstore_api.models.capitalization import Capitalization
from petstore_api.models.cat import Cat
from petstore_api.models.category import Category
from petstore_api.models.circular_all_of_ref import CircularAllOfRef
from petstore_api.models.circular_reference_model import CircularReferenceModel
from petstore_api.models.class_model import ClassModel
from petstore_api.models.client import Client
Expand Down Expand Up @@ -90,6 +92,7 @@
from petstore_api.models.property_map import PropertyMap
from petstore_api.models.property_name_collision import PropertyNameCollision
from petstore_api.models.read_only_first import ReadOnlyFirst
from petstore_api.models.second_circular_all_of_ref import SecondCircularAllOfRef
from petstore_api.models.second_ref import SecondRef
from petstore_api.models.self_reference_model import SelfReferenceModel
from petstore_api.models.single_ref_type import SingleRefType
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# coding: utf-8

"""
OpenAPI Petstore

This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\

The version of the OpenAPI document: 1.0.0
Generated by OpenAPI Generator (https://openapi-generator.tech)

Do not edit the class manually.
""" # noqa: E501


from __future__ import annotations
import pprint
import re # noqa: F401
import json

from pydantic import BaseModel, ConfigDict, Field, StrictStr
from typing import Any, ClassVar, Dict, List, Optional
from typing import Optional, Set
from typing_extensions import Self

class AllOfSuperModel(BaseModel):
"""
AllOfSuperModel
""" # noqa: E501
name: Optional[StrictStr] = Field(default=None, alias="_name")
__properties: ClassVar[List[str]] = ["_name"]

model_config = ConfigDict(
populate_by_name=True,
validate_assignment=True,
protected_namespaces=(),
)


def to_str(self) -> str:
"""Returns the string representation of the model using alias"""
return pprint.pformat(self.model_dump(by_alias=True))

def to_json(self) -> str:
"""Returns the JSON representation of the model using alias"""
# TODO: pydantic v2: use .model_dump_json(by_alias=True, exclude_unset=True) instead
return json.dumps(self.to_dict())

@classmethod
def from_json(cls, json_str: str) -> Optional[Self]:
"""Create an instance of AllOfSuperModel from a JSON string"""
return cls.from_dict(json.loads(json_str))

def to_dict(self) -> Dict[str, Any]:
"""Return the dictionary representation of the model using alias.

This has the following differences from calling pydantic's
`self.model_dump(by_alias=True)`:

* `None` is only added to the output dict for nullable fields that
were set at model initialization. Other fields with value `None`
are ignored.
"""
excluded_fields: Set[str] = set([
])

_dict = self.model_dump(
by_alias=True,
exclude=excluded_fields,
exclude_none=True,
)
return _dict

@classmethod
def from_dict(cls, obj: Optional[Dict[str, Any]]) -> Optional[Self]:
"""Create an instance of AllOfSuperModel from a dict"""
if obj is None:
return None

if not isinstance(obj, dict):
return cls.model_validate(obj)

_obj = cls.model_validate({
"_name": obj.get("_name")
})
return _obj


Loading