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.stack.imgur.com/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.

2

There are 2 best solutions below

0
Prashant On BEST ANSWER

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.

1
Jeremy Fiel On

Try using default keyword in the schema.

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