Skip to content

support optional/nullable response headers#2301

Draft
mromaszewicz wants to merge 1 commit intooapi-codegen:mainfrom
mromaszewicz:fix/issue-2267
Draft

support optional/nullable response headers#2301
mromaszewicz wants to merge 1 commit intooapi-codegen:mainfrom
mromaszewicz:fix/issue-2267

Conversation

@mromaszewicz
Copy link
Member

Closes: #2267

Response headers in strict server code were always generated as direct value types regardless of whether they were required. This adds support for optional and nullable response headers in the strict-interface and strict-responses templates.

Add Required and Nullable fields to ResponseHeaderDefinition, populated from the OpenAPI header object. Add GoTypeDef(), IsOptional(), and IsNullable() methods that mirror the existing Property logic for determining whether a field should be a pointer, nullable.Nullable[T], or a direct value.

Update strict-interface.tmpl and strict-responses.tmpl to use GoTypeDef for struct field types, and to conditionally guard w.Header().Set() calls with nil/IsSpecified checks for optional/nullable headers.

Note: this is a breaking change for specs where response headers lack explicit required: true, since the OpenAPI default is false. Existing headers will change from direct values to pointers. This may need to be gated behind a config option.

Closes: oapi-codegen#2267

Response headers in strict server code were always generated as direct
value types regardless of whether they were required. This adds support
for optional and nullable response headers in the strict-interface and
strict-responses templates.

Add Required and Nullable fields to ResponseHeaderDefinition, populated
from the OpenAPI header object. Add GoTypeDef(), IsOptional(), and
IsNullable() methods that mirror the existing Property logic for
determining whether a field should be a pointer, nullable.Nullable[T],
or a direct value.

Update strict-interface.tmpl and strict-responses.tmpl to use GoTypeDef
for struct field types, and to conditionally guard w.Header().Set()
calls with nil/IsSpecified checks for optional/nullable headers.

Note: this is a breaking change for specs where response headers lack
explicit `required: true`, since the OpenAPI default is false. Existing
headers will change from direct values to pointers. This may need to be
gated behind a config option.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@mromaszewicz mromaszewicz requested a review from a team as a code owner March 23, 2026 21:33
@mromaszewicz mromaszewicz added the ☢️ breaking change This change would break existing users' code label Mar 23, 2026
@mromaszewicz mromaszewicz marked this pull request as draft March 23, 2026 22:04
Copy link
Member

@jamietanna jamietanna left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per offline discussion, this makes sense to do - we should correctly add this behaviour to make sure that we're reflecting what is the "correct" default

As we've said, I think adding a CompatibilityOption for this, so folks can revert back to the old behaviour would be best

But I'm of the opinion that moving folks over by default to the correct code path (which is technically breaking their generated code) is best

Do we know if we could use go:fix or anything to help them migrate more easily?

@jamietanna jamietanna added the notable changes Used for release notes to highlight these more highly label Mar 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

☢️ breaking change This change would break existing users' code notable changes Used for release notes to highlight these more highly

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support nullable response headers

2 participants