Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module github.com/oapi-codegen/oapi-codegen/v2

go 1.20

replace github.com/getkin/kin-openapi => github.com/diamondburned/kin-openapi v0.92.1-0.20240812092412-a5b8ae983de2

require (
github.com/getkin/kin-openapi v0.126.0
github.com/stretchr/testify v1.9.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/getkin/kin-openapi v0.126.0 h1:c2cSgLnAsS0xYfKsgt5oBV6MYRM/giU8/RtwUY4wyfY=
github.com/getkin/kin-openapi v0.126.0/go.mod h1:7mONz8IwmSRg6RttPu6v8U/OJ+gr+J99qSFNjPGSQqw=
github.com/diamondburned/kin-openapi v0.92.1-0.20240812092412-a5b8ae983de2 h1:t2t/uIpi6OkUKIDCWz2YD5VmTFSUYBrQbryi8ZjMUQw=
github.com/diamondburned/kin-openapi v0.92.1-0.20240812092412-a5b8ae983de2/go.mod h1:z5cmU5eqK8Unl7sQCgDdJi44msx2m5Hbaro0V6HTqMw=
github.com/go-openapi/jsonpointer v0.21.0 h1:YgdVicSA9vH5RiHs9TZW5oyafXZFc6+2Vc1rr/O9oNQ=
github.com/go-openapi/jsonpointer v0.21.0/go.mod h1:IUyH9l/+uyhIYQ/PXVA41Rexl+kOkAPDdXEYns6fzUY=
github.com/go-openapi/swag v0.23.0 h1:vsEVJDUo2hPJ2tu0/Xc+4noaxyEffXNIs3cOULZ+GrE=
Expand Down
20 changes: 20 additions & 0 deletions pkg/codegen/merge_schemas.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package codegen
import (
"errors"
"fmt"
"slices"
"strings"

"github.com/getkin/kin-openapi/openapi3"
Expand Down Expand Up @@ -204,6 +205,10 @@ func mergeOpenapiSchemas(s1, s2 openapi3.Schema, allOf bool) (openapi3.Schema, e
result.Properties[k] = v
}

// We merge all property keys.
result.PropertyKeys = slices.Concat(s1.PropertyKeys, s2.PropertyKeys)
result.PropertyKeys = compactUsingSet(result.PropertyKeys)

if isAdditionalPropertiesExplicitFalse(&s1) || isAdditionalPropertiesExplicitFalse(&s2) {
result.WithoutAdditionalProperties()
} else if s1.AdditionalProperties.Schema != nil {
Expand All @@ -230,6 +235,21 @@ func mergeOpenapiSchemas(s1, s2 openapi3.Schema, allOf bool) (openapi3.Schema, e
return result, nil
}

// compactUsingSet compacts s by removing duplicates using a set.
// The compaction is done in-place.
func compactUsingSet[T comparable](s []T) []T {
set := make(map[T]struct{}, len(s))
s2 := s[:0]
for _, v := range s {
if _, ok := set[v]; ok {
continue
}
set[v] = struct{}{}
s2 = append(s2, v)
}
return s2
}

func equalTypes(t1 *openapi3.Types, t2 *openapi3.Types) bool {
s1 := t1.Slice()
s2 := t2.Slice()
Expand Down
2 changes: 1 addition & 1 deletion pkg/codegen/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ func GenerateGoSchema(sref *openapi3.SchemaRef, path []string) (Schema, error) {
}

// We've got an object with some properties.
for _, pName := range SortedSchemaKeys(schema.Properties) {
for _, pName := range schema.PropertyKeys {
p := schema.Properties[pName]
propertyPath := append(path, pName)
pSchema, err := GenerateGoSchema(p, propertyPath)
Expand Down