6

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"
      }
    }
  }
Mubin
  • 4,192
  • 3
  • 26
  • 45
  • Could you include the part of the json/yaml that gets parsed to that GET? – moondaisy Mar 27 '17 at 18:27
  • @moondaisy - Added the JSON snippet of the API spec. – Mubin Mar 27 '17 at 20:16
  • 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 Mar 28 '17 at 13:35
  • 1
    @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 Mar 28 '17 at 19:24

1 Answers1

7

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)

UPDATE: On May 2018, about 50 top contributors and template creators of Swagger Codegen decided to fork Swagger Codegen to maintain a community-driven version called OpenAPI Generator. Please refer to the Q&A for more information.

William Cheng
  • 10,137
  • 5
  • 54
  • 79
  • 4
    My question is not about removing an optional parameter, it is about removing the optional header from request from the method signature. My only concern is, why does swagger-codegen considers a header request as method parameter? – Mubin Mar 30 '17 at 10:31
  • 1
    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) – William Cheng Mar 30 '17 at 11:45
  • Thanks for the last comment. That clarifies my question, and I conclude what I want cannot be achieved without modifying the swagger spec manually. Can you please update your answer with the comment above so that I can accept it as an answer? – Mubin Mar 31 '17 at 06:39
  • Updated. You can also provide us feedback via https://github.com/swagger-api/swagger-codegen/issues/new – William Cheng Mar 31 '17 at 06:51
  • @WilliamCheng, I believe this feature of removing the parameters is still not available with Swagger-codegen. So I have created this new request for it with providing the use-case information: https://github.com/swagger-api/swagger-codegen/issues/7957 Please note this is relevant for codegen for Server side interface. – Kuldeep Jain Apr 02 '18 at 20:04