3
votes

I am using swagger-codegen for generating a Java REST client for one of my REST APIs. The REST APIs take an optional header parameter. The generated methods in the client have an additional parameter that takes the header. I would like the methods to be generated without the header parameter in the method signature. I have read the documentation, but couldn't find any reference.

For example, for a GET all API with option X-CUSTOM-HEADER parameter, swagger-codegen generates a method like below:

public List<SomeType> findAllUsingGET1(String optionalHeader)

where as I would like it to be:

public List<SomeType> findAllUsingGET1()

Looking for pointers for the workaround rather than customizing the client-code generation.

EDIT 1: Adding the JSON spec

  "get": {
    "summary": "findAll",
    "operationId": "findAllUsingGET1",
    "consumes": [
      "application/json"
    ],
    "produces": [
      "application/json"
    ],
    "parameters": [
      {
        "name": "X-CUSTOM-HEADER",
        "in": "header",
        "description": "Custom Header",
        "required": false,
        "type": "string"
      }
    ],
    "responses": {
      "200": {
        "description": "OK",
        "schema": {
          "type": "string"
        }
      },
      "401": {
        "description": "Unauthorized"
      },
      "403": {
        "description": "Forbidden"
      },
      "404": {
        "description": "Not Found"
      }
    }
  }
1
Could you include the part of the json/yaml that gets parsed to that GET? - moondaisy
@moondaisy - Added the JSON snippet of the API spec. - Mubin
Please explain a bit more on why you would like the methods to be generated without the header parameter in the method signature. If that parameter is really optional, then you can simply remove it from the spec so that swagger codegen won't include it in the Java method signature - William Cheng
@wing328 - I would like the header to be added as a header on the REST call rather than passing it as a parameter to the method. So something like setting up the header on the ApiClient.addDefaultHeader. The custom header is actually not optional, it is mandatory. I had to change it to optional so that I don't have to pass it in every method call. Anyways, if something is optional, then it doesn't means that we should remove it from the spec.. right? - Mubin

1 Answers

5
votes

If you want to remove a parameter (optional) from the method signature in the Java API client, the only way is to remove the parameter from the Swagger/OpenAPI specification.

To add default headers, you can use the addDefaultHeader method in ApiClient: https://github.com/swagger-api/swagger-codegen/blob/master/samples/client/petstore/java/okhttp-gson/src/main/java/io/swagger/client/ApiClient.java#L528

UPDATE: Header parameters, similar to form, query parameters, are generated as method arguments. From the developer perspective, it's just another parameter (and they do not need to know whether the parameter is a header, form or query parameter)