Repository navigation
fix: delegate JSON methods for inline union request bodies - #2501
Conversation
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]>
Greptile SummaryAdds JSON method delegation for distinct request-body wrapper types backed by inline unions.
Confidence Score: 5/5The PR appears safe to merge. No blocking failure remains.
|
| 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
|
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! |
|
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. |
|
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. |
Problem
Fixes #2498.
Inline
oneOfandanyOfrequest bodies generate an operation body type with custom JSON methods, followed by a distinct...JSONRequestBodytype. 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/jsonuse the existing union implementation for the request wrapper, matching the approach used for strict response wrappers.Summary
MarshalJSONandUnmarshalJSONmethods for JSON request body wrappers backed by union typesE2E testing
OpenAPI schema
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:
Using
PostDemoJSONRequestBodydirectly therefore produces:The
anotherfield stored in the private union payload is lost.After: generated by this PR
The generated request wrapper delegates to the existing union implementation:
The same request-body values now produce:
This confirms the generated wrapper preserves the complete
oneOfpayload in both directions.