Skip to content

Commit 0c39f64

Browse files
committed
Added support for JSON bodies
Closes #6
1 parent 4c24de4 commit 0c39f64

File tree

4 files changed

+43
-4
lines changed

4 files changed

+43
-4
lines changed

openapi_python_client/models/openapi.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,10 @@ def from_dict(d: Dict[str, Dict[str, Dict[str, Any]]], /) -> Dict[str, EndpointC
6161
)
6262
responses.append(response)
6363
form_body_reference = None
64+
json_body = None
6465
if "requestBody" in method_data:
65-
form_body_reference = Endpoint.parse_request_body(method_data["requestBody"])
66+
form_body_reference = Endpoint.parse_request_form_body(method_data["requestBody"])
67+
json_body = Endpoint.parse_request_json_body(method_data["requestBody"])
6668

6769
endpoint = Endpoint(
6870
path=path,
@@ -73,6 +75,7 @@ def from_dict(d: Dict[str, Dict[str, Dict[str, Any]]], /) -> Dict[str, EndpointC
7375
path_parameters=path_parameters,
7476
responses=responses,
7577
form_body_reference=form_body_reference,
78+
json_body=json_body,
7679
requires_security=bool(method_data.get("security")),
7780
)
7881

@@ -81,6 +84,14 @@ def from_dict(d: Dict[str, Dict[str, Dict[str, Any]]], /) -> Dict[str, EndpointC
8184
collection.relative_imports.add(
8285
import_string_from_reference(form_body_reference, prefix="..models")
8386
)
87+
if (
88+
json_body is not None
89+
and isinstance(json_body, (ListProperty, RefProperty, EnumProperty))
90+
and json_body.reference is not None
91+
):
92+
collection.relative_imports.add(
93+
import_string_from_reference(json_body.reference, prefix="..models")
94+
)
8495
return endpoints_by_tag
8596

8697

@@ -99,17 +110,27 @@ class Endpoint:
99110
responses: List[Response]
100111
requires_security: bool
101112
form_body_reference: Optional[Reference]
113+
json_body: Optional[Property]
102114

103115
@staticmethod
104-
def parse_request_body(body: Dict[str, Any], /) -> Optional[Reference]:
105-
""" Return form_body_ref """
116+
def parse_request_form_body(body: Dict[str, Any], /) -> Optional[Reference]:
117+
""" Return form_body_reference """
106118
form_body_reference = None
107119
body_content = body["content"]
108120
form_body = body_content.get("application/x-www-form-urlencoded")
109121
if form_body:
110122
form_body_reference = Reference(form_body["schema"]["$ref"])
111123
return form_body_reference
112124

125+
@staticmethod
126+
def parse_request_json_body(body: Dict[str, Any], /) -> Optional[Property]:
127+
""" Return json_body """
128+
body_content = body["content"]
129+
json_body = body_content.get("application/json")
130+
if json_body:
131+
return property_from_dict("json_body", required=True, data=json_body["schema"])
132+
return None
133+
113134

114135
@dataclass
115136
class Schema:

openapi_python_client/models/properties.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,10 @@ def get_type_string(self) -> str:
173173
return self.reference.class_name
174174
return f"Optional[{self.reference.class_name}]"
175175

176+
def transform(self) -> str:
177+
""" Convert this into a JSONable value """
178+
return f"{self.name}.to_dict()"
179+
176180

177181
@dataclass
178182
class DictProperty(Property):

openapi_python_client/templates/endpoint_module.pyi

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from dataclasses import asdict
2-
from typing import List, Optional, Union
2+
from typing import Dict, List, Optional, Union
33

44
import requests
55

@@ -23,6 +23,10 @@ def {{ endpoint.name }}(
2323
{% if endpoint.form_body_reference %}
2424
form_data: {{ endpoint.form_body_reference.class_name }},
2525
{% endif %}
26+
{# Form data if any #}
27+
{% if endpoint.json_body %}
28+
json_body: {{ endpoint.json_body.get_type_string() }},
29+
{% endif %}
2630
{# query parameters #}
2731
{% if endpoint.query_parameters %}
2832
{% for parameter in endpoint.query_parameters %}
@@ -51,6 +55,9 @@ def {{ endpoint.name }}(
5155
{% if endpoint.form_body_reference %}
5256
data=asdict(form_data),
5357
{% endif %}
58+
{% if endpoint.json_body %}
59+
json={{ endpoint.json_body.transform() }},
60+
{% endif %}
5461
{% if endpoint.query_parameters %}
5562
params=params,
5663
{% endif %}

openapi_python_client/templates/model.pyi

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ class {{ schema.reference.class_name }}:
1616
{{ property.to_string() }}
1717
{% endfor %}
1818

19+
def to_dict(self) -> Dict:
20+
return {
21+
{% for property in schema.required_properties + schema.optional_properties %}
22+
"{{ property.name }}": self.{{ property.transform() }} if self.{{ property.name }} else None,
23+
{% endfor %}
24+
}
25+
1926
@staticmethod
2027
def from_dict(d: Dict) -> {{ schema.reference.class_name }}:
2128
{% for property in schema.required_properties + schema.optional_properties %}

0 commit comments

Comments
 (0)