1 package genswagger
2
3 import (
4 "bytes"
5 "encoding/json"
6 "fmt"
7
8 "github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway/descriptor"
9 )
10
11 type param struct {
12 *descriptor.File
13 reg *descriptor.Registry
14 }
15
16 type binding struct {
17 *descriptor.Binding
18 }
19
20
21 type swaggerInfoObject struct {
22 Title string `json:"title"`
23 Description string `json:"description,omitempty"`
24 TermsOfService string `json:"termsOfService,omitempty"`
25 Version string `json:"version"`
26
27 Contact *swaggerContactObject `json:"contact,omitempty"`
28 License *swaggerLicenseObject `json:"license,omitempty"`
29
30 extensions []extension
31 }
32
33
34 type swaggerContactObject struct {
35 Name string `json:"name,omitempty"`
36 URL string `json:"url,omitempty"`
37 Email string `json:"email,omitempty"`
38 }
39
40
41 type swaggerLicenseObject struct {
42 Name string `json:"name,omitempty"`
43 URL string `json:"url,omitempty"`
44 }
45
46
47 type swaggerExternalDocumentationObject struct {
48 Description string `json:"description,omitempty"`
49 URL string `json:"url,omitempty"`
50 }
51
52 type extension struct {
53 key string
54 value json.RawMessage
55 }
56
57
58 type swaggerObject struct {
59 Swagger string `json:"swagger"`
60 Info swaggerInfoObject `json:"info"`
61 Host string `json:"host,omitempty"`
62 BasePath string `json:"basePath,omitempty"`
63 Schemes []string `json:"schemes,omitempty"`
64 Consumes []string `json:"consumes"`
65 Produces []string `json:"produces"`
66 Paths swaggerPathsObject `json:"paths"`
67 Definitions swaggerDefinitionsObject `json:"definitions"`
68 SecurityDefinitions swaggerSecurityDefinitionsObject `json:"securityDefinitions,omitempty"`
69 Security []swaggerSecurityRequirementObject `json:"security,omitempty"`
70 ExternalDocs *swaggerExternalDocumentationObject `json:"externalDocs,omitempty"`
71
72 extensions []extension
73 }
74
75
76 type swaggerSecurityDefinitionsObject map[string]swaggerSecuritySchemeObject
77
78
79 type swaggerSecuritySchemeObject struct {
80 Type string `json:"type"`
81 Description string `json:"description,omitempty"`
82 Name string `json:"name,omitempty"`
83 In string `json:"in,omitempty"`
84 Flow string `json:"flow,omitempty"`
85 AuthorizationURL string `json:"authorizationUrl,omitempty"`
86 TokenURL string `json:"tokenUrl,omitempty"`
87 Scopes swaggerScopesObject `json:"scopes,omitempty"`
88
89 extensions []extension
90 }
91
92
93 type swaggerScopesObject map[string]string
94
95
96 type swaggerSecurityRequirementObject map[string][]string
97
98
99 type swaggerPathsObject map[string]swaggerPathItemObject
100
101
102 type swaggerPathItemObject struct {
103 Get *swaggerOperationObject `json:"get,omitempty"`
104 Delete *swaggerOperationObject `json:"delete,omitempty"`
105 Post *swaggerOperationObject `json:"post,omitempty"`
106 Put *swaggerOperationObject `json:"put,omitempty"`
107 Patch *swaggerOperationObject `json:"patch,omitempty"`
108 }
109
110
111 type swaggerOperationObject struct {
112 Summary string `json:"summary,omitempty"`
113 Description string `json:"description,omitempty"`
114 OperationID string `json:"operationId"`
115 Responses swaggerResponsesObject `json:"responses"`
116 Parameters swaggerParametersObject `json:"parameters,omitempty"`
117 Tags []string `json:"tags,omitempty"`
118 Deprecated bool `json:"deprecated,omitempty"`
119 Produces []string `json:"produces,omitempty"`
120
121 Security *[]swaggerSecurityRequirementObject `json:"security,omitempty"`
122 ExternalDocs *swaggerExternalDocumentationObject `json:"externalDocs,omitempty"`
123
124 extensions []extension
125 }
126
127 type swaggerParametersObject []swaggerParameterObject
128
129
130 type swaggerParameterObject struct {
131 Name string `json:"name"`
132 Description string `json:"description,omitempty"`
133 In string `json:"in,omitempty"`
134 Required bool `json:"required"`
135 Type string `json:"type,omitempty"`
136 Format string `json:"format,omitempty"`
137 Items *swaggerItemsObject `json:"items,omitempty"`
138 Enum []string `json:"enum,omitempty"`
139 CollectionFormat string `json:"collectionFormat,omitempty"`
140 Default string `json:"default,omitempty"`
141 MinItems *int `json:"minItems,omitempty"`
142
143
144
145 Schema *swaggerSchemaObject `json:"schema,omitempty"`
146 }
147
148
149
150 type schemaCore struct {
151 Type string `json:"type,omitempty"`
152 Format string `json:"format,omitempty"`
153 Ref string `json:"$ref,omitempty"`
154 Example json.RawMessage `json:"example,omitempty"`
155
156 Items *swaggerItemsObject `json:"items,omitempty"`
157
158
159
160
161 Enum []string `json:"enum,omitempty"`
162 Default string `json:"default,omitempty"`
163 }
164
165 func (s *schemaCore) setRefFromFQN(ref string, reg *descriptor.Registry) error {
166 name, ok := fullyQualifiedNameToSwaggerName(ref, reg)
167 if !ok {
168 return fmt.Errorf("setRefFromFQN: can't resolve swagger name from '%v'", ref)
169 }
170 s.Ref = fmt.Sprintf("#/definitions/%s", name)
171 return nil
172 }
173
174 type swaggerItemsObject schemaCore
175
176
177 type swaggerResponsesObject map[string]swaggerResponseObject
178
179
180 type swaggerResponseObject struct {
181 Description string `json:"description"`
182 Schema swaggerSchemaObject `json:"schema"`
183 Examples map[string]interface{} `json:"examples,omitempty"`
184 Headers swaggerHeadersObject `json:"headers,omitempty"`
185
186 extensions []extension
187 }
188
189 type swaggerHeadersObject map[string]swaggerHeaderObject
190
191
192 type swaggerHeaderObject struct {
193 Description string `json:"description,omitempty"`
194 Type string `json:"type,omitempty"`
195 Format string `json:"format,omitempty"`
196 Default json.RawMessage `json:"default,omitempty"`
197 Pattern string `json:"pattern,omitempty"`
198 }
199
200 type keyVal struct {
201 Key string
202 Value interface{}
203 }
204
205 type swaggerSchemaObjectProperties []keyVal
206
207 func (op swaggerSchemaObjectProperties) MarshalJSON() ([]byte, error) {
208 var buf bytes.Buffer
209 buf.WriteString("{")
210 for i, kv := range op {
211 if i != 0 {
212 buf.WriteString(",")
213 }
214 key, err := json.Marshal(kv.Key)
215 if err != nil {
216 return nil, err
217 }
218 buf.Write(key)
219 buf.WriteString(":")
220 val, err := json.Marshal(kv.Value)
221 if err != nil {
222 return nil, err
223 }
224 buf.Write(val)
225 }
226
227 buf.WriteString("}")
228 return buf.Bytes(), nil
229 }
230
231
232 type swaggerSchemaObject struct {
233 schemaCore
234
235 Properties *swaggerSchemaObjectProperties `json:"properties,omitempty"`
236 AdditionalProperties *swaggerSchemaObject `json:"additionalProperties,omitempty"`
237
238 Description string `json:"description,omitempty"`
239 Title string `json:"title,omitempty"`
240
241 ExternalDocs *swaggerExternalDocumentationObject `json:"externalDocs,omitempty"`
242
243 ReadOnly bool `json:"readOnly,omitempty"`
244 MultipleOf float64 `json:"multipleOf,omitempty"`
245 Maximum float64 `json:"maximum,omitempty"`
246 ExclusiveMaximum bool `json:"exclusiveMaximum,omitempty"`
247 Minimum float64 `json:"minimum,omitempty"`
248 ExclusiveMinimum bool `json:"exclusiveMinimum,omitempty"`
249 MaxLength uint64 `json:"maxLength,omitempty"`
250 MinLength uint64 `json:"minLength,omitempty"`
251 Pattern string `json:"pattern,omitempty"`
252 MaxItems uint64 `json:"maxItems,omitempty"`
253 MinItems uint64 `json:"minItems,omitempty"`
254 UniqueItems bool `json:"uniqueItems,omitempty"`
255 MaxProperties uint64 `json:"maxProperties,omitempty"`
256 MinProperties uint64 `json:"minProperties,omitempty"`
257 Required []string `json:"required,omitempty"`
258 }
259
260
261 type swaggerReferenceObject struct {
262 Ref string `json:"$ref"`
263 }
264
265
266 type swaggerDefinitionsObject map[string]swaggerSchemaObject
267
268
269
270 type messageMap map[string]*descriptor.Message
271
272
273
274 type enumMap map[string]*descriptor.Enum
275
276
277 type refMap map[string]struct{}
278
View as plain text