Skip to content

[BUG] [Python] Python client content negotiation through accept header #2183

@dojeda

Description

@dojeda

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • What's the version of OpenAPI Generator used?
  • Have you search for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Bounty to sponsor the fix (example)
Description

The Python generated client does not support content negotiation. That is, there is no easy way to set the Accept header when making a request.

I have an API that responds with a JSON object or file contents depending on the Accept header. This is exemplified by the operation excerpt in the

openapi-generator version

4.0.0

OpenAPI declaration file content or url
openapi: 3.0.2
info:
  title: Minimum working example
  version: 0.0.1
paths:
  /file/{id}:
    get:
      summary: Fetch file metadata or contents
      parameters:
        - name: id
          in: path
          schema:
            type:
              integer
      operationId: app.api.data.file.details_w
      responses:
        '200':
          description: File contents or metadata
          content:
            application/json:
              schema:
                type: object
            application/octet-stream:
              schema:
                type: string
                format: binary
Command line used for generation
docker run --rm -v ${PWD}:/local openapitools/openapi-generator-cli generate \
    -i /local/openapi.yaml \
    -g python \
    -o /local/out/python
Steps to reproduce
  1. Download specification file
  2. Generate client
  3. Observe there is no way to setup the Accept header on the generated function. Here is an excerpt of the function:
        header_params['Accept'] = self.api_client.select_header_accept(
            ['application/json', 'application/octet-stream', 'application/problem+json'])  # noqa: E501
Related issues/PRs

I did not find a related issue, but a PR seems to be well ahead on this problem: #2134

Suggest a fix

I am not sure if the related PR can actually solve the problem, but my current workaround is to create a child class of the Api object and override the related ..._with_http_info function to handle an additional _accept keyword argument and replace the current header param as:

def app_api_data_file_details_w_with_http_info(self, id, **kwargs):
       # note that needs the following line to initialize local_var_params correctly
       all_params.append('_accept')
        ...
        # HTTP header `Accept`
        header_params['Accept'] = local_var_params.get('_accept', self.api_client.select_header_accept(
            # the following list was auto-generated
            ['application/json', 'application/octet-stream', 'application/problem+json'])  # noqa: E501
        )
        ...

which I can then use as:

response = my_client.app_api_data_file_details_w_with_http_info(some_id, _preload_content=False)

Note that the _preload_content is necessary so that there is no attempt to deserialize the binary response.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions