Skip to content

Required inheritance and generated objects when using allOf #531

@florentchauveau

Description

@florentchauveau

Considering the following schema:

    FooProperties:
      type: object
      properties:
        uuid:
          type: string
          format: uuid
          readOnly: true
        firstname:
          type: string
        lastname:
          type: string

    Foo:
      allOf:
        - $ref: "#/components/schemas/FooProperties"
        - type: object
          required:
            - uuid
            - firstname
            - lastname

    NewFoo:
      allOf:
        - $ref: "#/components/schemas/FooProperties"
        - type: object
          required:
            - firstname

According to the specs, also confirmed in the https://editor.swagger.io/, and swagger-api/swagger-editor#1212, NewFoo should have firstname as required, and Foo should have all properties as required.

The validation works as expected.

However, the resulting objects in Go are:

type Foo struct {
	// Embedded struct due to allOf(#/components/schemas/FooProperties)
	FooProperties `yaml:",inline"`
	// Embedded fields due to inline allOf schema
}

// FooProperties defines model for FooProperties.
type FooProperties struct {
	Firstname *string `json:"firstname,omitempty"`
	Lastname  *string `json:"lastname,omitempty"`
	Uuid      *string `json:"uuid,omitempty"`
}

// NewFoo defines model for NewFoo.
type NewFoo struct {
	// Embedded struct due to allOf(#/components/schemas/FooProperties)
	FooProperties `yaml:",inline"`
	// Embedded fields due to inline allOf schema
}

Because of the struct embedding, the properties are all pointers instead of values (like when using "required" directly in objects).

Now, because the validation works as expected, this might not be an issue at all. I wonder however if in this case, if the generated code should embed FooProperties as is, or if a new struct should be generated per "merge".

This has been discussed, and sometimes fixed, in several other projects:

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