Skip to content

Commit

Permalink
Fix bug in oneOf not propagating external references (deepmap#729)
Browse files Browse the repository at this point in the history
  • Loading branch information
technicianted committed Oct 27, 2022
1 parent 3eef8ea commit b812181
Showing 1 changed file with 35 additions and 2 deletions.
37 changes: 35 additions & 2 deletions pkg/codegen/merge_schemas.go
Expand Up @@ -3,6 +3,7 @@ package codegen
import (
"errors"
"fmt"
"strings"

"github.com/getkin/kin-openapi/openapi3"
)
Expand All @@ -25,18 +26,50 @@ func mergeSchemas(allOf []*openapi3.SchemaRef, path []string) (Schema, error) {
return GenerateGoSchema(allOf[0], path)
}

schema := *allOf[0].Value
schema, err := valueWithPropagatedRef(allOf[0])
if err != nil {
return Schema{}, err
}

for i := 1; i < n; i++ {
var err error
schema, err = mergeOpenapiSchemas(schema, *allOf[i].Value)
oneOfSchema, err := valueWithPropagatedRef(allOf[i])
if err != nil {
return Schema{}, err
}
schema, err = mergeOpenapiSchemas(schema, oneOfSchema)
if err != nil {
return Schema{}, fmt.Errorf("error merging schemas for AllOf: %w", err)
}
}
return GenerateGoSchema(openapi3.NewSchemaRef("", &schema), path)
}

// valueWithPropagatedRef returns a copy of ref schema with its Properties refs
// updated if ref itself is external. Otherwise return ref.Value as-is.
func valueWithPropagatedRef(ref *openapi3.SchemaRef) (openapi3.Schema, error) {
if len(ref.Ref) == 0 || ref.Ref[0] == '#' {
return *ref.Value, nil
}

pathParts := strings.Split(ref.Ref, "#")
if len(pathParts) != 2 {
return openapi3.Schema{}, fmt.Errorf("unsupported reference: %s", ref.Ref)
}
remoteComponent, _ := pathParts[0], pathParts[1]

// remote ref
schema := *ref.Value
for _, value := range schema.Properties {
if len(value.Ref) > 0 && value.Ref[0] == '#' {
// local reference, should propagate remote
value.Ref = remoteComponent + value.Ref
}
}

return schema, nil
}

func mergeAllOf(allOf []*openapi3.SchemaRef) (openapi3.Schema, error) {
var schema openapi3.Schema
for _, schemaRef := range allOf {
Expand Down

0 comments on commit b812181

Please sign in to comment.