Skip to content

fix: keep a strict response's MarshalJSON behind allOf: [$ref X] - #2575

Merged
mromaszewicz merged 3 commits into
oapi-codegen:mainfrom
mromaszewicz:fix/single-allof-marshal-delegation
Sep 24, 2026
Merged

mromaszewicz merged 3 commits into
oapi-codegen:mainfrom
mromaszewicz:fix/single-allof-marshal-delegation

Conversation

@mromaszewicz

Copy link
Copy Markdown
Member

In v2, a response whose schema is a single-member allOf: [$ref X] loses data when X has a generated MarshalJSON, which it has for additionalProperties or a union:

Open:
  type: object
  properties:
    b: {type: string}
  additionalProperties: {type: string}
Subject:
  allOf:
    - $ref: '#/components/schemas/Open'

Subject generates as type Subject = Open. The strict server wraps the response in a defined type over it, type GetX200JSONResponse Subject, which has none of Open's methods. It therefore needs a delegating MarshalJSON (#2549). generatesMarshalJSON decides 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

generateAllOfV2 now records the type such a composition is an alias of, the same aliasOf that generateAllOfV3 sets since #2574, and generatesMarshalJSON follows it. This adds a MarshalJSON method to the affected envelopes and changes nothing else. make generate changes no committed file, so no fixture in the repository hit this. v1 is unaffected: it generates a struct that embeds Open, which already worked.

Tests

  • A new matrix shape, 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.
  • TestSingleRefAllOfKeepsCustomJSONV2 checks that the envelopes delegate, for both an additionalProperties target and a union target.

🤖 Generated with Claude Code

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]>
@mromaszewicz
mromaszewicz requested a review from a team as a code owner September 24, 2026 22:28
@greptile-apps

greptile-apps Bot commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

The PR appears safe to merge; no outstanding findings or new actionable issues were identified.

Summary

The PR records alias targets for v2 single-reference allOf schemas so strict response envelopes can delegate custom JSON marshaling.

  • Adds additional-properties and union matrix fixtures across v1, v2, and v3.
  • Narrows the v1 test skips to the affected inline-holder position.

Reviews (3) · Last reviewed commit: "test: skip only the matrix positions a v..."

mromaszewicz and others added 2 commits September 24, 2026 15:38
…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]>
@mromaszewicz
mromaszewicz merged commit 05455ad into oapi-codegen:main Sep 24, 2026
15 checks passed
@mromaszewicz mromaszewicz added the bug Something isn't working label Sep 24, 2026
@mromaszewicz
mromaszewicz deleted the fix/single-allof-marshal-delegation branch September 26, 2026 00:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant