Skip to content

Commit f154169

Browse files
nitinmohan87raphael
authored andcommitted
Fix short-circuiting in hasTag and iterate through all bases (goadesign#2366)
1 parent e541a86 commit f154169

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

expr/method.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,9 @@ func hasTag(p *AttributeExpr, tag string) bool {
228228
if !ok {
229229
continue
230230
}
231-
return hasTag(ut.Attribute(), tag)
231+
if hasTag(ut.Attribute(), tag) {
232+
return true
233+
}
232234
}
233235
if ut, ok := p.Type.(UserType); ok {
234236
return hasTag(ut.Attribute(), tag)
@@ -248,7 +250,9 @@ func hasTagPrefix(p *AttributeExpr, prefix string) bool {
248250
if !ok {
249251
continue
250252
}
251-
return hasTagPrefix(ut.Attribute(), prefix)
253+
if hasTagPrefix(ut.Attribute(), prefix) {
254+
return true
255+
}
252256
}
253257
if ut, ok := p.Type.(UserType); ok {
254258
return hasTagPrefix(ut.Attribute(), prefix)

expr/method_test.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ func TestMethodExprValidate(t *testing.T) {
1414
DSL func()
1515
Error string
1616
}{
17+
{"valid-security-schemes-extend", testdata.ValidSecuritySchemesExtendDSL, ""},
1718
{"invalid-security-schemes", testdata.InvalidSecuritySchemesDSL,
1819
`service "InvalidSecuritySchemesService" method "SecureMethod": payload of method "SecureMethod" of service "InvalidSecuritySchemesService" does not define a username attribute, use Username to define one
1920
service "InvalidSecuritySchemesService" method "SecureMethod": payload of method "SecureMethod" of service "InvalidSecuritySchemesService" does not define a password attribute, use Password to define one
@@ -34,9 +35,13 @@ service "AnotherInvalidSecuritySchemesService" method "Method": payload of metho
3435
}
3536
for _, tc := range cases {
3637
t.Run(tc.Name, func(t *testing.T) {
37-
err := expr.RunInvalidDSL(t, tc.DSL)
38-
if tc.Error != err.Error() {
39-
t.Errorf("invalid error:\ngot:\n%s\n\ngot vs expected:\n%s", err.Error(), expr.Diff(t, err.Error(), tc.Error))
38+
if tc.Error == "" {
39+
expr.RunDSL(t, tc.DSL)
40+
} else {
41+
err := expr.RunInvalidDSL(t, tc.DSL)
42+
if tc.Error != err.Error() {
43+
t.Errorf("invalid error:\ngot:\n%s\n\ngot vs expected:\n%s", err.Error(), expr.Diff(t, err.Error(), tc.Error))
44+
}
4045
}
4146
})
4247
}

expr/testdata/method_validate_dsls.go

+19
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,25 @@ var OAuth2 = OAuth2Security("authCode", func() {
1818
Scope("api:read", "Read access")
1919
})
2020

21+
var ValidSecuritySchemesExtendDSL = func() {
22+
var CommonAttr = Type("Common", func() {
23+
Attribute("version", String)
24+
})
25+
var SecurityAttr = Type("Security", func() {
26+
Username("user", String)
27+
Password("pass", String)
28+
})
29+
Service("ValidSecuritySchemesExtendService", func() {
30+
Method("SecureMethod", func() {
31+
Security(BasicAuth)
32+
Payload(func() {
33+
Extend(CommonAttr)
34+
Extend(SecurityAttr)
35+
})
36+
})
37+
})
38+
}
39+
2140
var InvalidSecuritySchemesDSL = func() {
2241
Service("InvalidSecuritySchemesService", func() {
2342
Security(OAuth2, APIKeyAuth, func() {

0 commit comments

Comments
 (0)