Finalize sets the response result type from its type if the type is a result type and no result type is already specified.
(a *HTTPEndpointExpr, svcAtt *AttributeExpr)
| 266 | // Finalize sets the response result type from its type if the type is a result |
| 267 | // type and no result type is already specified. |
| 268 | func (r *HTTPResponseExpr) Finalize(a *HTTPEndpointExpr, svcAtt *AttributeExpr) { |
| 269 | r.Parent = a |
| 270 | |
| 271 | if r.Body != nil && r.Body.Type != Empty { |
| 272 | bodyAtt := svcAtt |
| 273 | if o, ok := r.Body.Meta["origin:attribute"]; ok { |
| 274 | bodyAtt = svcAtt.Find(o[0]) |
| 275 | } |
| 276 | bodyObj := AsObject(bodyAtt.Type) |
| 277 | if body := AsObject(r.Body.Type); body != nil { |
| 278 | for _, nat := range *body { |
| 279 | n := nat.Name |
| 280 | n = strings.Split(n, ":")[0] |
| 281 | var att, patt *AttributeExpr |
| 282 | var required bool |
| 283 | if bodyObj != nil { |
| 284 | att = bodyObj.Attribute(n) |
| 285 | required = bodyAtt.IsRequired(n) |
| 286 | } else { |
| 287 | att = bodyAtt |
| 288 | required = bodyAtt.Type != Empty |
| 289 | } |
| 290 | initAttrFromDesign(att, patt) |
| 291 | if required { |
| 292 | if r.Body.Validation == nil { |
| 293 | r.Body.Validation = &ValidationExpr{} |
| 294 | } |
| 295 | r.Body.Validation.AddRequired(n) |
| 296 | } |
| 297 | } |
| 298 | // Remember original name for example to generate friendlier OpenAPI specs. |
| 299 | if t, ok := r.Body.Type.(UserType); ok { |
| 300 | t.Attribute().AddMeta("name:original", t.Name()) |
| 301 | } |
| 302 | // Wrap object with user type to simplify response rendering code. |
| 303 | r.Body.Type = &UserTypeExpr{ |
| 304 | AttributeExpr: DupAtt(r.Body), |
| 305 | TypeName: fmt.Sprintf("%s%sResponseBody", a.Service.Name(), a.Name()), |
| 306 | } |
| 307 | } |
| 308 | if r.Body.Meta == nil { |
| 309 | r.Body.Meta = bodyAtt.Meta |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | // Set response content type if empty and if set in the result type |
| 314 | if r.ContentType == "" { |
| 315 | if rt, ok := svcAtt.Type.(*ResultTypeExpr); ok && rt.ContentType != "" { |
| 316 | r.ContentType = rt.ContentType |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | initAttr(r.Headers, svcAtt) |
| 321 | initAttr(r.Cookies, svcAtt) |
| 322 | } |
| 323 | |
| 324 | // Dup creates a copy of the response expression. |
| 325 | func (r *HTTPResponseExpr) Dup() *HTTPResponseExpr { |
nothing calls this directly
no test coverage detected