forked from openapi-generators/openapi-python-client
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbodies.py
More file actions
146 lines (131 loc) · 4.58 KB
/
bodies.py
File metadata and controls
146 lines (131 loc) · 4.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import sys
from typing import Union
import attr
from openapi_python_client.parser.properties import (
ModelProperty,
Property,
Schemas,
property_from_data,
)
from .. import schema as oai
from ..config import Config
from ..utils import get_content_type
from .errors import ErrorLevel, ParseError
if sys.version_info >= (3, 11):
from enum import StrEnum
class BodyType(StrEnum):
JSON = "json"
DATA = "data"
FILES = "files"
CONTENT = "content"
else:
from enum import Enum
class BodyType(str, Enum):
JSON = "json"
DATA = "data"
FILES = "files"
CONTENT = "content"
@attr.define
class Body:
content_type: str
prop: Property
body_type: BodyType
def body_from_data(
*,
data: oai.Operation,
schemas: Schemas,
request_bodies: dict[str, Union[oai.RequestBody, oai.Reference]],
config: Config,
endpoint_name: str,
) -> tuple[list[Union[Body, ParseError]], Schemas]:
"""Adds form or JSON body to Endpoint if included in data"""
body = _resolve_reference(data.request_body, request_bodies)
if isinstance(body, ParseError):
return [body], schemas
if body is None:
return [], schemas
bodies: list[Union[Body, ParseError]] = []
body_content = body.content
prefix_type_names = len(body_content) > 1
for content_type, media_type in body_content.items():
simplified_content_type = get_content_type(content_type, config)
if simplified_content_type is None:
bodies.append(
ParseError(
detail="Invalid content type",
data=body,
level=ErrorLevel.WARNING,
)
)
continue
media_type_schema = media_type.media_type_schema
if media_type_schema is None:
bodies.append(
ParseError(
detail="Missing schema",
data=body,
level=ErrorLevel.WARNING,
)
)
continue
if simplified_content_type == "application/x-www-form-urlencoded":
body_type = BodyType.DATA
elif simplified_content_type == "multipart/form-data":
body_type = BodyType.FILES
elif simplified_content_type == "application/octet-stream":
body_type = BodyType.CONTENT
elif simplified_content_type == "application/json" or simplified_content_type.endswith("+json"):
body_type = BodyType.JSON
else:
bodies.append(
ParseError(
detail=f"Unsupported content type {simplified_content_type}",
data=body,
level=ErrorLevel.WARNING,
)
)
continue
prop, schemas = property_from_data(
name="body",
required=True,
data=media_type_schema,
schemas=schemas,
parent_name=f"{endpoint_name}_{body_type}" if prefix_type_names else endpoint_name,
config=config,
)
if isinstance(prop, ParseError):
bodies.append(prop)
continue
if isinstance(prop, ModelProperty) and body_type == BodyType.FILES:
# Regardless of if we just made this property or found it, it now needs the `to_multipart` method
prop = attr.evolve(prop, is_multipart_body=True)
schemas = attr.evolve(
schemas,
classes_by_name={
**schemas.classes_by_name,
prop.class_info.name: prop,
},
models_to_process=[*schemas.models_to_process, prop],
)
bodies.append(
Body(
content_type=content_type,
prop=prop,
body_type=body_type,
)
)
return bodies, schemas
def _resolve_reference(
body: Union[oai.RequestBody, oai.Reference, None], request_bodies: dict[str, Union[oai.RequestBody, oai.Reference]]
) -> Union[oai.RequestBody, ParseError, None]:
if body is None:
return None
references_seen = []
while isinstance(body, oai.Reference) and body.ref not in references_seen:
references_seen.append(body.ref)
body = request_bodies.get(body.ref.split("/")[-1])
if isinstance(body, oai.Reference):
return ParseError(detail="Circular $ref in request body", data=body)
if body is None and references_seen:
return ParseError(detail=f"Could not resolve $ref {references_seen[-1]} in request body")
return body