OpenAPITools / OpenAPITools/openapi-generator

[python] can't handle 'application/json; charset=UTF-8' when spec lacks charset info

Open
#14,147 3 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Client: Python Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

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?
Description
openapi-generator version

openapi-generator-cli 6.2.1
commit : b0ce532
built : 2022-11-01T09:40:00Z
source : https://github.com/openapitools/openapi-generator
docs : https://openapi-generator.tech/

OpenAPI declaration file content or url

https://admin.splunk.com/service/info/specs/v2/openapi.json

Generation Details

openapi-generator generate -g python -o out -i https://admin.splunk.com/service/info/specs/v2/openapi.json --package-name splunkacs --skip-validate-spec

The API call for the following program goes through and in fact fetches the desired json, but the content-type check fails and exception is thrown instead.

Steps to reproduce
import splunkacs
from splunkacs.apis.tags import indexes_api
from splunkacs.model.error import Error
from splunkacs.model.index_response import IndexResponse
from pprint import pprint
# Defining the host is optional and defaults to http://localhost
# See configuration.py for a list of all supported configuration parameters.
configuration = splunkacs.Configuration(
    host = "https://admin.splunk.com"
)


# Configure Bearer authorization (JWT): bearerAuth
configuration.access_token = '<private token>'


# Enter a context with an instance of the API client
with splunkacs.ApiClient(configuration) as api_client:
    # Create an instance of the API class
    api_instance = indexes_api.IndexesApi(api_client)

    # example passing only required values which don't have defaults set
    path_params = {
        'stack': "michmed",
    }
    query_params = {
    }
    try:
        api_response = api_instance.list_indexes(
            path_params=path_params,
            query_params=query_params,
        )
        pprint(api_response)
    except splunkacs.ApiException as e:
        print("Exception when calling IndexesApi->list_indexes: %s\n" % e)
 

The error is as follows,

Traceback (most recent call last):
  File ".../splunkacstest/splunk_cloud_acs_test.py", line 33, in <module>
    api_response = api_instance.list_indexes(
  File "/Users/yyy/.local/lib/python3.10/site-packages/splunkacs-1.0.0-py3.10.egg/splunkacs/paths/stack_adminconfig_v2_indexes/get.py", line 408, in list_indexes
  File "/Users/yyy/.local/lib/python3.10/site-packages/splunkacs-1.0.0-py3.10.egg/splunkacs/paths/stack_adminconfig_v2_indexes/get.py", line 342, in _list_indexes_oapg
  File "/Users/yyy/.local/lib/python3.10/site-packages/splunkacs-1.0.0-py3.10.egg/splunkacs/api_client.py", line 929, in deserialize
splunkacs.exceptions.ApiValueError: Invalid content_type returned. Content_type='application/json; charset=UTF-8' was returned when only {'application/json'} are defined for status_code=200
Suggest a fix

Not sure whether the spec json is flawed or openapi-generator needs to relax the checking.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the generated splunkacs/api_client.py deserialize method and the failing list_indexes call shown in the traceback. Reproduce the response using the linked OpenAPI document, then trace how the Python generator compares the returned Content-Type with the declared media type. Done means the charset-qualified application/json response is handled correctly without breaking content-type validation.

Written by the indexing model from the issue text.

Assessment

Tech stack
openapi, python
Domain
api
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.