Skip to content

Commit 0715905

Browse files
committed
Generate validation code for required attributes
in inline struct http bodies.
1 parent 92ec895 commit 0715905

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

http/codegen/service_data.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1992,6 +1992,9 @@ func buildRequestBodyType(body, att *expr.AttributeExpr, e *expr.HTTPEndpointExp
19921992
}
19931993
}
19941994
} else {
1995+
// Generate validation code first because inline struct validation is removed.
1996+
ctx := codegen.NewAttributeContext(!expr.IsPrimitive(body.Type), false, !svr, "", sd.Scope)
1997+
validateRef = codegen.ValidationCode(body, nil, ctx, true, expr.IsAlias(body.Type), false, "body")
19951998
if svr && expr.IsObject(body.Type) {
19961999
// Body is an explicit object described in the design and in
19972000
// this case the GoTypeRef is an inline struct definition. We
@@ -2000,8 +2003,6 @@ func buildRequestBodyType(body, att *expr.AttributeExpr, e *expr.HTTPEndpointExp
20002003
body.Validation = nil
20012004
}
20022005
varname = sd.Scope.GoTypeRef(body)
2003-
ctx := codegen.NewAttributeContext(false, false, !svr, "", sd.Scope)
2004-
validateRef = codegen.ValidationCode(body, nil, ctx, true, expr.IsAlias(body.Type), false, "body")
20052006
desc = body.Description
20062007
}
20072008
var init *InitData

http/codegen/testdata/payload_decode_functions.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3938,6 +3938,12 @@ func DecodeMethodBodyObjectRequiredRequest(mux goahttp.Muxer, decoder func(*http
39383938
}
39393939
return nil, goa.DecodePayloadError(err.Error())
39403940
}
3941+
if body.B == nil {
3942+
err = goa.MergeErrors(err, goa.MissingFieldError("b", "body"))
3943+
}
3944+
if err != nil {
3945+
return nil, err
3946+
}
39413947
payload := NewMethodBodyObjectRequiredPayload(body)
39423948
39433949
return payload, nil
@@ -4494,6 +4500,16 @@ func DecodeMethodBodyPrimitiveArrayUserRequiredRequest(mux goahttp.Muxer, decode
44944500
}
44954501
return nil, goa.DecodePayloadError(err.Error())
44964502
}
4503+
for _, e := range body {
4504+
if e != nil {
4505+
if err2 := ValidatePayloadTypeRequestBody(e); err2 != nil {
4506+
err = goa.MergeErrors(err, err2)
4507+
}
4508+
}
4509+
}
4510+
if err != nil {
4511+
return nil, err
4512+
}
44974513
payload := NewMethodBodyPrimitiveArrayUserRequiredPayloadType(body)
44984514
44994515
return payload, nil

0 commit comments

Comments
 (0)