oapi-codegen icon indicating copy to clipboard operation
oapi-codegen copied to clipboard

Generate client using oneOf in request body works wrong

Open Egor-mn opened this issue 2 years ago • 1 comments

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

Egor-mn avatar Dec 01 '23 20:12 Egor-mn

FYI this is the same issue as @lzurbriggen noted in https://github.com/oapi-codegen/oapi-codegen/pull/1178#issuecomment-1772263434

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.

jamietanna avatar Sep 20 '24 13:09 jamietanna