items.go 6.0 KB

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