Search code examples
openapiopenapi-generatoropenapi-generator-cli

Is there any way as of today to instruct openapi-generator to add a Header and a fixed Header Value for a particular HTTP request in the client code?


I need the generated clients to forward a fixed Key Value pair as the Header. The generated client library should not have the Header as a method input. I expect the library to add the Header and Header value automatically.

https://i.sstatic.net/cEDB2.jpg

As per the Constant Parameter documentation, I have tried adding below in the OpenAPI Document parameters section.

        {
          "name": "X-CUSTOM-HEADER",
          "in": "header",
          "schema": {
            "type" : "string",
            "enum" : ["FIXED_VALUE"]
          },
          "required" : true

This adds the "X-CUSTOM-HEADER" as an argument to the method. My expectation is that generated library code should hard code this value.


Solution

  • At the time of posting this question there was no way in OpenAPI generator to generate code that hardcodes the value of contants.
    Filed feature request : https://github.com/OpenAPITools/openapi-generator/issues/16547
    Also, Contributed in the implementation for Java(OkHttp) code generation : https://github.com/OpenAPITools/openapi-generator/pull/16550
    As of today, We can use autosetConstants property to automatically hardcode the constants in the generated code. Property name is subject to change but the plan is to support this feature. Support for const keyword is also planned.