support optional/nullable response headers#2301
Draft
mromaszewicz wants to merge 1 commit intooapi-codegen:mainfrom
Draft
support optional/nullable response headers#2301mromaszewicz wants to merge 1 commit intooapi-codegen:mainfrom
mromaszewicz wants to merge 1 commit intooapi-codegen:mainfrom
Conversation
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>
jamietanna
reviewed
Mar 25, 2026
Member
jamietanna
left a comment
There was a problem hiding this comment.
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?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.