Skip to content

Commit 0a00e44

Browse files
authored
Merge pull request #3683 from goadesign/fix/example_compatibility
Validate example compatibility after DSL evaluation
2 parents 6fde172 + 1727549 commit 0a00e44

File tree

4 files changed

+15
-7
lines changed

4 files changed

+15
-7
lines changed

.github/workflows/test.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
strategy:
1414
fail-fast: true
1515
matrix:
16-
go: ['1.21', '1.22', '1.23']
16+
go: ['1.21', '1.22', '1.23', '1.24']
1717
os: ['ubuntu-latest', 'windows-latest']
1818
runs-on: ${{ matrix.os }}
1919

dsl/attribute.go

-5
Original file line numberDiff line numberDiff line change
@@ -338,11 +338,6 @@ func Example(args ...any) {
338338
eval.ReportError("example value is missing")
339339
return
340340
}
341-
if a.Type != nil && !a.Type.IsCompatible(ex.Value) {
342-
eval.ReportError("example value %#v is incompatible with attribute of type %s",
343-
ex.Value, a.Type.Name())
344-
return
345-
}
346341
a.UserExamples = append(a.UserExamples, ex)
347342
}
348343

expr/attribute.go

+13
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ func (a *AttributeExpr) Validate(ctx string, parent eval.Expression) *eval.Valid
212212
if v := a.Validation; v != nil {
213213
verr.Merge(v.Validate(ctx, parent))
214214
}
215+
verr.Merge(a.validateExamples(ctx, parent))
215216
if o := AsObject(a.Type); o != nil {
216217
for _, n := range a.AllRequired() {
217218
if a.Find(n) == nil {
@@ -734,6 +735,18 @@ func (a *AttributeExpr) validateEnumDefault(ctx string, parent eval.Expression)
734735
return verr
735736
}
736737

738+
// validateExamples makes sure that the attribute example values are compatible
739+
// with the attribute type.
740+
func (a *AttributeExpr) validateExamples(ctx string, parent eval.Expression) *eval.ValidationErrors {
741+
verr := new(eval.ValidationErrors)
742+
for _, ex := range a.UserExamples {
743+
if !a.Type.IsCompatible(ex.Value) { // DSL ensures ex.Value is not nil
744+
verr.Add(parent, "%sexample value %#v is incompatible with type %s", ctx, ex.Value, a.Type.Name())
745+
}
746+
}
747+
return verr
748+
}
749+
737750
func (a *AttributeExpr) inheritRecursive(parent *AttributeExpr, seen map[*AttributeExpr]struct{}) {
738751
if !a.shouldInherit(parent) {
739752
return

expr/example_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func TestExample(t *testing.T) {
6262
{"with-multiple-examples", testdata.WithMultipleExamplesDSL, 100, ""},
6363
{"overriding-example", testdata.OverridingExampleDSL, map[string]any{"name": "overridden"}, ""},
6464
{"with-extend", testdata.WithExtendExampleDSL, map[string]any{"name": "example"}, ""},
65-
{"invalid-example-type", testdata.InvalidExampleTypeDSL, nil, "example value map[int]int{1:1} is incompatible with attribute of type map in attribute"},
65+
{"invalid-example-type", testdata.InvalidExampleTypeDSL, nil, "service \"InvalidExampleType\" method \"Method\": payload - example value map[int]int{1:1} is incompatible with type map"},
6666
{"empty-example", testdata.EmptyExampleDSL, nil, "too few arguments given to Example in attribute"},
6767
{"hiding-example", testdata.HidingExampleDSL, nil, ""},
6868
{"overriding-hidden-examples", testdata.OverridingHiddenExamplesDSL, "example", ""},

0 commit comments

Comments
 (0)