-
Notifications
You must be signed in to change notification settings - Fork 69
/
operation.go
97 lines (80 loc) · 2.03 KB
/
operation.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package openapi
import (
"strconv"
"github.com/go-faster/errors"
"github.com/ogen-go/ogen/location"
)
// Operation is an OpenAPI Operation.
type Operation struct {
OperationID string // optional
Summary string // optional
Description string // optional
Deprecated bool // optional
HTTPMethod string
Path Path
Parameters []*Parameter
RequestBody *RequestBody // optional
// Security requirements.
Security SecurityRequirements
// Operation responses.
Responses Responses
XOgenOperationGroup string // Extension field for operation grouping.
location.Pointer `json:"-" yaml:"-"`
}
// RequestBody of an OpenAPI Operation.
type RequestBody struct {
Ref Ref
Description string
Required bool
Content map[string]*MediaType
location.Pointer `json:"-" yaml:"-"`
}
// Header is an OpenAPI Header definition.
type Header = Parameter
// Response is an OpenAPI Response definition.
type Response struct {
Ref Ref
Description string
Headers map[string]*Header
Content map[string]*MediaType
// Links map[string]*Link
location.Pointer `json:"-" yaml:"-"`
}
// Responses contains a list of parsed OpenAPI Responses.
type Responses struct {
StatusCode map[int]*Response
Pattern [5]*Response
Default *Response
location.Pointer `json:"-" yaml:"-"`
}
// Add adds a response to the Responses.
func (r *Responses) Add(pattern string, resp *Response) error {
switch pattern {
case "default":
r.Default = resp
case "1XX":
r.Pattern[0] = resp
case "2XX":
r.Pattern[1] = resp
case "3XX":
r.Pattern[2] = resp
case "4XX":
r.Pattern[3] = resp
case "5XX":
r.Pattern[4] = resp
default:
code, err := strconv.Atoi(pattern)
if err != nil {
// Do not return parsing error, it could be a bit confusing.
return errors.Errorf("invalid response pattern %q", pattern)
}
if code < 100 || code > 599 {
return errors.Errorf("invalid status code: %d", code)
}
if r.StatusCode == nil {
r.StatusCode = make(map[int]*Response)
}
r.StatusCode[code] = resp
}
return nil
}