Skip to content

Commit 729c354

Browse files
committed
Resolve circular import for Operation and PathItem
1 parent 7955faa commit 729c354

File tree

3 files changed

+41
-9
lines changed

3 files changed

+41
-9
lines changed

openapi_python_client/schema/openapi_schema_pydantic/operation.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
from .callback import Callback
66
from .external_documentation import ExternalDocumentation
77
from .parameter import Parameter
8+
9+
# Required to update forward ref after object creation, as this is not imported yet
10+
from .path_item import PathItem # pylint: disable=unused-import
811
from .reference import Reference
912
from .request_body import RequestBody
1013
from .responses import Responses
@@ -79,3 +82,7 @@ class Config: # pylint: disable=missing-class-docstring
7982
}
8083
]
8184
}
85+
86+
87+
# PathItem in Callback uses Operation, so we need to update forward refs due to circular dependency
88+
Operation.update_forward_refs()

openapi_python_client/schema/openapi_schema_pydantic/path_item.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
from pydantic import BaseModel, Extra, Field
44

5-
from .operation import Operation
65
from .parameter import Parameter
76
from .reference import Reference
87
from .server import Server
@@ -23,14 +22,14 @@ class PathItem(BaseModel):
2322
ref: Optional[str] = Field(default=None, alias="$ref")
2423
summary: Optional[str] = None
2524
description: Optional[str] = None
26-
get: Optional[Operation] = None
27-
put: Optional[Operation] = None
28-
post: Optional[Operation] = None
29-
delete: Optional[Operation] = None
30-
options: Optional[Operation] = None
31-
head: Optional[Operation] = None
32-
patch: Optional[Operation] = None
33-
trace: Optional[Operation] = None
25+
get: Optional["Operation"] = None
26+
put: Optional["Operation"] = None
27+
post: Optional["Operation"] = None
28+
delete: Optional["Operation"] = None
29+
options: Optional["Operation"] = None
30+
head: Optional["Operation"] = None
31+
patch: Optional["Operation"] = None
32+
trace: Optional["Operation"] = None
3433
servers: Optional[List[Server]] = None
3534
parameters: Optional[List[Union[Parameter, Reference]]] = None
3635

@@ -70,3 +69,9 @@ class Config: # pylint: disable=missing-class-docstring
7069
}
7170
]
7271
}
72+
73+
74+
# Operation uses PathItem via Callback, so we need late import and to update forward refs due to circular dependency
75+
from .operation import Operation # pylint: disable=wrong-import-position unused-import
76+
77+
PathItem.update_forward_refs()

tests/test_schema/test_open_api.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,23 @@ def test_validate_version(version, valid):
1414
else:
1515
with pytest.raises(ValidationError):
1616
OpenAPI.parse_obj(data)
17+
18+
19+
def test_parse_with_callback():
20+
data = {
21+
"openapi": "3.0.1",
22+
"info": {"title": "API with Callback", "version": ""},
23+
"paths": {
24+
"/create": {
25+
"post": {
26+
"responses": {"200": {"description": "Success"}},
27+
"callbacks": {"event": {"callback": {"post": {"responses": {"200": {"description": "Success"}}}}}},
28+
}
29+
}
30+
},
31+
}
32+
33+
open_api = OpenAPI.parse_obj(data)
34+
create_endpoint = open_api.paths["/create"]
35+
assert "200" in create_endpoint.post.responses
36+
assert "200" in create_endpoint.post.callbacks["event"]["callback"].post.responses

0 commit comments

Comments
 (0)