diff --git a/internal/test/issues/issue-1476/config.yaml b/internal/test/issues/issue-1476/config.yaml new file mode 100644 index 0000000000..f1852fb7d5 --- /dev/null +++ b/internal/test/issues/issue-1476/config.yaml @@ -0,0 +1,7 @@ +# yaml-language-server: $schema=../../../../configuration-schema.json +package: issue1476 +generate: + models: true +output: issue1476.gen.go +output-options: + skip-prune: true diff --git a/internal/test/issues/issue-1476/doc.go b/internal/test/issues/issue-1476/doc.go new file mode 100644 index 0000000000..0f76e432d7 --- /dev/null +++ b/internal/test/issues/issue-1476/doc.go @@ -0,0 +1,3 @@ +package issue1476 + +//go:generate go run github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen --config=config.yaml spec.yaml diff --git a/internal/test/issues/issue-1476/issue1476.gen.go b/internal/test/issues/issue-1476/issue1476.gen.go new file mode 100644 index 0000000000..fb5d753861 --- /dev/null +++ b/internal/test/issues/issue-1476/issue1476.gen.go @@ -0,0 +1,20 @@ +// Package issue1476 provides primitives to interact with the openapi HTTP API. +// +// Code generated by github.com/oapi-codegen/oapi-codegen/v2 version (devel) DO NOT EDIT. +package issue1476 + +// Dog defines model for Dog. +type Dog struct { + Name *string `json:"name,omitempty"` + Pet *struct { + Id int64 `json:"id"` + Tag *string `json:"tag,omitempty"` + } `json:"-"` + PetId *int64 `json:"petId,omitempty"` +} + +// Pet defines model for Pet. +type Pet struct { + Id int64 `json:"id"` + Tag *string `json:"tag,omitempty"` +} diff --git a/internal/test/issues/issue-1476/spec.yaml b/internal/test/issues/issue-1476/spec.yaml new file mode 100644 index 0000000000..c99d497139 --- /dev/null +++ b/internal/test/issues/issue-1476/spec.yaml @@ -0,0 +1,26 @@ +openapi: "3.0.0" +components: + schemas: + Pet: + type: object + required: + - id + properties: + id: + type: integer + format: int64 + tag: + type: string + Dog: + type: object + properties: + pet: + allOf: + - $ref: "#/components/schemas/Pet" + - type: object + x-go-json-ignore: true + petId: + type: integer + format: int64 + name: + type: string diff --git a/pkg/codegen/schema.go b/pkg/codegen/schema.go index f0054a8671..f8fe22d514 100644 --- a/pkg/codegen/schema.go +++ b/pkg/codegen/schema.go @@ -391,6 +391,16 @@ func GenerateGoSchema(sref *openapi3.SchemaRef, path []string) (Schema, error) { // We've got an object with some properties. for _, pName := range SortedSchemaKeys(schema.Properties) { p := schema.Properties[pName] + + // Merge allOf in property + if p.Value.AllOf != nil { + val, err := mergeAllOf(p.Value.AllOf) + if err != nil { + return Schema{}, fmt.Errorf("error generating Go schema from allOf for property '%s': %w", pName, err) + } + p.Value = &val + } + propertyPath := append(path, pName) pSchema, err := GenerateGoSchema(p, propertyPath) if err != nil {