Skip to content

Generate client using oneOf in request body works wrong #1367

@Egor-mn

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

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