Skip to content

fix: delegate JSON methods for inline union request bodies - #2501

Merged
mromaszewicz merged 1 commit into
oapi-codegen:mainfrom
shiiyan:fix-inline-oneof-request-body-json
Jul 31, 2026
Merged

mromaszewicz merged 1 commit into
oapi-codegen:mainfrom
shiiyan:fix-inline-oneof-request-body-json

Conversation

@shiiyan

@shiiyan shiiyan commented Jul 26, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Fixes #2498.

Inline oneOf and anyOf request bodies generate an operation body type with custom JSON methods, followed by a distinct ...JSONRequestBody type. Defined Go types do not inherit methods from their underlying type, so marshaling the request wrapper omits the private union payload and unmarshaling into it discards that payload.

The generated forwarding methods make encoding/json use the existing union implementation for the request wrapper, matching the approach used for strict response wrappers.

Summary

  • generate forwarding MarshalJSON and UnmarshalJSON methods for JSON request body wrappers backed by union types
  • preserve the existing distinct request body types while delegating to their generated inline union implementations
  • add request-wrapper round-trip coverage and regenerate the affected fixtures

E2E testing

OpenAPI schema

paths:
  /demo:
    post:
      operationId: postDemo
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [type]
              properties:
                type:
                  type: string
                  enum: [foo]
              discriminator:
                propertyName: type
                mapping:
                  foo: "#/components/schemas/FooRequest"
              oneOf:
                - $ref: "#/components/schemas/FooRequest"
      responses:
        "202":
          description: accepted

components:
  schemas:
    FooRequest:
      type: object
      required: [type, another]
      properties:
        type:
          type: string
          enum: [foo]
        another:
          type: string

Before: generated by v2.8.0

The inner operation body has the union storage and generated JSON methods, but the request wrapper has no methods:

type PostDemoJSONBody struct {
	Type  PostDemoJSONBodyType `json:"type"`
	union json.RawMessage
}

type PostDemoJSONRequestBody PostDemoJSONBody

Using PostDemoJSONRequestBody directly therefore produces:

marshal:   {"type":"foo"}
unmarshal: AsFooRequest() -> unexpected end of JSON input

The another field stored in the private union payload is lost.

After: generated by this PR

The generated request wrapper delegates to the existing union implementation:

type PostDemoJSONRequestBody PostDemoJSONBody

func (t PostDemoJSONRequestBody) MarshalJSON() ([]byte, error) {
	return PostDemoJSONBody(t).MarshalJSON()
}

func (t *PostDemoJSONRequestBody) UnmarshalJSON(b []byte) error {
	return (*PostDemoJSONBody)(t).UnmarshalJSON(b)
}

The same request-body values now produce:

marshal:   {"another":"value","type":"foo"}
unmarshal: FooRequest{Another:"value", Type:"foo"}

This confirms the generated wrapper preserves the complete oneOf payload in both directions.

Inline union request body wrappers are distinct defined types, so they do not inherit the generated union MarshalJSON and UnmarshalJSON methods. Generate forwarding methods for JSON wrappers and cover the request-body round trip.

Fixes oapi-codegen#2498.

Signed-off-by: SY <[email protected]>
@shiiyan
shiiyan marked this pull request as ready for review July 26, 2026 14:35
@shiiyan
shiiyan requested a review from a team as a code owner July 26, 2026 14:35
@greptile-apps

greptile-apps Bot commented Jul 26, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

Adds JSON method delegation for distinct request-body wrapper types backed by inline unions.

  • Detects request-body schemas whose underlying union type provides custom JSON behavior.
  • Generates forwarding MarshalJSON and UnmarshalJSON methods for non-alias JSON wrappers.
  • Adds round-trip coverage and updates the generated hoisting fixture.

Confidence Score: 5/5

The PR appears safe to merge.

No blocking failure remains.

Important Files Changed

Filename Overview
pkg/codegen/schema.go Adds request-body-specific detection for schemas requiring delegated union JSON methods.
pkg/codegen/templates/request-bodies.tmpl Generates JSON forwarding methods for eligible distinct request-body wrapper types.
internal/test/aggregates/hoisting/hoisting_test.go Adds request-wrapper JSON round-trip regression coverage for an inline root oneOf.
internal/test/aggregates/hoisting/hoisting_implicit.gen.go Regenerates the representative fixture with forwarding methods for path, webhook, and callback request wrappers.

Reviews (2): Last reviewed commit: "fix: delegate JSON methods for inline un..." | Re-trigger Greptile

@shiiyan

shiiyan commented Jul 28, 2026

Copy link
Copy Markdown
Contributor Author

Hi! This is my first contribution to this repository. It looks like the remaining CI workflows require approval before they can run. When someone has a chance, could you please approve the workflows and take a look at the PR? If I’ve missed any project conventions or additional tests, please let me know. Thank you!

@mromaszewicz

Copy link
Copy Markdown
Member

This fix is reasonable, but it's surprising to me that the bug happened. I'm happy to merge it, but let me investigate it a little bit first.

@mromaszewicz mromaszewicz added the bug Something isn't working label Jul 31, 2026
@mromaszewicz

Copy link
Copy Markdown
Member

Yup, this is a good fix. I was afraid I introduced this regression recently, since I did a lot of work on hoisting anonymous types into named types, but I exposed this bug by fixing another one - it would have failed differently before.

Thanks for the fix.

@mromaszewicz
mromaszewicz merged commit b024e15 into oapi-codegen:main Jul 31, 2026
15 checks passed
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.

Inline oneOf JSON request body loses custom marshal/unmarshal behavior

2 participants