forked from go-swagger/go-swagger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spec.go
255 lines (221 loc) · 6.16 KB
/
spec.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
// Copyright 2015 go-swagger maintainers
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package spec
import (
"encoding/json"
"fmt"
"net/url"
"path/filepath"
"github.com/go-swagger/go-swagger/swag"
)
//go:generate go-bindata -pkg=spec -prefix=./schemas -ignore=.*\.md ./schemas/...
//go:generate perl -pi -e s,Json,JSON,g bindata.go
const (
// SwaggerSchemaURL the url for the swagger 2.0 schema to validate specs
SwaggerSchemaURL = "http://swagger.io/v2/schema.json#"
// JSONSchemaURL the url for the json schema schema
JSONSchemaURL = "http://json-schema.org/draft-04/schema#"
)
var (
jsonSchema = MustLoadJSONSchemaDraft04()
swaggerSchema = MustLoadSwagger20Schema()
)
// DocLoader represents a doc loader type
type DocLoader func(string) (json.RawMessage, error)
// JSONSpec loads a spec from a json document
func JSONSpec(path string) (*Document, error) {
data, err := swag.JSONDoc(path)
if err != nil {
return nil, err
}
// convert to json
return New(json.RawMessage(data), "")
}
// YAMLSpec loads a swagger spec document
func YAMLSpec(path string) (*Document, error) {
data, err := swag.YAMLDoc(path)
if err != nil {
return nil, err
}
return New(data, "")
}
// MustLoadJSONSchemaDraft04 panics when Swagger20Schema returns an error
func MustLoadJSONSchemaDraft04() *Schema {
d, e := JSONSchemaDraft04()
if e != nil {
panic(e)
}
return d
}
// JSONSchemaDraft04 loads the json schema document for json shema draft04
func JSONSchemaDraft04() (*Schema, error) {
b, err := Asset("jsonschema-draft-04.json")
if err != nil {
return nil, err
}
schema := new(Schema)
if err := json.Unmarshal(b, schema); err != nil {
return nil, err
}
return schema, nil
}
// MustLoadSwagger20Schema panics when Swagger20Schema returns an error
func MustLoadSwagger20Schema() *Schema {
d, e := Swagger20Schema()
if e != nil {
panic(e)
}
return d
}
// Swagger20Schema loads the swagger 2.0 schema from the embedded assets
func Swagger20Schema() (*Schema, error) {
b, err := Asset("v2/schema.json")
if err != nil {
return nil, err
}
schema := new(Schema)
if err := json.Unmarshal(b, schema); err != nil {
return nil, err
}
return schema, nil
}
// Document represents a swagger spec document
type Document struct {
specAnalyzer
spec *Swagger
raw json.RawMessage
}
// Load loads a new spec document
func Load(path string) (*Document, error) {
specURL, err := url.Parse(path)
if err != nil {
return nil, err
}
ext := filepath.Ext(specURL.Path)
if ext == ".yaml" || ext == ".yml" {
return YAMLSpec(path)
}
return JSONSpec(path)
}
// New creates a new shema document
func New(data json.RawMessage, version string) (*Document, error) {
if version == "" {
version = "2.0"
}
if version != "2.0" {
return nil, fmt.Errorf("spec version %q is not supported", version)
}
spec := new(Swagger)
if err := json.Unmarshal(data, spec); err != nil {
return nil, err
}
d := &Document{
specAnalyzer: specAnalyzer{
spec: spec,
consumes: make(map[string]struct{}),
produces: make(map[string]struct{}),
authSchemes: make(map[string]struct{}),
operations: make(map[string]map[string]*Operation),
referenced: referenceAnalysis{
schemas: make(map[string]SchemaRef),
responses: make(map[string]*Response),
parameters: make(map[string]*Parameter),
},
allSchemas: make(map[string]SchemaRef),
allOfs: make(map[string]SchemaRef),
},
spec: spec,
raw: data,
}
d.initialize()
return d, nil
}
// Expanded expands the ref fields in the spec document and returns a new spec document
func (d *Document) Expanded() (*Document, error) {
spec := new(Swagger)
if err := json.Unmarshal(d.raw, spec); err != nil {
return nil, err
}
if err := expandSpec(spec); err != nil {
return nil, err
}
dd := &Document{
specAnalyzer: specAnalyzer{
spec: spec,
consumes: make(map[string]struct{}),
produces: make(map[string]struct{}),
authSchemes: make(map[string]struct{}),
operations: make(map[string]map[string]*Operation),
referenced: referenceAnalysis{
schemas: make(map[string]SchemaRef),
responses: make(map[string]*Response),
parameters: make(map[string]*Parameter),
},
allSchemas: make(map[string]SchemaRef),
allOfs: make(map[string]SchemaRef),
},
spec: spec,
raw: d.raw,
}
dd.initialize()
return dd, nil
}
// BasePath the base path for this spec
func (d *Document) BasePath() string {
return d.spec.BasePath
}
// Version returns the version of this spec
func (d *Document) Version() string {
return d.spec.Swagger
}
// Schema returns the swagger 2.0 schema
func (d *Document) Schema() *Schema {
return swaggerSchema
}
// Spec returns the swagger spec object model
func (d *Document) Spec() *Swagger {
return d.spec
}
// Host returns the host for the API
func (d *Document) Host() string {
return d.spec.Host
}
// Raw returns the raw swagger spec as json bytes
func (d *Document) Raw() json.RawMessage {
return d.raw
}
// Reload reanalyzes the spec
func (d *Document) Reload() *Document {
d.specAnalyzer = specAnalyzer{
spec: d.spec,
consumes: make(map[string]struct{}),
produces: make(map[string]struct{}),
authSchemes: make(map[string]struct{}),
operations: make(map[string]map[string]*Operation),
referenced: referenceAnalysis{
schemas: make(map[string]SchemaRef),
responses: make(map[string]*Response),
parameters: make(map[string]*Parameter),
},
allSchemas: make(map[string]SchemaRef),
allOfs: make(map[string]SchemaRef),
}
d.initialize()
return d
}
// Prisitine creates a new pristine document instance based on the input data
func (d *Document) Pristine() *Document {
dd, _ := New(d.Raw(), d.Version())
return dd
}