forked from getkin/kin-openapi
/
operation.go
99 lines (79 loc) · 2.32 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
98
99
package openapi3
import (
"context"
"strconv"
"github.com/getkin/kin-openapi/jsoninfo"
)
// Operation represents "operation" specified by" OpenAPI/Swagger 3.0 standard.
type Operation struct {
ExtensionProps
// Optional tags for documentation.
Tags []string `json:"tags,omitempty"`
// Optional short summary.
Summary string `json:"summary,omitempty"`
// Optional description. Should use CommonMark syntax.
Description string `json:"description,omitempty"`
// Optional operation ID.
OperationID string `json:"operationId,omitempty"`
// Optional parameters.
Parameters Parameters `json:"parameters,omitempty"`
// Optional body parameter.
RequestBody *RequestBodyRef `json:"requestBody,omitempty"`
// Optional responses.
Responses Responses `json:"responses,omitempty"`
// Optional callbacks
Callbacks map[string]*CallbackRef `json:"callbacks,omitempty"`
Deprecated bool `json:"deprecated,omitempty"`
// Optional security requirements that overrides top-level security.
Security *SecurityRequirements `json:"security,omitempty"`
// Optional servers that overrides top-level servers.
Servers *Servers `json:"servers,omitempty"`
}
func NewOperation() *Operation {
return &Operation{}
}
func (operation *Operation) MarshalJSON() ([]byte, error) {
return jsoninfo.MarshalStrictStruct(operation)
}
func (operation *Operation) UnmarshalJSON(data []byte) error {
return jsoninfo.UnmarshalStrictStruct(data, operation)
}
func (operation *Operation) AddParameter(p *Parameter) {
operation.Parameters = append(operation.Parameters, &ParameterRef{
Value: p,
})
}
func (operation *Operation) AddResponse(status int, response *Response) {
responses := operation.Responses
if responses == nil {
responses = NewResponses()
operation.Responses = responses
}
if status == 0 {
responses["default"] = &ResponseRef{
Value: response,
}
} else {
responses[strconv.FormatInt(int64(status), 10)] = &ResponseRef{
Value: response,
}
}
}
func (operation *Operation) Validate(c context.Context) error {
if v := operation.Parameters; v != nil {
if err := v.Validate(c); err != nil {
return err
}
}
if v := operation.RequestBody; v != nil {
if err := v.Validate(c); err != nil {
return err
}
}
if v := operation.Responses; v != nil {
if err := v.Validate(c); err != nil {
return err
}
}
return nil
}