blob: 77db96d877b312405936be21b3ef02fb5a53f4c9 [file] [log] [blame]
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -07001package genswagger
2
3import (
4 "bytes"
5 "encoding/json"
6
7 "github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway/descriptor"
8)
9
10type param struct {
11 *descriptor.File
12 reg *descriptor.Registry
13}
14
15type binding struct {
16 *descriptor.Binding
17}
18
19// http://swagger.io/specification/#infoObject
20type swaggerInfoObject struct {
21 Title string `json:"title"`
22 Description string `json:"description,omitempty"`
23 TermsOfService string `json:"termsOfService,omitempty"`
24 Version string `json:"version"`
25
26 Contact *swaggerContactObject `json:"contact,omitempty"`
27 License *swaggerLicenseObject `json:"license,omitempty"`
28
29 extensions []extension
30}
31
32// http://swagger.io/specification/#contactObject
33type swaggerContactObject struct {
34 Name string `json:"name,omitempty"`
35 URL string `json:"url,omitempty"`
36 Email string `json:"email,omitempty"`
37}
38
39// http://swagger.io/specification/#licenseObject
40type swaggerLicenseObject struct {
41 Name string `json:"name,omitempty"`
42 URL string `json:"url,omitempty"`
43}
44
45// http://swagger.io/specification/#externalDocumentationObject
46type swaggerExternalDocumentationObject struct {
47 Description string `json:"description,omitempty"`
48 URL string `json:"url,omitempty"`
49}
50
51type extension struct {
52 key string
53 value json.RawMessage
54}
55
56// http://swagger.io/specification/#swaggerObject
57type swaggerObject struct {
58 Swagger string `json:"swagger"`
59 Info swaggerInfoObject `json:"info"`
60 Host string `json:"host,omitempty"`
61 BasePath string `json:"basePath,omitempty"`
62 Schemes []string `json:"schemes"`
63 Consumes []string `json:"consumes"`
64 Produces []string `json:"produces"`
65 Paths swaggerPathsObject `json:"paths"`
66 Definitions swaggerDefinitionsObject `json:"definitions"`
67 StreamDefinitions swaggerDefinitionsObject `json:"x-stream-definitions,omitempty"`
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// http://swagger.io/specification/#securityDefinitionsObject
76type swaggerSecurityDefinitionsObject map[string]swaggerSecuritySchemeObject
77
78// http://swagger.io/specification/#securitySchemeObject
79type 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// http://swagger.io/specification/#scopesObject
93type swaggerScopesObject map[string]string
94
95// http://swagger.io/specification/#securityRequirementObject
96type swaggerSecurityRequirementObject map[string][]string
97
98// http://swagger.io/specification/#pathsObject
99type swaggerPathsObject map[string]swaggerPathItemObject
100
101// http://swagger.io/specification/#pathItemObject
102type 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// http://swagger.io/specification/#operationObject
111type 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
120 Security *[]swaggerSecurityRequirementObject `json:"security,omitempty"`
121 ExternalDocs *swaggerExternalDocumentationObject `json:"externalDocs,omitempty"`
122
123 extensions []extension
124}
125
126type swaggerParametersObject []swaggerParameterObject
127
128// http://swagger.io/specification/#parameterObject
129type swaggerParameterObject struct {
130 Name string `json:"name"`
131 Description string `json:"description,omitempty"`
132 In string `json:"in,omitempty"`
133 Required bool `json:"required"`
134 Type string `json:"type,omitempty"`
135 Format string `json:"format,omitempty"`
136 Items *swaggerItemsObject `json:"items,omitempty"`
137 Enum []string `json:"enum,omitempty"`
138 CollectionFormat string `json:"collectionFormat,omitempty"`
139 Default string `json:"default,omitempty"`
140 MinItems *int `json:"minItems,omitempty"`
141
142 // Or you can explicitly refer to another type. If this is defined all
143 // other fields should be empty
144 Schema *swaggerSchemaObject `json:"schema,omitempty"`
145}
146
147// core part of schema, which is common to itemsObject and schemaObject.
148// http://swagger.io/specification/#itemsObject
149type schemaCore struct {
150 Type string `json:"type,omitempty"`
151 Format string `json:"format,omitempty"`
152 Ref string `json:"$ref,omitempty"`
153 Example json.RawMessage `json:"example,omitempty"`
154
155 Items *swaggerItemsObject `json:"items,omitempty"`
156
157 // If the item is an enumeration include a list of all the *NAMES* of the
158 // enum values. I'm not sure how well this will work but assuming all enums
159 // start from 0 index it will be great. I don't think that is a good assumption.
160 Enum []string `json:"enum,omitempty"`
161 Default string `json:"default,omitempty"`
162}
163
164type swaggerItemsObject schemaCore
165
166// http://swagger.io/specification/#responsesObject
167type swaggerResponsesObject map[string]swaggerResponseObject
168
169// http://swagger.io/specification/#responseObject
170type swaggerResponseObject struct {
171 Description string `json:"description"`
172 Schema swaggerSchemaObject `json:"schema"`
173
174 extensions []extension
175}
176
177type keyVal struct {
178 Key string
179 Value interface{}
180}
181
182type swaggerSchemaObjectProperties []keyVal
183
184func (op swaggerSchemaObjectProperties) MarshalJSON() ([]byte, error) {
185 var buf bytes.Buffer
186 buf.WriteString("{")
187 for i, kv := range op {
188 if i != 0 {
189 buf.WriteString(",")
190 }
191 key, err := json.Marshal(kv.Key)
192 if err != nil {
193 return nil, err
194 }
195 buf.Write(key)
196 buf.WriteString(":")
197 val, err := json.Marshal(kv.Value)
198 if err != nil {
199 return nil, err
200 }
201 buf.Write(val)
202 }
203
204 buf.WriteString("}")
205 return buf.Bytes(), nil
206}
207
208// http://swagger.io/specification/#schemaObject
209type swaggerSchemaObject struct {
210 schemaCore
211 // Properties can be recursively defined
212 Properties *swaggerSchemaObjectProperties `json:"properties,omitempty"`
213 AdditionalProperties *swaggerSchemaObject `json:"additionalProperties,omitempty"`
214
215 Description string `json:"description,omitempty"`
216 Title string `json:"title,omitempty"`
217
218 ExternalDocs *swaggerExternalDocumentationObject `json:"externalDocs,omitempty"`
219
220 ReadOnly bool `json:"readOnly,omitempty"`
221 MultipleOf float64 `json:"multipleOf,omitempty"`
222 Maximum float64 `json:"maximum,omitempty"`
223 ExclusiveMaximum bool `json:"exclusiveMaximum,omitempty"`
224 Minimum float64 `json:"minimum,omitempty"`
225 ExclusiveMinimum bool `json:"exclusiveMinimum,omitempty"`
226 MaxLength uint64 `json:"maxLength,omitempty"`
227 MinLength uint64 `json:"minLength,omitempty"`
228 Pattern string `json:"pattern,omitempty"`
229 MaxItems uint64 `json:"maxItems,omitempty"`
230 MinItems uint64 `json:"minItems,omitempty"`
231 UniqueItems bool `json:"uniqueItems,omitempty"`
232 MaxProperties uint64 `json:"maxProperties,omitempty"`
233 MinProperties uint64 `json:"minProperties,omitempty"`
234 Required []string `json:"required,omitempty"`
235}
236
237// http://swagger.io/specification/#referenceObject
238type swaggerReferenceObject struct {
239 Ref string `json:"$ref"`
240}
241
242// http://swagger.io/specification/#definitionsObject
243type swaggerDefinitionsObject map[string]swaggerSchemaObject
244
245// Internal type mapping from FQMN to descriptor.Message. Used as a set by the
246// findServiceMessages function.
247type messageMap map[string]*descriptor.Message
248
249// Internal type mapping from FQEN to descriptor.Enum. Used as a set by the
250// findServiceMessages function.
251type enumMap map[string]*descriptor.Enum
252
253// Internal type to store used references.
254type refMap map[string]struct{}