1 // Copyright 2015 go-swagger maintainers
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
22 "github.com/go-openapi/jsonpointer"
23 "github.com/go-openapi/swag"
26 // Swagger this is the root document object for the API specification.
27 // It combines what previously was the Resource Listing and API Declaration (version 1.2 and earlier)
28 // together into one document.
30 // For more information: http://goo.gl/8us55a#swagger-object-
36 // JSONLookup look up a value by the json property name
37 func (s Swagger) JSONLookup(token string) (interface{}, error) {
38 if ex, ok := s.Extensions[token]; ok {
41 r, _, err := jsonpointer.GetForToken(s.SwaggerProps, token)
45 // MarshalJSON marshals this swagger structure to json
46 func (s Swagger) MarshalJSON() ([]byte, error) {
47 b1, err := json.Marshal(s.SwaggerProps)
51 b2, err := json.Marshal(s.VendorExtensible)
55 return swag.ConcatJSON(b1, b2), nil
58 // UnmarshalJSON unmarshals a swagger spec from json
59 func (s *Swagger) UnmarshalJSON(data []byte) error {
61 if err := json.Unmarshal(data, &sw.SwaggerProps); err != nil {
64 if err := json.Unmarshal(data, &sw.VendorExtensible); err != nil {
71 // SwaggerProps captures the top-level properties of an Api specification
73 // NOTE: validation rules
74 // - the scheme, when present must be from [http, https, ws, wss]
75 // - BasePath must start with a leading "/"
76 // - Paths is required
77 type SwaggerProps struct {
78 ID string `json:"id,omitempty"`
79 Consumes []string `json:"consumes,omitempty"`
80 Produces []string `json:"produces,omitempty"`
81 Schemes []string `json:"schemes,omitempty"`
82 Swagger string `json:"swagger,omitempty"`
83 Info *Info `json:"info,omitempty"`
84 Host string `json:"host,omitempty"`
85 BasePath string `json:"basePath,omitempty"`
86 Paths *Paths `json:"paths"`
87 Definitions Definitions `json:"definitions,omitempty"`
88 Parameters map[string]Parameter `json:"parameters,omitempty"`
89 Responses map[string]Response `json:"responses,omitempty"`
90 SecurityDefinitions SecurityDefinitions `json:"securityDefinitions,omitempty"`
91 Security []map[string][]string `json:"security,omitempty"`
92 Tags []Tag `json:"tags,omitempty"`
93 ExternalDocs *ExternalDocumentation `json:"externalDocs,omitempty"`
96 // Dependencies represent a dependencies property
97 type Dependencies map[string]SchemaOrStringArray
99 // SchemaOrBool represents a schema or boolean value, is biased towards true for the boolean property
100 type SchemaOrBool struct {
105 // JSONLookup implements an interface to customize json pointer lookup
106 func (s SchemaOrBool) JSONLookup(token string) (interface{}, error) {
107 if token == "allows" {
110 r, _, err := jsonpointer.GetForToken(s.Schema, token)
114 var jsTrue = []byte("true")
115 var jsFalse = []byte("false")
117 // MarshalJSON convert this object to JSON
118 func (s SchemaOrBool) MarshalJSON() ([]byte, error) {
120 return json.Marshal(s.Schema)
123 if s.Schema == nil && !s.Allows {
129 // UnmarshalJSON converts this bool or schema object from a JSON structure
130 func (s *SchemaOrBool) UnmarshalJSON(data []byte) error {
135 if err := json.Unmarshal(data, &sch); err != nil {
140 nw.Allows = !(data[0] == 'f' && data[1] == 'a' && data[2] == 'l' && data[3] == 's' && data[4] == 'e')
146 // SchemaOrStringArray represents a schema or a string array
147 type SchemaOrStringArray struct {
152 // JSONLookup implements an interface to customize json pointer lookup
153 func (s SchemaOrStringArray) JSONLookup(token string) (interface{}, error) {
154 r, _, err := jsonpointer.GetForToken(s.Schema, token)
158 // MarshalJSON converts this schema object or array into JSON structure
159 func (s SchemaOrStringArray) MarshalJSON() ([]byte, error) {
160 if len(s.Property) > 0 {
161 return json.Marshal(s.Property)
164 return json.Marshal(s.Schema)
166 return []byte("null"), nil
169 // UnmarshalJSON converts this schema object or array from a JSON structure
170 func (s *SchemaOrStringArray) UnmarshalJSON(data []byte) error {
175 var nw SchemaOrStringArray
178 if err := json.Unmarshal(data, &sch); err != nil {
184 if err := json.Unmarshal(data, &nw.Property); err != nil {
192 // Definitions contains the models explicitly defined in this spec
193 // An object to hold data types that can be consumed and produced by operations.
194 // These data types can be primitives, arrays or models.
196 // For more information: http://goo.gl/8us55a#definitionsObject
197 type Definitions map[string]Schema
199 // SecurityDefinitions a declaration of the security schemes available to be used in the specification.
200 // This does not enforce the security schemes on the operations and only serves to provide
201 // the relevant details for each scheme.
203 // For more information: http://goo.gl/8us55a#securityDefinitionsObject
204 type SecurityDefinitions map[string]*SecurityScheme
206 // StringOrArray represents a value that can either be a string
207 // or an array of strings. Mainly here for serialization purposes
208 type StringOrArray []string
210 // Contains returns true when the value is contained in the slice
211 func (s StringOrArray) Contains(value string) bool {
212 for _, str := range s {
220 // JSONLookup implements an interface to customize json pointer lookup
221 func (s SchemaOrArray) JSONLookup(token string) (interface{}, error) {
222 if _, err := strconv.Atoi(token); err == nil {
223 r, _, err := jsonpointer.GetForToken(s.Schemas, token)
226 r, _, err := jsonpointer.GetForToken(s.Schema, token)
230 // UnmarshalJSON unmarshals this string or array object from a JSON array or JSON string
231 func (s *StringOrArray) UnmarshalJSON(data []byte) error {
239 if err := json.Unmarshal(data, &parsed); err != nil {
242 *s = StringOrArray(parsed)
246 var single interface{}
247 if err := json.Unmarshal(data, &single); err != nil {
253 switch v := single.(type) {
255 *s = StringOrArray([]string{v})
258 return fmt.Errorf("only string or array is allowed, not %T", single)
262 // MarshalJSON converts this string or array to a JSON array or JSON string
263 func (s StringOrArray) MarshalJSON() ([]byte, error) {
265 return json.Marshal([]string(s)[0])
267 return json.Marshal([]string(s))
270 // SchemaOrArray represents a value that can either be a Schema
271 // or an array of Schema. Mainly here for serialization purposes
272 type SchemaOrArray struct {
277 // Len returns the number of schemas in this property
278 func (s SchemaOrArray) Len() int {
282 return len(s.Schemas)
285 // ContainsType returns true when one of the schemas is of the specified type
286 func (s *SchemaOrArray) ContainsType(name string) bool {
288 return s.Schema.Type != nil && s.Schema.Type.Contains(name)
293 // MarshalJSON converts this schema object or array into JSON structure
294 func (s SchemaOrArray) MarshalJSON() ([]byte, error) {
295 if len(s.Schemas) > 0 {
296 return json.Marshal(s.Schemas)
298 return json.Marshal(s.Schema)
301 // UnmarshalJSON converts this schema object or array from a JSON structure
302 func (s *SchemaOrArray) UnmarshalJSON(data []byte) error {
310 if err := json.Unmarshal(data, &sch); err != nil {
316 if err := json.Unmarshal(data, &nw.Schemas); err != nil {
324 // vim:set ft=go noet sts=2 sw=2 ts=2: