-
Notifications
You must be signed in to change notification settings - Fork 2
/
path.go
131 lines (117 loc) · 4.8 KB
/
path.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package openapi
import "encoding/json"
// path.go contains
//
// PathItemObject
// OperationObject
// ParameterObject
// https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#path-item-object
type Path struct {
// TODO: how to deal with Ref, we could inline everything when generate though ....
//Ref string `json:"$ref" yaml:"$ref"`
Summary string `json:"summary" yaml:"summary"`
Description string `json:"description" yaml:"description"`
Servers []Server `json:"servers,omitempty" yaml:"servers,omitempty"`
Parameters []Parameter `json:"parameters,omitempty" yaml:"parameters,omitempty"`
Get *Operation `json:"get,omitempty" yaml:"get,omitempty"`
Put *Operation `json:"put,omitempty" yaml:"put,omitempty"`
Post *Operation `json:"post,omitempty" yaml:"post,omitempty"`
Delete *Operation `json:"delete,omitempty" yaml:"delete,omitempty"`
Options *Operation `json:"options,omitempty" yaml:"options,omitempty"`
Head *Operation `json:"head,omitempty" yaml:"head,omitempty"`
Patch *Operation `json:"patch,omitempty" yaml:"patch,omitempty"`
Trace *Operation `json:"trace,omitempty" yaml:"trace,omitempty"`
}
// Operations describes a single API call <HTTP verb> <path>, i.e. GET /pets
// https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#operation-object
type Operation struct {
Tags []string `json:"tags" yaml:"tags"`
Summary string `json:"summary" yaml:"summary"`
Description string `json:"description" yaml:"description"`
ExternalDocs *ExternalDoc `json:"externalDocs,omitempty" yaml:"externalDocs,omitempty"`
OperationId string `json:"operationId" yaml:"operationId"`
Parameters []Parameter `json:"parameters,omitempty" yaml:"parameters,omitempty"`
RequestBody *RequestBody `json:"requestBody,omitempty" yaml:"requestBody,omitempty"`
Responses map[string]Response `json:"responses" yaml:"responses"`
// TODO: callbacks
Deprecated bool `json:"deprecated" yaml:"deprecated"`
Security *Security `json:"security,omitempty" yaml:"security,omitempty"`
Servers []Server `json:"servers,omitempty" yaml:"servers,omitempty"`
}
type ParameterOrRef struct {
Ref Reference
Parameter Parameter
}
func (r ParameterOrRef) MarshalJSON() ([]byte, error) {
// use reference
if r.Ref.Ref != "" {
return json.Marshal(r.Ref)
}
return json.Marshal(r.Parameter)
}
// https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#parameter-object
// {
// "name": "token",
// "in": "header",
// "description": "token to be passed as a header",
// "required": true,
// "schema": {
// "type": "array",
// "items": {
// "type": "integer",
// "format": "int64"
// }
// },
// "style": "simple"
// }
type Parameter struct {
Name string `json:"name" yaml:"name"`
In string `json:"in" yaml:"in"`
Description string `json:"description" yaml:"description"`
Schema *SchemaOrRef `json:"schema" yaml:"schema"`
Style string `json:"style" yaml:"style"`
Required bool `json:"required" yaml:"required"`
Deprecated bool `json:"deprecated" yaml:"deprecated"`
AllowEmptyValue bool `json:"allowEmptyValue" yaml:"allowEmptyValue"`
}
type RequestBodyOrRef struct {
Ref Reference
RequestBody RequestBody
}
func (r RequestBodyOrRef) MarshalJSON() ([]byte, error) {
// use reference
if r.Ref.Ref != "" {
return json.Marshal(r.Ref)
}
return json.Marshal(r.RequestBody)
}
// https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#requestBodyObject
type RequestBody struct {
Description string `json:"description" yaml:"description"`
Content map[string]MediaType `json:"content" yaml:"content"`
Required bool `json:"required" yaml:"required"`
}
type ResponseOrRef struct {
Ref Reference
Response Response
}
func (r ResponseOrRef) MarshalJSON() ([]byte, error) {
// use reference
if r.Ref.Ref != "" {
return json.Marshal(r.Ref)
}
return json.Marshal(r.Response)
}
// https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#responseObject
type Response struct {
Description string `json:"description" yaml:"description"`
// TODO: headers, need to define header object https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#headerObject
// TODO: might need to use pointer to indicate there is no response body
Content map[string]MediaType `json:"content" yaml:"content"`
// TODO: links
}
// https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#mediaTypeObject
type MediaType struct {
Schema *SchemaOrRef `json:"schema" yaml:"schema"`
// TODO: example, examples
}