Skip to content

Commit de4249b

Browse files
committed
Handle octet-stream content media type as binary format
1 parent 5fcaf72 commit de4249b

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

openapi_python_client/parser/properties/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def _string_based_property(
7474
description=data.description,
7575
example=data.example,
7676
)
77-
if string_format == "binary":
77+
if string_format == "binary" or data.content_media_type == "application/octet-stream":
7878
return FileProperty.build(
7979
name=name,
8080
required=required,

openapi_python_client/schema/openapi_schema_pydantic/schema.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class Schema(BaseModel):
4848
additionalProperties: bool | ReferenceOr["Schema"] | None = None
4949
description: str | None = None
5050
schema_format: str | None = Field(default=None, alias="format")
51+
content_media_type: str | None = Field(default=None, alias="contentMediaType")
5152
default: Any | None = None
5253
nullable: bool = Field(default=False)
5354
discriminator: Discriminator | None = None

tests/test_parser/test_properties/test_init.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
build_schemas,
1818
property_from_data,
1919
)
20+
from openapi_python_client.parser.properties.string import StringProperty
2021
from openapi_python_client.schema import Parameter, Reference, Schema
2122
from openapi_python_client.utils import ClassName, PythonIdentifier
2223

@@ -224,6 +225,35 @@ def test__string_based_property_binary_format(self, file_property_factory, confi
224225
)
225226
assert p == file_property_factory(name=name, required=required)
226227

228+
@pytest.mark.parametrize(
229+
"content_media_type,schema_format",
230+
[
231+
pytest.param("application/octet-stream", None, id="content-media-type-only"),
232+
pytest.param("application/octet-stream", "binary", id="both-content-media-type-and-binary-format"),
233+
],
234+
)
235+
def test__string_based_property_content_media_type(
236+
self, file_property_factory, config, content_media_type, schema_format
237+
):
238+
name = "file_prop"
239+
required = True
240+
data = oai.Schema(type="string", contentMediaType=content_media_type, format=schema_format)
241+
242+
p, _ = property_from_data(
243+
name=name, required=required, data=data, schemas=Schemas(), config=config, parent_name=""
244+
)
245+
assert p == file_property_factory(name=name, required=required)
246+
247+
def test__string_based_property_non_binary_content_media_type_ignored(self, config):
248+
name = "text_prop"
249+
required = True
250+
data = oai.Schema(type="string", contentMediaType="text/plain")
251+
252+
p, _ = property_from_data(
253+
name=name, required=required, data=data, schemas=Schemas(), config=config, parent_name=""
254+
)
255+
assert isinstance(p, StringProperty)
256+
227257

228258
class TestCreateSchemas:
229259
def test_dereference_references(self, mocker, config):

0 commit comments

Comments
 (0)