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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix generation of unused validation functions #3652

Merged
merged 1 commit into from
Feb 11, 2025
Merged
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
37 changes: 37 additions & 0 deletions http/codegen/server_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func TestServerTypes(t *testing.T) {
{"server-query-custom-name", testdata.PayloadQueryCustomNameDSL, QueryCustomNameServerTypesFile},
{"server-header-custom-name", testdata.PayloadHeaderCustomNameDSL, HeaderCustomNameServerTypesFile},
{"server-cookie-custom-name", testdata.PayloadCookieCustomNameDSL, CookieCustomNameServerTypesFile},
{"server-payload-with-validated-alias", testdata.PayloadWithValidatedAliasDSL, PayloadWithValidatedAliasServerTypeCode},
}
for _, c := range cases {
t.Run(c.Name, func(t *testing.T) {
Expand Down Expand Up @@ -380,3 +381,39 @@ func NewMethodCookieCustomNamePayload(c2 *string) *servicecookiecustomname.Metho
return v
}
`

const PayloadWithValidatedAliasServerTypeCode = `// MethodStreamingBody is the type of the "ServicePayloadValidatedAlias"
// service "Method" endpoint HTTP request body.
type MethodStreamingBody struct {
Name *ValidatedStringStreamingBody ` + "`" + `form:"name,omitempty" json:"name,omitempty" xml:"name,omitempty"` + "`" + `
}

// ValidatedStringStreamingBody is used to define fields on request body types.
type ValidatedStringStreamingBody string

// NewMethodStreamingBody builds a ServicePayloadValidatedAlias service Method
// endpoint payload.
func NewMethodStreamingBody(body *MethodStreamingBody) *servicepayloadvalidatedalias.MethodStreamingPayload {
v := &servicepayloadvalidatedalias.MethodStreamingPayload{}
if body.Name != nil {
name := servicepayloadvalidatedalias.ValidatedString(*body.Name)
v.Name = &name
}

return v
}

// ValidateMethodStreamingBody runs the validations defined on
// MethodStreamingBody
func ValidateMethodStreamingBody(body *MethodStreamingBody) (err error) {
if body.Name != nil {
err = goa.MergeErrors(err, goa.ValidatePattern("body.name", string(*body.Name), "^[a-zA-Z]+$"))
}
if body.Name != nil {
if utf8.RuneCountInString(string(*body.Name)) < 10 {
err = goa.MergeErrors(err, goa.InvalidLengthError("body.name", string(*body.Name), utf8.RuneCountInString(string(*body.Name)), 10, true))
}
}
return
}
`
7 changes: 4 additions & 3 deletions http/codegen/service_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -2586,9 +2586,10 @@ func attributeTypeData(ut expr.UserType, req, ptr, server bool, rd *ServiceData)
ctx = "response"
}
desc = name + " is used to define fields on " + ctx + " body types."
if req || !req && !server {
// generate validations for responses client-side and for
// requests server-side and CLI
if (req || !req && !server) && !expr.IsAlias(ut) {
// Generate validations for responses client-side and for
// requests server-side and CLI.
// Alias types are validated inline in the parent type
validate = codegen.ValidationCode(ut.Attribute(), ut, hctx, true, expr.IsAlias(ut), false, "body")
}
if validate != "" {
Expand Down
18 changes: 18 additions & 0 deletions http/codegen/testdata/payload_dsls.go
Original file line number Diff line number Diff line change
Expand Up @@ -3648,3 +3648,21 @@ var PayloadCookieCustomNameDSL = func() {
})
})
}

var PayloadWithValidatedAliasDSL = func() {
var ValidatedString = Type("ValidatedString", String, func() {
MinLength(10)
Pattern("^[a-zA-Z]+$")
})

Service("ServicePayloadValidatedAlias", func() {
Method("Method", func() {
StreamingPayload(func() {
Attribute("name", ValidatedString)
})
HTTP(func() {
GET("/")
})
})
})
}
Loading