Skip to content

[BUG] [PYTHON] Multipart mixed post fails when passing objects #18076

Description

@Poolmann

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

The multipart mixed post fails when trying to send objects.

openapi-generator version

7.4.0

OpenAPI declaration file content or url

form-multipart-binary-array.yaml

Generation Details

java -jar openapi-generator-cli.jar generate -i form-multipart-binary-array.yaml -g python -o test-client

Steps to reproduce

Generate client with mentioned YAML spec and try to run the client like:

with openapi_client.ApiClient(configuration) as api_client:
    # Create an instance of the API class
    api_instance = openapi_client.MultipartApi(api_client)
    marker = MultipartMixedRequestMarker(name="Test")

    try:
        api_instance.multipart_mixed(
            status=MultipartMixedStatus.ALLOWED, file="<file>", marker=marker
        )
    except Exception as e:
        print("Exception when calling MultipartApi->multipart_array: %s\n" % e)
Suggest a fix

The exception actually occurs in urllib3 when, it tries to write the object into a byte stream and can't interpret the dict as bytes (body.write(data)):

def encode_multipart_formdata(
    fields: _TYPE_FIELDS, boundary: str | None = None
) -> tuple[bytes, str]:
    """
    Encode a dictionary of ``fields`` using the multipart/form-data MIME format.

    :param fields:
        Dictionary of fields or list of (key, :class:`~urllib3.fields.RequestField`).
        Values are processed by :func:`urllib3.fields.RequestField.from_tuples`.

    :param boundary:
        If not specified, then a random boundary will be generated using
        :func:`urllib3.filepost.choose_boundary`.
    """
    body = BytesIO()
    if boundary is None:
        boundary = choose_boundary()

    for field in iter_field_objects(fields):
        body.write(f"--{boundary}\r\n".encode("latin-1"))

        writer(body).write(field.render_headers())
        data = field.data

        if isinstance(data, int):
            data = str(data)  # Backwards compatibility

        if isinstance(data, str):
            writer(body).write(data)
        else:
            body.write(data)

        body.write(b"\r\n")

Error message: a bytes-like object is required, not 'dict'

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions