/
response.go
58 lines (54 loc) Β· 1.47 KB
/
response.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package openapiv3
import (
"fmt"
"net/http"
"goa.design/goa/v3/expr"
"goa.design/goa/v3/http/codegen/openapi"
)
func responseFromExpr(r *expr.HTTPResponseExpr, bodies map[int][]*openapi.Schema, rand *expr.Random) *Response {
ct := r.ContentType
rt, ok := r.Body.Type.(*expr.ResultTypeExpr)
if ok && ct == "" {
ct = rt.ContentType
}
if ct == "" {
// Default to application/json
ct = "application/json"
}
var headers map[string]*HeaderRef
o := expr.AsObject(r.Headers.Type)
if len(*o) > 0 {
headers = make(map[string]*HeaderRef, len(*o))
expr.WalkMappedAttr(r.Headers, func(name, elem string, attr *expr.AttributeExpr) error {
headers[elem] = &HeaderRef{Value: &Header{
Description: attr.Description,
Required: r.Headers.IsRequiredNoDefault(name),
Schema: newSchemafier(rand).schemafy(attr),
Example: attr.Example(rand),
Extensions: openapi.ExtensionsFromExpr(attr.Meta),
}}
return nil
})
}
var content map[string]*MediaType
{
if r.Body.Type != expr.Empty {
content = make(map[string]*MediaType)
content[ct] = &MediaType{
Schema: bodies[r.StatusCode][0],
Example: r.Body.Example(rand),
Extensions: openapi.ExtensionsFromExpr(r.Body.Meta),
}
}
}
desc := r.Description
if desc == "" {
desc = fmt.Sprintf("%s response.", http.StatusText(r.StatusCode))
}
return &Response{
Description: &desc,
Headers: headers,
Content: content,
Extensions: openapi.ExtensionsFromExpr(r.Meta),
}
}