Skip to content

Commit 3943036

Browse files
committed
feat(embedding): allof embedding structs impl
1 parent a427fd8 commit 3943036

5 files changed

Lines changed: 66 additions & 36 deletions

File tree

internal/test/all_of/openapi.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,15 @@ components:
5151
type: integer
5252
format: int64
5353
required: [ ID ]
54+
PersonWithRole:
55+
type: object
56+
description: |
57+
This tests a schema that has both its own properties and an allOf reference.
58+
The generated code should embed the allOf ref and inline the own properties.
59+
properties:
60+
Role:
61+
type: string
62+
allOf:
63+
- $ref: "#/components/schemas/Person"
64+
required:
65+
- Role

internal/test/all_of/v1/openapi.gen.go

Lines changed: 21 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/test/all_of/v2/openapi.gen.go

Lines changed: 21 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/codegen/merge_schemas.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,20 @@ import (
1313
func MergeSchemas(schema *openapi3.Schema, path []string) (Schema, error) {
1414
allOf := schema.AllOf
1515

16+
// If the parent schema has its own properties alongside allOf,
17+
// include them as a synthetic schema in the allOf list so they
18+
// are not lost during merging.
19+
if len(schema.Properties) > 0 {
20+
parentOnly := *schema
21+
parentOnly.AllOf = nil
22+
allOf = append(allOf, openapi3.NewSchemaRef("", &parentOnly))
23+
}
24+
1625
// If someone asked for the old way, for backward compatibility, return the
1726
// old style result.
18-
if globalState.options.Compatibility.OldMergeSchemas || schema.Extensions[extGoEmbedding] == true {
27+
// Also use v1 merging when the parent has its own properties, because v1
28+
// generates struct embedding for $ref types which preserves the type hierarchy.
29+
if globalState.options.Compatibility.OldMergeSchemas || schema.Extensions[extGoEmbedding] == true || len(schema.Properties) > 0 {
1930
return mergeSchemasV1(allOf, path)
2031
}
2132
return mergeSchemas(allOf, path)

pkg/codegen/schema.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -351,19 +351,6 @@ func GenerateGoSchema(sref *openapi3.SchemaRef, path []string) (Schema, error) {
351351
return mergedSchema, nil
352352
}
353353

354-
// AllOf is interesting, and useful. It's the union of a number of other
355-
// schemas. A common usage is to create a union of an object with an ID,
356-
// so that in a RESTful paradigm, the Create operation can return
357-
// (object, id), so that other operations can refer to (id)
358-
if schema.AllOf != nil {
359-
mergedSchema, err := MergeSchemas(schema, path)
360-
if err != nil {
361-
return Schema{}, fmt.Errorf("error merging schemas: %w", err)
362-
}
363-
mergedSchema.OAPISchema = schema
364-
return mergedSchema, nil
365-
}
366-
367354
// Schema type and format, eg. string / binary
368355
t := schema.Type
369356
// Handle objects and empty schemas first as a special case

0 commit comments

Comments
 (0)