Skip to content

fix(strict-server): delegate JSON marshalling for allOf unions and additionalProperties - #2552

Merged
mromaszewicz merged 2 commits into
oapi-codegen:mainfrom
TanbirRamim:fix/strict-response-marshal-allof-additionalprops
Sep 24, 2026
Merged

mromaszewicz merged 2 commits into
oapi-codegen:mainfrom
TanbirRamim:fix/strict-response-marshal-allof-additionalprops

Conversation

@TanbirRamim

Copy link
Copy Markdown
Contributor

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 generate only changes that fixture, and go test ./pkg/codegen/... plus the aggregates, servers/strict and bodies/responses tests pass.

Fixes #2549

…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
@TanbirRamim
TanbirRamim marked this pull request as ready for review September 13, 2026 00:11
@TanbirRamim
TanbirRamim requested a review from a team as a code owner September 13, 2026 00:11
Copilot AI lite review requested due to automatic review settings September 13, 2026 00:11

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@greptile-apps

greptile-apps Bot commented Sep 13, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

The PR appears safe to merge; no actionable regression or outstanding blocking issue was identified.

Summary

This PR fixes strict-server JSON serialization so response visitors marshal the generated model rather than bypassing its custom JSON methods.

  • Detects custom marshalling generated for allOf-composed unions, additional properties, and renamed model wrappers.
  • Updates strict templates consistently across standard, Fiber, and Iris response writers.
  • Adds focused generator and integration coverage for referenced, inline, renamed, and combined union/additional-properties schemas.
  • Regenerates representative strict-server fixtures across supported router backends.

Reviews (2) · Last reviewed commit: "fix(strict-server): delegate by the gene..."

…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
mromaszewicz merged commit 2d153c6 into oapi-codegen:main Sep 24, 2026
15 checks passed
@mromaszewicz mromaszewicz added the bug Something isn't working label Oct 24, 2026
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.

strict-server: response types silently drop data for allOf-composed unions and properties + additionalProperties

3 participants