items.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. // CommonValidations describe common JSON-schema validations
  35. type CommonValidations struct {
  36. Maximum *float64 `json:"maximum,omitempty"`
  37. ExclusiveMaximum bool `json:"exclusiveMaximum,omitempty"`
  38. Minimum *float64 `json:"minimum,omitempty"`
  39. ExclusiveMinimum bool `json:"exclusiveMinimum,omitempty"`
  40. MaxLength *int64 `json:"maxLength,omitempty"`
  41. MinLength *int64 `json:"minLength,omitempty"`
  42. Pattern string `json:"pattern,omitempty"`
  43. MaxItems *int64 `json:"maxItems,omitempty"`
  44. MinItems *int64 `json:"minItems,omitempty"`
  45. UniqueItems bool `json:"uniqueItems,omitempty"`
  46. MultipleOf *float64 `json:"multipleOf,omitempty"`
  47. Enum []interface{} `json:"enum,omitempty"`
  48. }
  49. // Items a limited subset of JSON-Schema's items object.
  50. // It is used by parameter definitions that are not located in "body".
  51. //
  52. // For more information: http://goo.gl/8us55a#items-object
  53. type Items struct {
  54. Refable
  55. CommonValidations
  56. SimpleSchema
  57. VendorExtensible
  58. }
  59. // UnmarshalJSON hydrates this items instance with the data from JSON
  60. func (i *Items) UnmarshalJSON(data []byte) error {
  61. if internal.UseOptimizedJSONUnmarshaling {
  62. return jsonv2.Unmarshal(data, i)
  63. }
  64. var validations CommonValidations
  65. if err := json.Unmarshal(data, &validations); err != nil {
  66. return err
  67. }
  68. var ref Refable
  69. if err := json.Unmarshal(data, &ref); err != nil {
  70. return err
  71. }
  72. var simpleSchema SimpleSchema
  73. if err := json.Unmarshal(data, &simpleSchema); err != nil {
  74. return err
  75. }
  76. var vendorExtensible VendorExtensible
  77. if err := json.Unmarshal(data, &vendorExtensible); err != nil {
  78. return err
  79. }
  80. i.Refable = ref
  81. i.CommonValidations = validations
  82. i.SimpleSchema = simpleSchema
  83. i.VendorExtensible = vendorExtensible
  84. return nil
  85. }
  86. func (i *Items) UnmarshalNextJSON(opts jsonv2.UnmarshalOptions, dec *jsonv2.Decoder) error {
  87. var x struct {
  88. CommonValidations
  89. SimpleSchema
  90. Extensions
  91. }
  92. if err := opts.UnmarshalNext(dec, &x); err != nil {
  93. return err
  94. }
  95. if err := i.Refable.Ref.fromMap(x.Extensions); err != nil {
  96. return err
  97. }
  98. x.Extensions.sanitize()
  99. if len(x.Extensions) == 0 {
  100. x.Extensions = nil
  101. }
  102. i.CommonValidations = x.CommonValidations
  103. i.SimpleSchema = x.SimpleSchema
  104. i.VendorExtensible.Extensions = x.Extensions
  105. return nil
  106. }
  107. // MarshalJSON converts this items object to JSON
  108. func (i Items) MarshalJSON() ([]byte, error) {
  109. b1, err := json.Marshal(i.CommonValidations)
  110. if err != nil {
  111. return nil, err
  112. }
  113. b2, err := json.Marshal(i.SimpleSchema)
  114. if err != nil {
  115. return nil, err
  116. }
  117. b3, err := json.Marshal(i.Refable)
  118. if err != nil {
  119. return nil, err
  120. }
  121. b4, err := json.Marshal(i.VendorExtensible)
  122. if err != nil {
  123. return nil, err
  124. }
  125. return swag.ConcatJSON(b4, b3, b1, b2), nil
  126. }