You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Generate client using oneOf in request body works wrong #1367
Generating client that has request body with oneOf leads to problem with creating valid request.
Generated models has invalid inheriting and seems like one json body model is redundant
oapi-codegen -package api -generate types,client pet.openapi.yaml > client.gen.go
client.gen.go
type ClientWithResponsesInterface interface {
...
PatchPetsWithResponse(ctx context.Context, body PatchPetsJSONRequestBody, reqEditors ...RequestEditorFn) (*PatchPetsResponse, error)
}
// PatchPetsJSONRequestBody defines body for PatchPets for application/json ContentType.
type PatchPetsJSONRequestBody PatchPetsJSONBody
// PatchPetsJSONBody defines parameters for PatchPets.
type PatchPetsJSONBody struct {
union json.RawMessage
}
// CreatePetRequestBody defines model for CreatePetRequestBody.
type CreatePetRequestBody struct {
union json.RawMessage
}
Generated models CreatePetRequestBody and PatchPetsJSONBody with the same go type definition. PatchPetsJSONBody has no methods at all, bcs of that marshalled request is empty, but CreatePetRequestBody has AsPet / AsDog / Marshal / Unmarshal methods.
Seems like it should be type PatchPetsJSONRequestBody CreatePetRequestBody
This is fixed on main by #2501. PatchPetsJSONRequestBody is a type defined over PatchPetsJSONBody, so it didn't get the union's MarshalJSON, and the client sent {}. It now has a MarshalJSON and UnmarshalJSON that delegate to the union's, so the request carries the variant you set. With your spec:
The As*/From* methods are on PatchPetsJSONBody, as above, and the request body still gets a union type of its own rather than reusing CreatePetRequestBody. This applies to every schema-merging-behavior, including the default, so no configuration change is needed. There's no release with the fix yet, so you'll need the latest main, for example go run github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen@main.
Activity
jamietanna commentedon Sep 20, 2024
FYI this is the same issue as @lzurbriggen noted in #1178 (comment)
output:
mromaszewicz commentedon Sep 26, 2026
This is fixed on
mainby #2501.PatchPetsJSONRequestBodyis a type defined overPatchPetsJSONBody, so it didn't get the union'sMarshalJSON, and the client sent{}. It now has aMarshalJSONandUnmarshalJSONthat delegate to the union's, so the request carries the variant you set. With your spec:The
As*/From*methods are onPatchPetsJSONBody, as above, and the request body still gets a union type of its own rather than reusingCreatePetRequestBody. This applies to everyschema-merging-behavior, including the default, so no configuration change is needed. There's no release with the fix yet, so you'll need the latestmain, for examplego run github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen@main.This message was generated by AI.