Skip to content

[BUG][PYTHON] Request preparation fails when content-type contains parameters (except for JSON) #23990

@MarcelKonrad

Description

@MarcelKonrad

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator?
  • 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

Given an OpenAPI specification for an endpoint that defines a request body for a content type that does not contain 'json' as a substring, and has a parameter defined (e.g. charset=UTF-8), then the request preparation will fail in generated client library when calling the respective endpoint.

openapi-generator version

7.23.0

OpenAPI declaration file content or url
openapi: 3.1.1
info:
  title: Example
  version: 1.0.0
paths:
  /example:
    post:
      summary: Example
      operationId: example
      requestBody:
        content:
          multipart/form-data;charset=UTF-8:
            schema:
              type: object
              properties:
                field:
                  type: string
      responses:
        "200":
          description: "Example"
Generation Details

java -jar openapi-generator-cli.jar generate -g python -i apispec.yml -o output

Steps to reproduce
  1. Create a new file called apispec.yml from the yaml above.
  2. Download the latest openapi-generator-cli.jar and put it in the same folder.
  3. Run the command above.
  4. Run cd output.
  5. Install the client python setup.py install --user
  6. Run the following python script:
from openapi_client import DefaultApi, ApiClient, Configuration

configuration = Configuration(host="http://localhost")
api_client = ApiClient(configuration=configuration)
default_api = DefaultApi(api_client)
default_api.example(var_field="test")

The following error trace will be printed to console, instead of the request being sent:

Traceback (most recent call last):
  File "/example/test.py", line 6, in <module>
    default_api.example(var_field="test")
    ~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^
  File "/python313/Lib/site-packages/pydantic/_internal/_validate_call.py", line 40, in wrapper_function
    return wrapper(*args, **kwargs)
  File "/python313/Lib/site-packages/pydantic/_internal/_validate_call.py", line 137, in __call__
    res = self.__pydantic_validator__.validate_python(pydantic_core.ArgsKwargs(args, kwargs))
  File "/python313/site-packages/openapi_client/api/default_api.py", line 94, in example
    response_data = self.api_client.call_api(
        *_param,
        _request_timeout=_request_timeout
    )
  File "/python313/site-packages/openapi_client/api_client.py", line 282, in call_api
    raise e
  File "/python313/site-packages/openapi_client/api_client.py", line 274, in call_api
    response_data = self.rest_client.request(
        method, url,
    ...<2 lines>...
        _request_timeout=_request_timeout
    )
  File "/python313/site-packages/openapi_client/rest.py", line 247, in request
    raise ApiException(status=0, reason=msg)
openapi_client.exceptions.ApiException: (0)
Reason: Cannot prepare a request message for provided
                             arguments. Please check that your arguments match
                             declared content type.
Related issues/PRs

I could not find any existing issue describing this exact same problem.

Suggest a fix

Adjust the content-type matching code in rest.py to account for parameters. I will create a PR.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    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