Repository navigation
fix: keep a strict response's MarshalJSON behind allOf: [$ref X] - #2575
Merged
mromaszewicz merged 3 commits intoSep 24, 2026
Merged
mromaszewicz merged 3 commits into
mromaszewicz merged 3 commits into
Conversation
In v2, `allOf: [$ref X]` generates as an alias of X. When X has a generated MarshalJSON, for additionalProperties or a union, the strict server's response envelope, a defined type over the alias, needs a delegating MarshalJSON (oapi-codegen#2549). generatesMarshalJSON, which decides that, couldn't see through the alias, so the envelope had none, and X's additional properties or union data were silently dropped from the response. generateAllOfV2 now records the type such a composition is an alias of, as generateAllOfV3 already does (oapi-codegen#2574), and the check follows it. This only adds a MarshalJSON method to the envelopes of such responses; no generated fixture in the repository changes. v1 is unaffected: it generates a struct that embeds X. A new matrix shape, allof_single_ref_custom_json, covers it under v1, v2 and v3. Its v2 cell lost the additional properties at every body position before this fix. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Contributor
|
…nt cells
CI failed on the v1 cell of allof_single_ref_custom_json, which passes
on Go 1.27 but not on the 1.25 and 1.26 that CI runs. v1 generates
`struct { Open }`, embedding the referenced type in an unnamed struct.
Before Go 1.27, encoding/json doesn't call Open's UnmarshalJSON for list
and map elements of such a struct, so their additional properties were
lost. This predates the fix and v1 doesn't use it; the cell is marked
broken for v1, with that reason, so its generated code stays pinned
while its round trips are skipped.
Review asked for a union round trip too: allof_single_ref_union does
the same for `allOf: [$ref Either]` over a oneOf, under v1, v2 and v3,
with v1 marked broken for the same reason. Without the fix, its v2 cell
loses the union's data at every body position.
The new cells pass on Go 1.25, 1.26 and 1.27.
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
A shape's `broken:` entry skipped every round trip of a version, so
marking v1 broken for the two allOf: [$ref X] shapes also dropped the
positions v1 handles, although only holder-inline fails: the list and
map elements of v1's unnamed `struct { X }`, before Go 1.27.
A `broken:` entry can now map positions to reasons, and only those
positions are skipped:
broken:
v1:
holder-inline: v1 embeds Open in an unnamed struct. ...
The existing form, a reason for every position, is unchanged, and so is
every cell that uses it. The two shapes now run v1's other eight
positions, on Go 1.25, 1.26 and 1.27.
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
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.
In v2, a response whose schema is a single-member
allOf: [$ref X]loses data whenXhas a generatedMarshalJSON, which it has foradditionalPropertiesor a union:Subjectgenerates astype Subject = Open. The strict server wraps the response in a defined type over it,type GetX200JSONResponse Subject, which has none ofOpen's methods. It therefore needs a delegatingMarshalJSON(#2549).generatesMarshalJSONdecides whether it gets one, and couldn't see through the alias. So the envelope had no delegating method, and{"b": "x", "k": "v"}went out as{"b": "x"}. This is a pre-existing bug, found while working on #2574.Fix
generateAllOfV2now records the type such a composition is an alias of, the samealiasOfthatgenerateAllOfV3sets since #2574, andgeneratesMarshalJSONfollows it. This adds aMarshalJSONmethod to the affected envelopes and changes nothing else.make generatechanges no committed file, so no fixture in the repository hit this. v1 is unaffected: it generates a struct that embedsOpen, which already worked.Tests
allof_single_ref_custom_json, runs under v1, v2 and v3. Before this fix, its v2 cell lost the additional properties at every request and response body position. All three cells now round-trip the sample.TestSingleRefAllOfKeepsCustomJSONV2checks that the envelopes delegate, for both anadditionalPropertiestarget and a union target.🤖 Generated with Claude Code