OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
20.59k stars 6.28k forks source link

[BUG] Cannot change the Accept header to a request #15774

Open tloubrieu-jpl opened 1 year ago

tloubrieu-jpl commented 1 year ago

Bug Report Checklist

Description

Our API is using the Accept HTTP header not only to change the format (csv, xml, json) of the response but the type of objects returned. This is useful for us since we have multiple flavor of a same object type. For example our object being a product it can be returned into its json structured label format or as a json flat key-value pair format or csv. We use the HTTP Accept header to request the different flavor of the objects. We use to be able to do these requests with the python language generator, using the argument accept_content_types for example: https://github.com/NASA-PDS/pds-api-client/blob/77846d343f3dd6f5dcde060f1ffc43ea9f926d7c/src/pds/api_client/test/integration/test_collections.py#L71:L73

The response was in returning the appropriate object type, as defined in the API specification.

With the python-nextgen generator, which is much better regarding the explicit prototype of the methods generated, I cannot get the objects described in the specification but only a dictionary object and the application/json Accept header is always used to do the server request.

Could we have the flexibility we used to have regarding the response format returned ? I made some tests adding the _header argument to the API calls, but the values get overridden. See below.

openapi-generator version

6.5.0, python-nextgen

OpenAPI declaration file content or url

https://raw.githubusercontent.com/NASA-PDS/pds-api/main/specs/PDS_APIs-search-1.1.1-swagger.yaml

Generation Details

Code generated with command: openapi-generator generate -g python-nextgen -i {yaml openapi input file} --package-name pds.api_client --additional-properties=packageVersion=1.2.0'

Steps to reproduce

Running the python code using the generated code:


from __future__ import print_function
from pds.api_client.rest import ApiException
from pds.api_client import Configuration
from pds.api_client import ApiClient
from pds.api_client.api.all_products_api import AllProductsApi
from pprint import pprint

# create an instance of the API class
configuration = Configuration()
configuration.host = 'https://pds.nasa.gov/api/search/1'
api_client = ApiClient(configuration)

all_products = AllProductsApi(api_client)

try:
    # see ref doc on
    api_response = all_products.select_by_lidvid_with_http_info(
        'urn:nasa:pds:mars2020.spice:spice_kernels::3.0',
        _headers={'Accept': 'application/vnd.nasa.pds.pds4+json'}
    )
    pprint(api_response['summary'])

except ApiException as e:
    print("Exception when calling CollectionsApi->get_collection: %s\n" % e)

The _headers Accept parameter is changed to application/json before the request is sent to the server. The reponse to the request is always a dict instead of the objects defined in the OpenAPI specification.

Related issues/PRs
Suggest a fix

In the generated code we get in classes in the api subpackage:

  ...
  # set the HTTP header `Accept`
        _header_params['Accept'] = self.api_client.select_header_accept(
            ['*/*', 'application/csv', 'application/json', 'application/kvp+json', 'application/vnd.nasa.pds.pds4+json', 'application/vnd.nasa.pds.pds4+xml', 'application/xml', 'text/csv', 'text/html', 'text/xml'])  # noqa: E501

        # authentication setting
        _auth_settings = []  # noqa: E501

        _response_types_map = {
            '200': "PdsProduct",
            '400': "ErrorMessage",
            '404': "ErrorMessage",
            '500': "ErrorMessage",
            '501': "ErrorMessage",
        }
  ...

We need these fix:

  ...
        # set the HTTP header `Accept`
        # Accept header can be passed in api call, for example:
        #        api_response = all_products.select_by_lidvid_with_http_info(
        #                'urn:nasa:pds:mars2020.spice:spice_kernels::3.0',
        #                _headers={'Accept': 'application/vnd.nasa.pds.pds4+json'}
        #        )
        if not 'Accept' in _header_params:
            _header_params['Accept'] = self.api_client.select_header_accept(
                ['*/*', 'application/csv', 'application/json', 'application/kvp+json', 'application/vnd.nasa.pds.pds4+json', 'application/vnd.nasa.pds.pds4+xml', 'application/xml', 'text/csv', 'text/html', 'text/xml'])  # noqa: E501

        # authentication setting
        _auth_settings = []  # noqa: E501

        # the 200 object depends on the Accept header value
        # and can be found from the OPENAPI specification used as input
        # See for example https://github.com/NASA-PDS/pds-api/blob/main/specs/PDS_APIs-search-1.1.1-swagger.yaml#L1251:L1286
        _response_types_map = {
            '200': get_subtype_map(
                              'Plural',  # super type used as a generic type for multiple Accept header values
                              _header_params['Accept']
                        ),
            '400': "ErrorMessage",
            '404': "ErrorMessage",
            '500': "ErrorMessage",
            '501': "ErrorMessage",
        }
  ...
wing328 commented 1 year ago

@tloubrieu-jpl thanks for reporting the issue. Can you please PM me via Slack when you've time?

https://join.slack.com/t/openapi-generator/shared_invite/zt-12jxxd7p2-XUeQM~4pzsU9x~eGLQqX2g

tloubrieu-jpl commented 12 months ago

Hi @wing328 , any update on this issue ? Thanks

Leszeg commented 3 months ago

Hi @wing328 , I am bumping up. Any update on this issue ? Thanks

gmiretti commented 3 months ago

This is still happening using python generator for

$  openapi-generator version
7.4.0

Is someone working on this?