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

Fix: Respect parent properties in case of merged schemas by allOf

Open musaprg opened this issue 2 years ago • 2 comments

fixes #697

Currently, oapi-codegen ignores original (parent) fields if allOf is defined. OpenAPI schema doesn't seem to refuse a combination of general fields (properties) and allOf.

For example, the following schema

    PersonWithMorePropertiesOutsideOfAllOf:
      type: object
      description: |
        This is a person record as returned from a Create endpoint. It contains
        all the fields of a Person, with an additional property outside of allOf directives.
      properties:
        additionalProperty:
          type: string
      required: [ additionalProperty ]
      allOf:
        - $ref: "#/components/schemas/Person"

will be converted into the following type definition, which seems to be wrong result.

type PersonWithMorePropertiesOutsideOfAllOf = Person

The expected result would be like:

type PersonWithMorePropertiesOutsideOfAllOf struct {
	// Embedded struct due to allOf(#/components/schemas/Person)
	Person `yaml:",inline"`
	// Embedded fields due to inline allOf schema
	AdditionalProperty string `json:"additionalProperty"`
}

The known approach is normalization as proposed in other similar project like REFACTOR_ALLOF_WITH_PROPERTIES_ONLY in opneapi-generator. This PR is inspired by the idea.

REFACTOR_ALLOF_WITH_PROPERTIES_ONLY: When set to true, refactor schema with allOf and properties in the same level to a schema with allOf only and, the allOf contains a new schema containing the properties in the top level. https://github.com/OpenAPITools/openapi-generator/blob/8b5b5a74c333b809c5a651366656257ec8a6fef3/docs/customization.md?plain=1#L567

musaprg avatar Jan 02 '24 15:01 musaprg

@jamietanna Would you take a look at this PR? Thanks.

musaprg avatar Jan 27 '24 00:01 musaprg