简体   繁体   中英

swagger-codegen header parameter for Java REST Client

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"
      }
    }
  }

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM