Skip to content

Generate client using oneOf in request body works wrong #1367

Closed

Description

@Egor-mn

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

Version: 2.0.0

Example Spec

openapi: "3.0.0"
info:
  version: 1.0.0
  title: Swagger Petstore
paths:
  /pets:
    patch:
      requestBody:
        $ref: '#/components/requestBodies/CreatePetRequestBody'
      responses:
        '200':
          description: Updated
  
components:
  requestBodies:
    CreatePetRequestBody:
      content:
        application/json:
          schema:
            oneOf:
              - $ref: '#/components/schemas/Cat'
              - $ref: '#/components/schemas/Dog'
  schemas:
    Dog:
      type: object
      properties:
        bark:
          type: boolean
        breed:
          type: string
          enum: [Dingo, Husky, Retriever, Shepherd]
    Cat:
      type: object
      properties:
        hunts:
          type: boolean
        age:
          type: integer
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

and model PatchPetsJSONBody should be deleted

Activity

  1. jamietanna commented on Sep 20, 2024

    @jamietanna
    Member

    FYI this is the same issue as @lzurbriggen noted in #1178 (comment)

    FYI, i tried this fork on a schema i had issues with. i have a scenario where the fix outputs the same struct twice:

          operationId: myOperation
          responses:
            "200":
              content:
                application/something.v1+json:
                  schema:
                    type: array
                    items:
                      type: object
                      oneOf:
                      - $ref: '#/components/schemas/Ref1'
                      - $ref: '#/components/schemas/Ref2'
                      - $ref: '#/components/schemas/Ref3'
                application/json:
                  schema:
                    type: array
                    items:
                      type: object
                      oneOf:
                      - $ref: '#/components/schemas/Ref1'
                      - $ref: '#/components/schemas/Ref2'
                      - $ref: '#/components/schemas/Ref3'

    output:

    type MyOperation_200_Item struct {
    	union json.RawMessage
    }
    type MyOperation_200_Item struct {
    	union json.RawMessage
    }

    i don't know why the schema looks the way it does and i have no control over it, but it seems to be valid.

  2. mromaszewicz commented on Sep 26, 2026

    @mromaszewicz
    Member

    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:

    var body PatchPetsJSONBody
    _ = body.FromCat(Cat{Hunts: &hunts})
    client.PatchPetsWithResponse(ctx, PatchPetsJSONRequestBody(body))
    // sends {"hunts":true}

    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.


    This message was generated by AI.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions