Repository navigation
fix(strict-server): delegate JSON marshalling for allOf unions and additionalProperties - #2552
Merged
mromaszewicz merged 2 commits intoSep 24, 2026
Conversation
…ditionalProperties Strict response types are defined types, so they don't inherit the model's MarshalJSON. For a $ref to an allOf containing a oneOf/anyOf, or to an object with properties and additionalProperties, the union or extra fields were silently dropped. Detect those shapes by generating the referenced schema. Fixes oapi-codegen#2549
Contributor
|
…nion
Finishes the approach of the previous commit, which generates the
referenced schema to learn whether its model has custom JSON methods:
- HasCustomMarshalJSON no longer short-circuits on a top-level
oneOf/anyOf in the OpenAPI schema. That shape can collapse to no union
at all: an OpenAPI 3.1 `anyOf: [{type: string}, {type: "null"}]` is a
plain string alias, and the delegator emitted for it did not compile.
- The question it answers is now whether the envelope is an alias or a
defined type. An alias already has the model's methods. A defined type
gets a delegator whenever the model generates marshalling, which is
asked of the generated schema, looking through x-go-type-name.
- The strict visitors (std-http, fiber, iris) encode the response, or
its Body, instead of the raw `.union` field. For a bare union the bytes
are identical; for an inline allOf+oneOf response body, encoding
`.union` dropped the properties the allOf merged in.
Adds the inline allOf+oneOf, x-go-type-name and oneOf+additionalProperties
shapes to the aggregates/oneof union fixture, and a pkg/codegen test that
pins which envelopes get a delegator.
Fixes oapi-codegen#2549
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
mromaszewicz
approved these changes
Sep 24, 2026
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.
Strict response types for a $ref to an allOf containing a oneOf/anyOf, or to an object with both properties and additionalProperties, didn't delegate to the model's MarshalJSON/UnmarshalJSON, so the union branch or the extra fields were silently dropped from the response body. HasCustomMarshalJSON now generates the referenced schema and also returns true when it has union elements or additional properties, extending what #2332 did for direct oneOf/anyOf refs. I added both shapes from the issue to the aggregates/oneof union fixture with tests that encode through the visitor and round-trip; they fail before the change and pass after.
make generateonly changes that fixture, andgo test ./pkg/codegen/...plus the aggregates, servers/strict and bodies/responses tests pass.Fixes #2549