forked from lazada/swgen
-
Notifications
You must be signed in to change notification settings - Fork 1
/
json_schema.go
295 lines (235 loc) · 7.17 KB
/
json_schema.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package swgen
import (
"strings"
"github.com/pkg/errors"
)
type schemaBuilder struct {
refs map[string]int64 // References counter
g *Generator
}
// JSONSchemaConfig is optional JSON schema rendering configuration.
type JSONSchemaConfig struct {
CollectDefinitions map[string]map[string]interface{}
StripDefinitions bool
DefinitionsPrefix string
}
// JSONSchema builds JSON Schema for Swagger Schema object.
func (g *Generator) JSONSchema(s SchemaObj, option ...JSONSchemaConfig) (map[string]interface{}, error) {
var cfg *JSONSchemaConfig
if len(option) != 0 {
cfg = &option[0]
}
refs := make(map[string]int64)
sb := &schemaBuilder{
refs: make(map[string]int64),
g: g,
}
res, err := sb.jsonSchemaPlain(s, cfg)
if err != nil {
return nil, err
}
allDef := g.definitions.GenDefinitions()
definitions := make(map[string]map[string]interface{})
for len(sb.refs) > 0 {
for ref, cnt := range sb.refs {
refs[ref] += cnt
}
sb.refs = make(map[string]int64)
for ref := range refs {
ref := strings.TrimPrefix(ref, "#/definitions/")
if _, ok := definitions[ref]; !ok {
jsonSchema, err := sb.jsonSchemaPlain(allDef[ref], cfg)
if err != nil {
return nil, err
}
definitions[ref] = jsonSchema
if cfg != nil && cfg.CollectDefinitions != nil {
cfg.CollectDefinitions[ref] = jsonSchema
}
}
}
}
// Expanding top-level reference
if ref, isString := res["$ref"].(string); isString && strings.HasPrefix(ref, "#/definitions/") {
defName := ref[len("#/definitions/"):]
def := definitions[defName]
if def != nil {
for k, v := range def {
res[k] = v
}
delete(res, "$ref")
// Delete root definition if only one (root) usage
if refs[ref] == 1 {
delete(definitions, defName)
}
}
}
// Inject definitions into result
if len(definitions) > 0 {
if cfg == nil || !cfg.StripDefinitions {
res["definitions"] = definitions
}
}
return res, nil
}
func (sb *schemaBuilder) jsonSchemaPlain(s SchemaObj, cfg *JSONSchemaConfig) (map[string]interface{}, error) {
if s.Ref != "" {
sb.refs[s.Ref]++
ref := s.Ref
if cfg != nil && cfg.DefinitionsPrefix != "" {
ref = cfg.DefinitionsPrefix + strings.TrimPrefix(ref, "#/definitions/")
}
return map[string]interface{}{"$ref": ref}, nil
}
res, err := jsonRecode(s)
if err != nil {
return nil, err
}
if s.Nullable && s.Type != "" {
res["type"] = []interface{}{s.Type, "null"}
}
if s.Properties != nil && len(s.Properties) > 0 {
properties := make(map[string]interface{}, len(s.Properties))
for name, schema := range s.Properties {
properties[name], err = sb.jsonSchemaPlain(schema, cfg)
if err != nil {
return nil, err
}
}
res["properties"] = properties
}
if s.AdditionalProperties != nil {
jsonSchema, err := sb.jsonSchemaPlain(*s.AdditionalProperties, cfg)
if err != nil {
return nil, err
}
res["additionalProperties"] = jsonSchema
}
if s.Items != nil {
jsonSchema, err := sb.jsonSchemaPlain(*s.Items, cfg)
if err != nil {
return nil, err
}
res["items"] = jsonSchema
}
return res, nil
}
// ParamJSONSchema builds JSON Schema for Swagger Parameter object.
func (g *Generator) ParamJSONSchema(p ParamObj, cfg ...JSONSchemaConfig) (map[string]interface{}, error) {
if p.Schema != nil {
return g.JSONSchema(*p.Schema, cfg...)
}
p.Name = ""
p.In = ""
p.Required = false
p.CollectionFormat = ""
if p.Type == "file" {
p.Type = ""
}
res, err := jsonRecode(p)
return res, err
}
// ObjectJSONSchema is a simplified JSON Schema for object.
type ObjectJSONSchema struct {
ID string `json:"id,omitempty"`
Schema string `json:"$schema,omitempty"`
Type string `json:"type"`
Required []string `json:"required,omitempty"`
Properties map[string]map[string]interface{} `json:"properties"`
}
// ToMap converts ObjectJSONSchema to a map.
func (o ObjectJSONSchema) ToMap() (map[string]interface{}, error) {
return jsonRecode(o)
}
// GetJSONSchemaRequestBody returns returns request body schema if any.
func (g *Generator) GetJSONSchemaRequestBody(op *OperationObj, cfg ...JSONSchemaConfig) (map[string]interface{}, error) {
for _, param := range op.Parameters {
if param.In == "body" {
schema, err := g.ParamJSONSchema(param, cfg...)
if err != nil {
return nil, err
}
return schema, nil
}
}
return nil, nil
}
// GetJSONSchemaRequestGroups returns a map of object schemas converted from parameters (excluding in body), grouped by in.
func (g *Generator) GetJSONSchemaRequestGroups(op *OperationObj, cfg ...JSONSchemaConfig) (map[string]ObjectJSONSchema, error) {
var err error
requestSchemas := map[string]ObjectJSONSchema{}
for _, param := range op.Parameters {
if param.In == "body" {
continue
}
if _, ok := requestSchemas[param.In]; !ok {
requestSchemas[param.In] = ObjectJSONSchema{
Schema: "http://json-schema.org/draft-04/schema#",
Type: "object",
Required: []string{},
Properties: map[string]map[string]interface{}{},
}
}
if param.Required {
rs := requestSchemas[param.In]
rs.Required = append(rs.Required, param.Name)
requestSchemas[param.In] = rs
}
requestSchemas[param.In].Properties[param.Name], err = g.ParamJSONSchema(param, cfg...)
if err != nil {
return nil, err
}
}
return requestSchemas, nil
}
// WalkJSONSchemaRequestGroups iterates over all request parameters grouped by path, method and in into an instance of JSON Schema.
func (g *Generator) WalkJSONSchemaRequestGroups(function func(path, method, in string, schema ObjectJSONSchema)) error {
for path, pi := range g.paths {
for method, op := range pi.Map() {
requestSchemas, err := g.GetJSONSchemaRequestGroups(op)
if err != nil {
return errors.Wrapf(err, "failed to get schema request groups schemas for %s %s", method, path)
}
for in, schema := range requestSchemas {
function(path, method, in, schema)
}
}
}
return nil
}
// WalkJSONSchemaRequestBodies iterates over all request bodies.
func (g *Generator) WalkJSONSchemaRequestBodies(function func(path, method string, schema map[string]interface{})) error {
for path, pi := range g.paths {
for method, op := range pi.Map() {
for _, param := range op.Parameters {
if param.In == "body" {
schema, err := g.ParamJSONSchema(param)
if err != nil {
return err
}
function(path, method, schema)
}
}
}
}
return nil
}
// WalkJSONSchemaResponses iterates over all responses grouped by path, method and status code into an instance of JSON Schema.
func (g *Generator) WalkJSONSchemaResponses(function func(path, method string, statusCode int, schema map[string]interface{})) error {
for path, pi := range g.paths {
for method, op := range pi.Map() {
for statusCode, resp := range op.Responses {
if resp.Schema == nil {
continue
}
schema, err := g.JSONSchema(*resp.Schema)
if err != nil {
return errors.Wrapf(err, "failed to get response schema for %s %s %d", method, path, statusCode)
}
schema["$schema"] = "http://json-schema.org/draft-04/schema#"
function(path, method, statusCode, schema)
}
}
}
return nil
}