Skip to content

Commit efaa5ac

Browse files
authored
Merge pull request #699 from python-openapi/feature/move-mimetype-to-content-type
Replace mimetype with content_type
2 parents cc95ed0 + 2a3bd96 commit efaa5ac

29 files changed

+94
-83
lines changed

Diff for: openapi_core/contrib/aiohttp/requests.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,5 @@ def body(self) -> str | None:
4949
return self._body
5050

5151
@property
52-
def mimetype(self) -> str:
52+
def content_type(self) -> str:
5353
return self.request.content_type

Diff for: openapi_core/contrib/aiohttp/responses.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def status_code(self) -> int:
2626
return self.response.status
2727

2828
@property
29-
def mimetype(self) -> str:
29+
def content_type(self) -> str:
3030
return self.response.content_type or ""
3131

3232
@property

Diff for: openapi_core/contrib/django/requests.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -81,5 +81,7 @@ def body(self) -> str:
8181
return self.request.body.decode("utf-8")
8282

8383
@property
84-
def mimetype(self) -> str:
85-
return self.request.content_type or ""
84+
def content_type(self) -> str:
85+
content_type = self.request.META.get("CONTENT_TYPE", "")
86+
assert isinstance(content_type, str)
87+
return content_type

Diff for: openapi_core/contrib/django/responses.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def headers(self) -> Headers:
2626
return Headers(self.response.headers.items())
2727

2828
@property
29-
def mimetype(self) -> str:
29+
def content_type(self) -> str:
3030
content_type = self.response.get("Content-Type", "")
3131
assert isinstance(content_type, str)
3232
return content_type

Diff for: openapi_core/contrib/falcon/requests.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ def body(self) -> Optional[str]:
5656
return dumps(getattr(self.request, "json", media))
5757

5858
@property
59-
def mimetype(self) -> str:
59+
def content_type(self) -> str:
6060
if self.request.content_type:
6161
assert isinstance(self.request.content_type, str)
62-
return self.request.content_type.partition(";")[0]
62+
return self.request.content_type
6363

6464
assert isinstance(self.request.options, RequestOptions)
6565
assert isinstance(self.request.options.default_media_type, str)

Diff for: openapi_core/contrib/falcon/responses.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ def status_code(self) -> int:
2121
return int(self.response.status[:3])
2222

2323
@property
24-
def mimetype(self) -> str:
25-
mimetype = ""
24+
def content_type(self) -> str:
25+
content_type = ""
2626
if self.response.content_type:
27-
mimetype = self.response.content_type.partition(";")[0]
27+
content_type = self.response.content_type
2828
else:
29-
mimetype = self.response.options.default_media_type
30-
return mimetype
29+
content_type = self.response.options.default_media_type
30+
return content_type
3131

3232
@property
3333
def headers(self) -> Headers:

Diff for: openapi_core/contrib/requests/requests.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,13 @@ def body(self) -> Optional[str]:
7474
return self.request.body
7575

7676
@property
77-
def mimetype(self) -> str:
77+
def content_type(self) -> str:
7878
# Order matters because all python requests issued from a session
7979
# include Accept */* which does not necessarily match the content type
8080
return str(
8181
self.request.headers.get("Content-Type")
8282
or self.request.headers.get("Accept")
83-
).split(";")[0]
83+
)
8484

8585

8686
class RequestsOpenAPIWebhookRequest(RequestsOpenAPIRequest):

Diff for: openapi_core/contrib/requests/responses.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def status_code(self) -> int:
1919
return int(self.response.status_code)
2020

2121
@property
22-
def mimetype(self) -> str:
22+
def content_type(self) -> str:
2323
return str(self.response.headers.get("Content-Type", ""))
2424

2525
@property

Diff for: openapi_core/contrib/starlette/requests.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ def body(self) -> Optional[str]:
4444
return body
4545

4646
@property
47-
def mimetype(self) -> str:
48-
content_type = self.request.headers.get("Content-Type")
49-
if content_type:
50-
return content_type.partition(";")[0]
51-
52-
return ""
47+
def content_type(self) -> str:
48+
# default value according to RFC 2616
49+
return (
50+
self.request.headers.get("Content-Type")
51+
or "application/octet-stream"
52+
)

Diff for: openapi_core/contrib/starlette/responses.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ def status_code(self) -> int:
2121
return self.response.status_code
2222

2323
@property
24-
def mimetype(self) -> str:
25-
return self.response.media_type or ""
24+
def content_type(self) -> str:
25+
return self.response.headers.get("Content-Type") or ""
2626

2727
@property
2828
def headers(self) -> Headers:

Diff for: openapi_core/contrib/werkzeug/requests.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,9 @@ def body(self) -> Optional[str]:
4343
return self.request.get_data(as_text=True)
4444

4545
@property
46-
def mimetype(self) -> str:
47-
return self.request.mimetype
46+
def content_type(self) -> str:
47+
# default value according to RFC 2616
48+
return self.request.content_type or "application/octet-stream"
4849

4950
def get_path(self, path: str) -> str:
5051
return "".join([self.request.root_path, path])

Diff for: openapi_core/contrib/werkzeug/responses.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def status_code(self) -> int:
1818
return self.response._status_code
1919

2020
@property
21-
def mimetype(self) -> str:
21+
def content_type(self) -> str:
2222
return str(self.response.mimetype)
2323

2424
@property

Diff for: openapi_core/protocols.py

+10-14
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def body(self) -> Optional[str]:
2121
...
2222

2323
@property
24-
def mimetype(self) -> str:
24+
def content_type(self) -> str:
2525
...
2626

2727

@@ -48,11 +48,9 @@ class Request(BaseRequest, Protocol):
4848
to write resolved path parameters.
4949
body
5050
The request body, as string.
51-
mimetype
52-
Like content type, but without parameters (eg, without charset,
53-
type etc.) and always lowercase.
54-
For example if the content type is "text/HTML; charset=utf-8"
55-
the mimetype would be "text/html".
51+
content_type
52+
The content type with parameters (eg, charset, boundary etc.)
53+
and always lowercase.
5654
"""
5755

5856
@property
@@ -78,11 +76,9 @@ class WebhookRequest(BaseRequest, Protocol):
7876
to write resolved path parameters.
7977
body
8078
The request body, as string.
81-
mimetype
82-
Like content type, but without parameters (eg, without charset,
83-
type etc.) and always lowercase.
84-
For example if the content type is "text/HTML; charset=utf-8"
85-
the mimetype would be "text/html".
79+
content_type
80+
The content type with parameters (eg, charset, boundary etc.)
81+
and always lowercase.
8682
"""
8783

8884
@property
@@ -119,8 +115,8 @@ class Response(Protocol):
119115
The status code as integer.
120116
headers
121117
Response headers as Headers.
122-
mimetype
123-
Lowercase content type without charset.
118+
content_type
119+
The content type with parameters and always lowercase.
124120
"""
125121

126122
@property
@@ -132,7 +128,7 @@ def status_code(self) -> int:
132128
...
133129

134130
@property
135-
def mimetype(self) -> str:
131+
def content_type(self) -> str:
136132
...
137133

138134
@property

Diff for: openapi_core/testing/requests.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def __init__(
2121
headers: Optional[Dict[str, Any]] = None,
2222
cookies: Optional[Dict[str, Any]] = None,
2323
data: Optional[str] = None,
24-
mimetype: str = "application/json",
24+
content_type: str = "application/json",
2525
):
2626
self.host_url = host_url
2727
self.method = method.lower()
@@ -32,7 +32,7 @@ def __init__(
3232
self.headers = headers
3333
self.cookies = cookies
3434
self.body = data or ""
35-
self.mimetype = mimetype
35+
self.content_type = content_type
3636

3737
self.parameters = RequestParameters(
3838
path=self.view_args or {},

Diff for: openapi_core/testing/responses.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ def __init__(
1212
data: str,
1313
status_code: int = 200,
1414
headers: Optional[Dict[str, Any]] = None,
15-
mimetype: str = "application/json",
15+
content_type: str = "application/json",
1616
):
1717
self.data = data
1818
self.status_code = status_code
1919
self.headers = Headers(headers or {})
20-
self.mimetype = mimetype
20+
self.content_type = content_type

Diff for: openapi_core/unmarshalling/request/unmarshallers.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,9 @@ def _unmarshal(
150150
params_errors = []
151151

152152
try:
153-
body = self._get_body(request.body, request.mimetype, operation)
153+
body = self._get_body(
154+
request.body, request.content_type, operation
155+
)
154156
except MissingRequestBody:
155157
body = None
156158
body_errors = []
@@ -172,7 +174,9 @@ def _unmarshal_body(
172174
self, request: BaseRequest, operation: SchemaPath, path: SchemaPath
173175
) -> RequestUnmarshalResult:
174176
try:
175-
body = self._get_body(request.body, request.mimetype, operation)
177+
body = self._get_body(
178+
request.body, request.content_type, operation
179+
)
176180
except MissingRequestBody:
177181
body = None
178182
errors = []

Diff for: openapi_core/unmarshalling/response/unmarshallers.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def _unmarshal(
6666

6767
try:
6868
validated_data = self._get_data(
69-
response.data, response.mimetype, operation_response
69+
response.data, response.content_type, operation_response
7070
)
7171
except DataValidationError as exc:
7272
validated_data = None
@@ -106,7 +106,7 @@ def _unmarshal_data(
106106

107107
try:
108108
validated = self._get_data(
109-
response.data, response.mimetype, operation_response
109+
response.data, response.content_type, operation_response
110110
)
111111
except DataValidationError as exc:
112112
validated = None

Diff for: openapi_core/validation/request/validators.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -111,15 +111,15 @@ def _iter_errors(
111111
yield from exc.errors
112112

113113
try:
114-
self._get_body(request.body, request.mimetype, operation)
114+
self._get_body(request.body, request.content_type, operation)
115115
except RequestBodyValidationError as exc:
116116
yield exc
117117

118118
def _iter_body_errors(
119119
self, request: BaseRequest, operation: SchemaPath
120120
) -> Iterator[Exception]:
121121
try:
122-
self._get_body(request.body, request.mimetype, operation)
122+
self._get_body(request.body, request.content_type, operation)
123123
except RequestBodyValidationError as exc:
124124
yield exc
125125

Diff for: openapi_core/validation/response/validators.py

+10-4
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,10 @@ def iter_errors(
234234
return
235235

236236
yield from self._iter_data_errors(
237-
response.status_code, response.data, response.mimetype, operation
237+
response.status_code,
238+
response.data,
239+
response.content_type,
240+
operation,
238241
)
239242

240243

@@ -273,7 +276,7 @@ def iter_errors(
273276
response.status_code,
274277
response.data,
275278
response.headers,
276-
response.mimetype,
279+
response.content_type,
277280
operation,
278281
)
279282

@@ -292,7 +295,10 @@ def iter_errors(
292295
return
293296

294297
yield from self._iter_data_errors(
295-
response.status_code, response.data, response.mimetype, operation
298+
response.status_code,
299+
response.data,
300+
response.content_type,
301+
operation,
296302
)
297303

298304

@@ -331,7 +337,7 @@ def iter_errors(
331337
response.status_code,
332338
response.data,
333339
response.headers,
334-
response.mimetype,
340+
response.content_type,
335341
operation,
336342
)
337343

Diff for: tests/integration/test_petstore.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ def test_get_pets_response(self, spec):
195195
assert response_result.data.data[0].id == 1
196196
assert response_result.data.data[0].name == "Cat"
197197

198-
def test_get_pets_response_no_schema(self, spec):
198+
def test_get_pets_response_media_type(self, spec):
199199
host_url = "http://petstore.swagger.io/v1"
200200
path_pattern = "/v1/pets"
201201
query_params = {
@@ -230,15 +230,15 @@ def test_get_pets_response_no_schema(self, spec):
230230

231231
assert result.body is None
232232

233-
data = b"<html></html>"
233+
data = b"<html>\xb1\xbc</html>"
234234
response = MockResponse(
235-
data, status_code=404, mimetype="text/html; charset=utf-8"
235+
data, status_code=404, content_type="text/html; charset=iso-8859-2"
236236
)
237237

238238
response_result = unmarshal_response(request, response, spec=spec)
239239

240240
assert response_result.errors == []
241-
assert response_result.data == data.decode("utf-8")
241+
assert response_result.data == data.decode("iso-8859-2")
242242

243243
def test_get_pets_invalid_response(self, spec, response_unmarshaller):
244244
host_url = "http://petstore.swagger.io/v1"
@@ -1015,7 +1015,7 @@ def test_post_pets_raises_invalid_mimetype(self, spec):
10151015
"/pets",
10161016
path_pattern=path_pattern,
10171017
data=data,
1018-
mimetype="text/html",
1018+
content_type="text/html",
10191019
headers=headers,
10201020
cookies=cookies,
10211021
)
@@ -1150,7 +1150,7 @@ def test_post_pets_raises_invalid_server_error(self, spec):
11501150
"/pets",
11511151
path_pattern=path_pattern,
11521152
data=data,
1153-
mimetype="text/html",
1153+
content_type="text/html",
11541154
headers=headers,
11551155
cookies=cookies,
11561156
)
@@ -1372,7 +1372,7 @@ def test_get_pet_wildcard(self, spec):
13721372
assert result.body is None
13731373

13741374
data = b"imagedata"
1375-
response = MockResponse(data, mimetype="image/png")
1375+
response = MockResponse(data, content_type="image/png")
13761376

13771377
response_result = unmarshal_response(request, response, spec=spec)
13781378

0 commit comments

Comments
 (0)