From 66a9014d473afd3208a6526c5055b95b3a044034 Mon Sep 17 00:00:00 2001 From: Hidden Marten Date: Wed, 11 Sep 2024 23:45:21 +0200 Subject: [PATCH 1/2] fix: ensure extensions are applied from the allOf section, specifically for properties --- internal/test/issues/issue-1476/config.yaml | 7 +++++ internal/test/issues/issue-1476/doc.go | 3 +++ .../test/issues/issue-1476/issue1476.gen.go | 20 ++++++++++++++ internal/test/issues/issue-1476/spec.yaml | 26 +++++++++++++++++++ pkg/codegen/schema.go | 9 +++++++ 5 files changed, 65 insertions(+) create mode 100644 internal/test/issues/issue-1476/config.yaml create mode 100644 internal/test/issues/issue-1476/doc.go create mode 100644 internal/test/issues/issue-1476/issue1476.gen.go create mode 100644 internal/test/issues/issue-1476/spec.yaml 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 3d4bdbbddb..1e4b323003 100644 --- a/pkg/codegen/schema.go +++ b/pkg/codegen/schema.go @@ -417,6 +417,15 @@ func GenerateGoSchema(sref *openapi3.SchemaRef, path []string) (Schema, error) { if p.Value != nil { description = p.Value.Description } + + // Add extensions for properties from AllOf + p.Value.Extensions = make(map[string]interface{}) + for _, pva := range p.Value.AllOf { + for key, value := range pva.Value.Extensions { + p.Value.Extensions[key] = value + } + } + prop := Property{ JsonFieldName: pName, Schema: pSchema, From def7b93ce9ccefe2cac723b8da1e73c462fb71c6 Mon Sep 17 00:00:00 2001 From: Hidden Marten Date: Thu, 12 Sep 2024 14:13:29 +0200 Subject: [PATCH 2/2] fix: a bit clearer solution --- pkg/codegen/schema.go | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/pkg/codegen/schema.go b/pkg/codegen/schema.go index 1e4b323003..ab1a0a1757 100644 --- a/pkg/codegen/schema.go +++ b/pkg/codegen/schema.go @@ -389,6 +389,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 { @@ -417,15 +427,6 @@ func GenerateGoSchema(sref *openapi3.SchemaRef, path []string) (Schema, error) { if p.Value != nil { description = p.Value.Description } - - // Add extensions for properties from AllOf - p.Value.Extensions = make(map[string]interface{}) - for _, pva := range p.Value.AllOf { - for key, value := range pva.Value.Extensions { - p.Value.Extensions[key] = value - } - } - prop := Property{ JsonFieldName: pName, Schema: pSchema,