Skip to content

Commit bf6c769

Browse files
mromaszewiczclaude
andcommitted
Add HeadersImplicitlyRequired compatibility flag for #2267
Allows users to restore the pre-v2.5.0 behavior where all response headers are treated as required, ignoring the OpenAPI `required` field. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 603e70f commit bf6c769

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

configuration-schema.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,10 @@
115115
"preserve-original-operation-id-casing-in-embedded-spec": {
116116
"type": "boolean",
117117
"description": "When `oapi-codegen` parses the original OpenAPI specification, it will apply the configured `output-options.name-normalizer` to each operation's `operationId` before that is used to generate code from.\nHowever, this is also applied to the copy of the `operationId`s in the `embedded-spec` generation, which means that the embedded OpenAPI specification is then out-of-sync with the input specificiation.\nTo ensure that the `operationId` in the embedded spec is preserved as-is from the input specification, set this. NOTE that this will not impact generated code.\nNOTE that if you're using `include-operation-ids` or `exclude-operation-ids` you may want to ensure that the `operationId`s used are correct."
118+
},
119+
"headers-implicitly-required": {
120+
"type": "boolean",
121+
"description": "Treats all response headers as required, ignoring the `required` property from the header definition. Prior to v2.6.0, oapi-codegen generated all response headers as direct values (implicitly required). The OpenAPI specification defaults headers to optional (required: false), so the corrected behavior generates optional headers as pointers. Set this to true to restore the old behavior where all headers are treated as required.\nPlease see https://github.com/oapi-codegen/oapi-codegen/issues/2267"
118122
}
119123
}
120124
},

pkg/codegen/configuration.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,16 @@ type CompatibilityOptions struct {
287287
// NOTE that this will not impact generated code.
288288
// NOTE that if you're using `include-operation-ids` or `exclude-operation-ids` you may want to ensure that the `operationId`s used are correct.
289289
PreserveOriginalOperationIdCasingInEmbeddedSpec bool `yaml:"preserve-original-operation-id-casing-in-embedded-spec"`
290+
291+
// HeadersImplicitlyRequired treats all response headers as required, ignoring
292+
// the `required` property from the header definition. Prior to v2.6.0,
293+
// oapi-codegen generated all response headers as direct values (implicitly
294+
// required). The OpenAPI specification defaults headers to optional
295+
// (required: false), so the corrected behavior generates optional headers as
296+
// pointers. Set this to true to restore the old behavior where all headers
297+
// are treated as required.
298+
// Please see https://github.com/oapi-codegen/oapi-codegen/issues/2267
299+
HeadersImplicitlyRequired bool `yaml:"headers-implicitly-required,omitempty"`
290300
}
291301

292302
func (co CompatibilityOptions) Validate() map[string]string {

pkg/codegen/operations.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -947,7 +947,7 @@ func GenerateResponseDefinitions(operationID string, responses map[string]*opena
947947
Name: headerName,
948948
GoName: SchemaNameToTypeName(headerName),
949949
Schema: contentSchema,
950-
Required: header.Value.Required,
950+
Required: header.Value.Required || globalState.options.Compatibility.HeadersImplicitlyRequired,
951951
Nullable: nullable,
952952
}
953953
responseHeaderDefinitions = append(responseHeaderDefinitions, headerDefinition)

0 commit comments

Comments
 (0)