k8s.io/kube-openapi@v0.0.0-20240228011516-70dd3763d340/pkg/spec3/encoding.go (about)

     1  /*
     2  Copyright 2021 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package spec3
    18  
    19  import (
    20  	"encoding/json"
    21  
    22  	"github.com/go-openapi/swag"
    23  	"k8s.io/kube-openapi/pkg/internal"
    24  	jsonv2 "k8s.io/kube-openapi/pkg/internal/third_party/go-json-experiment/json"
    25  	"k8s.io/kube-openapi/pkg/validation/spec"
    26  )
    27  
    28  type Encoding struct {
    29  	EncodingProps
    30  	spec.VendorExtensible
    31  }
    32  
    33  // MarshalJSON is a custom marshal function that knows how to encode Encoding as JSON
    34  func (e *Encoding) MarshalJSON() ([]byte, error) {
    35  	if internal.UseOptimizedJSONMarshalingV3 {
    36  		return internal.DeterministicMarshal(e)
    37  	}
    38  	b1, err := json.Marshal(e.EncodingProps)
    39  	if err != nil {
    40  		return nil, err
    41  	}
    42  	b2, err := json.Marshal(e.VendorExtensible)
    43  	if err != nil {
    44  		return nil, err
    45  	}
    46  	return swag.ConcatJSON(b1, b2), nil
    47  }
    48  
    49  func (e *Encoding) MarshalNextJSON(opts jsonv2.MarshalOptions, enc *jsonv2.Encoder) error {
    50  	var x struct {
    51  		EncodingProps encodingPropsOmitZero `json:",inline"`
    52  		spec.Extensions
    53  	}
    54  	x.Extensions = internal.SanitizeExtensions(e.Extensions)
    55  	x.EncodingProps = encodingPropsOmitZero(e.EncodingProps)
    56  	return opts.MarshalNext(enc, x)
    57  }
    58  
    59  func (e *Encoding) UnmarshalJSON(data []byte) error {
    60  	if internal.UseOptimizedJSONUnmarshalingV3 {
    61  		return jsonv2.Unmarshal(data, e)
    62  	}
    63  	if err := json.Unmarshal(data, &e.EncodingProps); err != nil {
    64  		return err
    65  	}
    66  	if err := json.Unmarshal(data, &e.VendorExtensible); err != nil {
    67  		return err
    68  	}
    69  	return nil
    70  }
    71  
    72  func (e *Encoding) UnmarshalNextJSON(opts jsonv2.UnmarshalOptions, dec *jsonv2.Decoder) error {
    73  	var x struct {
    74  		spec.Extensions
    75  		EncodingProps
    76  	}
    77  	if err := opts.UnmarshalNext(dec, &x); err != nil {
    78  		return err
    79  	}
    80  
    81  	e.Extensions = internal.SanitizeExtensions(x.Extensions)
    82  	e.EncodingProps = x.EncodingProps
    83  	return nil
    84  }
    85  
    86  type EncodingProps struct {
    87  	// Content Type for encoding a specific property
    88  	ContentType string `json:"contentType,omitempty"`
    89  	// A map allowing additional information to be provided as headers
    90  	Headers map[string]*Header `json:"headers,omitempty"`
    91  	// Describes how a specific property value will be serialized depending on its type
    92  	Style string `json:"style,omitempty"`
    93  	// When this is true, property values of type array or object generate separate parameters for each value of the array, or key-value-pair of the map. For other types of properties this property has no effect
    94  	Explode bool `json:"explode,omitempty"`
    95  	// AllowReserved determines whether the parameter value SHOULD allow reserved characters, as defined by RFC3986
    96  	AllowReserved bool `json:"allowReserved,omitempty"`
    97  }
    98  
    99  type encodingPropsOmitZero struct {
   100  	ContentType   string             `json:"contentType,omitempty"`
   101  	Headers       map[string]*Header `json:"headers,omitempty"`
   102  	Style         string             `json:"style,omitempty"`
   103  	Explode       bool               `json:"explode,omitzero"`
   104  	AllowReserved bool               `json:"allowReserved,omitzero"`
   105  }