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
21.9k stars 6.58k forks source link

Question: How to add accept or contentType header as method parameter in generated webclient method? #17012

Open metalpalo opened 1 year ago

metalpalo commented 1 year ago

Hi

We have defined our OAS(openapi.yml) with one GET operation where response can different contents for status 200 like this:

    DummyResponse:
      description: Dummy response
      content:
        "application/json":
          schema:
            $ref: "#/components/schemas/DummyObject"
        "application/json;version=2":
          schema:
            $ref: "#/components/schemas/DummyObjectV2"
        "application/json;version=3":
          schema:
            $ref: "#/components/schemas/DummyObjectV3"

When springboot application starts and swagger page is opened we are able to choose concrete value from combobox like this: image

Now we want to generate apiclient via openapi-generator-maven-plugin(4.3.1) with following config:

<interfaceOnly>false</interfaceOnly>
<useOptional>true</useOptional>
<skipDefaultInterface>true</skipDefaultInterface>
<dateLibrary>java8</dateLibrary>
<java8>true</java8>
<library>webclient</library>

with result that method will contains accept header as parameter:

Object getDummy(List accept) or Object getDummy(String accept)

Of course we can define accept header in yaml file and put it as parameter but then we have duplicated field in rendered swagger

components:
  parameters:
    Accept:
      name: accept
      in: header
      required: false
      schema:
        type: array
        items:
          type: string
        default: ["application/json"]

Can somebody help me how to call our generated method via webclient with full control which accept header to be passed?

thanks brpalo

darshan-au commented 11 months ago

+1 on this request

We are currently experiencing a problem where certain headers are getting dropped from the generated api client class. We need an option to allow the generated methods to accept custom headers as arguments

cingaldi commented 8 months ago

+1 to this proposal

wing328 commented 8 months ago

one approach is to add a defaultHeaders (list of string) in ApiClient.java to allow users to provide custom headers in the request.

if anyone wants to contribute a PR or sponsor the enhancement, please reply to let us know.

jorgerod commented 2 months ago

one approach is to add a defaultHeaders (list of string) in ApiClient.java to allow users to provide custom headers in the request.

if anyone wants to contribute a PR or sponsor the enhancement, please reply to let us know.

Hi @wing328

I tried adding defaultHeaders and it didn't work.

Related issue: