Skip to content

Commit 4dc8abd

Browse files
committed
Preserve description and default for allOf-wrapped $ref
Instead of delegating to ParseV3SchemaReference (which reads from the inner $ref), build the Ref from the outer schema so sibling fields like description, default, and extensions are retained.
1 parent 99b3298 commit 4dc8abd

2 files changed

Lines changed: 30 additions & 2 deletions

File tree

pkg/util/proto/document_v3.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,27 @@ func (d *Definitions) ParseSchemaV3(s *openapi_v3.Schema, path *Path) (Schema, e
121121
// allOf containing a $ref, treat it as a reference. This is the OpenAPI 3.0
122122
// compliant way to add sibling properties (description, default) alongside
123123
// a $ref, since per the spec $ref siblings are ignored.
124+
// We build the Ref from the outer schema so that description, default, and
125+
// extensions are preserved.
124126
if s.GetType() == "" && len(s.GetAllOf()) == 1 {
125127
if ref, ok := s.GetAllOf()[0].GetOneof().(*openapi_v3.SchemaOrReference_Reference); ok {
126-
return d.ParseV3SchemaReference(ref.Reference, path)
128+
xRef := ref.Reference.GetXRef()
129+
if !strings.HasPrefix(xRef, "#/components/schemas") {
130+
return d.parseV3Arbitrary(s, path)
131+
}
132+
reference := strings.TrimPrefix(xRef, "#/components/schemas/")
133+
if _, ok := d.models[reference]; !ok {
134+
return nil, newSchemaError(path, "unknown model in reference: %q", reference)
135+
}
136+
base, err := d.parseV3BaseSchema(s, path)
137+
if err != nil {
138+
return nil, err
139+
}
140+
return &Ref{
141+
BaseSchema: *base,
142+
reference: reference,
143+
definitions: d,
144+
}, nil
127145
}
128146
}
129147

pkg/util/proto/openapi_test.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -780,7 +780,7 @@ func TestV3AllOfRef(t *testing.T) {
780780
}
781781
],
782782
"description": "field using allOf-wrapped ref",
783-
"default": {}
783+
"default": "some-default"
784784
}
785785
},
786786
"x-kubernetes-group-version-kind": [
@@ -855,4 +855,14 @@ func TestV3AllOfRef(t *testing.T) {
855855
t.Errorf("allOfRef reference = %q, want %q", ref.Reference(), "Child")
856856
}
857857
})
858+
859+
t.Run("allOf-wrapped ref preserves description and default", func(t *testing.T) {
860+
field := parent.Fields["allOfRef"]
861+
if got := field.GetDescription(); got != "field using allOf-wrapped ref" {
862+
t.Errorf("description = %q, want %q", got, "field using allOf-wrapped ref")
863+
}
864+
if got, want := field.GetDefault(), "some-default"; got != want {
865+
t.Errorf("default = %v, want %v", got, want)
866+
}
867+
})
858868
}

0 commit comments

Comments
 (0)