k8s.io/kube-openapi@v0.0.0-20240228011516-70dd3763d340/pkg/validation/spec/items.go (about)

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