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

fix: allow x-go-type and x-go-type-skip-optional-pointer for allOf

Open turip opened this issue 1 year ago • 0 comments

Right now if a schema contains x-go-type, for example:

components:
  schemas:
    Pet:
      x-go-type: pet.Pet
      x-go-type-import:
        path: github.com/somepetproject/pkg/pet
      type: object
      required:
        - pet_type
      properties:
        pet_type:
          type: string
      discriminator:
        propertyName: pet_type
    Dog:     # "Dog" is a value for the pet_type property (the discriminator value)
      x-go-type: dog.Dog
      x-go-type-import:
        path: github.com/somepetproject/pkg/dog
      allOf: # Combines the main `Pet` schema with `Dog`-specific properties 
        - $ref: '#/components/schemas/Pet'
        - type: object
          # all other properties specific to a `Dog`
          properties:
            bark:
              type: boolean
            breed:
              type: string
              enum: [Dingo, Husky, Retriever, Shepherd]
    Cat:     # "Cat" is a value for the pet_type property (the discriminator value)
      x-go-type: cat.Cat
      x-go-type-import:
        path: github.com/somepetproject/pkg/cat
      allOf: # Combines the main `Pet` schema with `Cat`-specific properties 
        - $ref: '#/components/schemas/Pet'
        - type: object
          # all other properties specific to a `Cat`
          properties:
            hunts:
              type: boolean
            age:
              type: integer

old behavior

The generated code will still take the pet as the x-go-type:


	"github.com/getkin/kin-openapi/openapi3"
	"github.com/labstack/echo/v4"
	"github.com/somepetproject/pkg/pet"
)

// Cat defines model for Cat.
type Cat = pet.Pet

// Dog defines model for Dog.
type Dog = pet.Pet

// Pet defines model for Pet.
type Pet = pet.Pet

new behavior

This patch makes sure that we are generating the expected output:

	"github.com/somepetproject/pkg/cat"
	"github.com/somepetproject/pkg/dog"
	"github.com/somepetproject/pkg/pet"
)

// Cat defines model for Cat.
type Cat = cat.Cat

// Dog defines model for Dog.
type Dog = dog.Dog

// Pet defines model for Pet.
type Pet = pet.Pet

This is a corner case, as in most cases the same type is used (e.g. splitting the ID from the CreateRequest but using the same underlying object), but in case we would want to compose the objects it would make sense to make this overridable on the parent level.

This patch doesn't introduce a breaking change, as if the x-go-* values are not set, the same inheritance will hapen.

turip avatar May 14 '24 19:05 turip