Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃悰 Fix XValidations flattening #998

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 pkg/crd/flatten.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ func flattenAllOfInto(dst *apiext.JSONSchemaProps, src apiext.JSONSchemaProps, e
dstField.Set(srcField)
case "XMapType":
dstField.Set(srcField)
case "XValidations":
dstField.Set(reflect.AppendSlice(dstField, srcField))
// NB(directxman12): no need to explicitly handle nullable -- false is considered to be the zero value
// TODO(directxman12): src isn't necessarily the field value -- it's just the most recent allOf entry
default:
Expand Down
19 changes: 19 additions & 0 deletions pkg/crd/flatten_all_of_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,25 @@ var _ = Describe("AllOf Flattening", func() {
},
}))
})

It("should merge XValidation fields", func() {
By("flattening a schema with multiple validation fields")
original := &apiext.JSONSchemaProps{
AllOf: []apiext.JSONSchemaProps{
{XValidations: apiext.ValidationRules{{Rule: "rule1"}, {Rule: "rule2"}}},
{XValidations: apiext.ValidationRules{{Rule: "rule3"}}},
},
}
flattened := crd.FlattenEmbedded(original, errRec)
Expect(errRec.FirstError()).NotTo(HaveOccurred())

By("ensuring that the result lists all validation rules")
Expect(flattened).To(Equal(&apiext.JSONSchemaProps{
XValidations: []apiext.ValidationRule{
{Rule: "rule1"}, {Rule: "rule2"}, {Rule: "rule3"},
},
}))
})
})

It("should skip Title, Description, Example, and ExternalDocs, assuming they've been merged pre-AllOf flattening", func() {
Expand Down