| 1 |
// Copyright 2015 go-swagger maintainers |
| 2 |
// |
| 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 |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 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. |
| 14 |
|
| 15 |
package spec |
| 16 |
|
| 17 |
import ( |
| 18 |
"encoding/json" |
| 19 |
|
| 20 |
"github.com/go-openapi/swag" |
| 21 |
"k8s.io/kube-openapi/pkg/internal" |
| 22 |
jsonv2 "k8s.io/kube-openapi/pkg/internal/third_party/go-json-experiment/json" |
| 23 |
"k8s.io/kube-openapi/pkg/internal/third_party/go-json-experiment/json/jsontext" |
| 24 |
) |
| 25 |
|
| 26 |
const ( |
| 27 |
jsonRef = "$ref" |
| 28 |
) |
| 29 |
|
| 30 |
// SimpleSchema describe swagger simple schemas for parameters and headers |
| 31 |
type SimpleSchema struct { |
| 32 |
Type string `json:"type,omitempty"` |
| 33 |
Nullable bool `json:"nullable,omitempty"` |
| 34 |
Format string `json:"format,omitempty"` |
| 35 |
Items *Items `json:"items,omitempty"` |
| 36 |
CollectionFormat string `json:"collectionFormat,omitempty"` |
| 37 |
Default interface{} `json:"default,omitempty"` |
| 38 |
Example interface{} `json:"example,omitempty"` |
| 39 |
} |
| 40 |
|
| 41 |
// Marshaling structure only, always edit along with corresponding |
| 42 |
// struct (or compilation will fail). |
| 43 |
type simpleSchemaOmitZero struct { |
| 44 |
Type string `json:"type,omitempty"` |
| 45 |
Nullable bool `json:"nullable,omitzero"` |
| 46 |
Format string `json:"format,omitempty"` |
| 47 |
Items *Items `json:"items,omitzero"` |
| 48 |
CollectionFormat string `json:"collectionFormat,omitempty"` |
| 49 |
Default interface{} `json:"default,omitempty"` |
| 50 |
Example interface{} `json:"example,omitempty"` |
| 51 |
} |
| 52 |
|
| 53 |
// CommonValidations describe common JSON-schema validations |
| 54 |
type CommonValidations struct { |
| 55 |
Maximum *float64 `json:"maximum,omitempty"` |
| 56 |
ExclusiveMaximum bool `json:"exclusiveMaximum,omitempty"` |
| 57 |
Minimum *float64 `json:"minimum,omitempty"` |
| 58 |
ExclusiveMinimum bool `json:"exclusiveMinimum,omitempty"` |
| 59 |
MaxLength *int64 `json:"maxLength,omitempty"` |
| 60 |
MinLength *int64 `json:"minLength,omitempty"` |
| 61 |
Pattern string `json:"pattern,omitempty"` |
| 62 |
MaxItems *int64 `json:"maxItems,omitempty"` |
| 63 |
MinItems *int64 `json:"minItems,omitempty"` |
| 64 |
UniqueItems bool `json:"uniqueItems,omitempty"` |
| 65 |
MultipleOf *float64 `json:"multipleOf,omitempty"` |
| 66 |
Enum []interface{} `json:"enum,omitempty"` |
| 67 |
} |
| 68 |
|
| 69 |
// Marshaling structure only, always edit along with corresponding |
| 70 |
// struct (or compilation will fail). |
| 71 |
type commonValidationsOmitZero struct { |
| 72 |
Maximum *float64 `json:"maximum,omitempty"` |
| 73 |
ExclusiveMaximum bool `json:"exclusiveMaximum,omitzero"` |
| 74 |
Minimum *float64 `json:"minimum,omitempty"` |
| 75 |
ExclusiveMinimum bool `json:"exclusiveMinimum,omitzero"` |
| 76 |
MaxLength *int64 `json:"maxLength,omitempty"` |
| 77 |
MinLength *int64 `json:"minLength,omitempty"` |
| 78 |
Pattern string `json:"pattern,omitempty"` |
| 79 |
MaxItems *int64 `json:"maxItems,omitempty"` |
| 80 |
MinItems *int64 `json:"minItems,omitempty"` |
| 81 |
UniqueItems bool `json:"uniqueItems,omitzero"` |
| 82 |
MultipleOf *float64 `json:"multipleOf,omitempty"` |
| 83 |
Enum []interface{} `json:"enum,omitempty"` |
| 84 |
} |
| 85 |
|
| 86 |
// Items a limited subset of JSON-Schema's items object. |
| 87 |
// It is used by parameter definitions that are not located in "body". |
| 88 |
// |
| 89 |
// For more information: http://goo.gl/8us55a#items-object |
| 90 |
type Items struct { |
| 91 |
Refable |
| 92 |
CommonValidations |
| 93 |
SimpleSchema |
| 94 |
VendorExtensible |
| 95 |
} |
| 96 |
|
| 97 |
// UnmarshalJSON hydrates this items instance with the data from JSON |
| 98 |
func (i *Items) UnmarshalJSON(data []byte) error { |
| 99 |
if internal.UseOptimizedJSONUnmarshaling { |
| 100 |
return jsonv2.Unmarshal(data, i) |
| 101 |
} |
| 102 |
|
| 103 |
var validations CommonValidations |
| 104 |
if err := json.Unmarshal(data, &validations); err != nil { |
| 105 |
return err |
| 106 |
} |
| 107 |
var ref Refable |
| 108 |
if err := json.Unmarshal(data, &ref); err != nil { |
| 109 |
return err |
| 110 |
} |
| 111 |
var simpleSchema SimpleSchema |
| 112 |
if err := json.Unmarshal(data, &simpleSchema); err != nil { |
| 113 |
return err |
| 114 |
} |
| 115 |
var vendorExtensible VendorExtensible |
| 116 |
if err := json.Unmarshal(data, &vendorExtensible); err != nil { |
| 117 |
return err |
| 118 |
} |
| 119 |
i.Refable = ref |
| 120 |
i.CommonValidations = validations |
| 121 |
i.SimpleSchema = simpleSchema |
| 122 |
i.VendorExtensible = vendorExtensible |
| 123 |
return nil |
| 124 |
} |
| 125 |
|
| 126 |
func (i *Items) UnmarshalJSONFrom(dec *jsontext.Decoder) error { |
| 127 |
var x struct { |
| 128 |
CommonValidations |
| 129 |
SimpleSchema |
| 130 |
Extensions Extensions `json:",inline"` |
| 131 |
} |
| 132 |
if err := jsonv2.UnmarshalDecode(dec, &x); err != nil { |
| 133 |
return err |
| 134 |
} |
| 135 |
if err := i.Refable.Ref.fromMap(x.Extensions); err != nil { |
| 136 |
return err |
| 137 |
} |
| 138 |
|
| 139 |
i.CommonValidations = x.CommonValidations |
| 140 |
i.SimpleSchema = x.SimpleSchema |
| 141 |
i.Extensions = internal.SanitizeExtensions(x.Extensions) |
| 142 |
return nil |
| 143 |
} |
| 144 |
|
| 145 |
// MarshalJSON converts this items object to JSON |
| 146 |
func (i Items) MarshalJSON() ([]byte, error) { |
| 147 |
if internal.UseOptimizedJSONMarshaling { |
| 148 |
return internal.DeterministicMarshal(i) |
| 149 |
} |
| 150 |
b1, err := json.Marshal(i.CommonValidations) |
| 151 |
if err != nil { |
| 152 |
return nil, err |
| 153 |
} |
| 154 |
b2, err := json.Marshal(i.SimpleSchema) |
| 155 |
if err != nil { |
| 156 |
return nil, err |
| 157 |
} |
| 158 |
b3, err := json.Marshal(i.Refable) |
| 159 |
if err != nil { |
| 160 |
return nil, err |
| 161 |
} |
| 162 |
b4, err := json.Marshal(i.VendorExtensible) |
| 163 |
if err != nil { |
| 164 |
return nil, err |
| 165 |
} |
| 166 |
return swag.ConcatJSON(b4, b3, b1, b2), nil |
| 167 |
} |
| 168 |
|
| 169 |
func (i Items) MarshalJSONTo(enc *jsontext.Encoder) error { |
| 170 |
var x struct { |
| 171 |
CommonValidations commonValidationsOmitZero `json:",inline"` |
| 172 |
SimpleSchema simpleSchemaOmitZero `json:",inline"` |
| 173 |
Ref string `json:"$ref,omitempty"` |
| 174 |
Extensions Extensions `json:",inline"` |
| 175 |
} |
| 176 |
x.CommonValidations = commonValidationsOmitZero(i.CommonValidations) |
| 177 |
x.SimpleSchema = simpleSchemaOmitZero(i.SimpleSchema) |
| 178 |
x.Ref = i.Refable.Ref.String() |
| 179 |
x.Extensions = internal.SanitizeExtensions(i.Extensions) |
| 180 |
return jsonv2.MarshalEncode(enc, x) |
| 181 |
} |