k8s.io/kube-openapi@v0.0.0-20240228011516-70dd3763d340/pkg/validation/spec/path_item.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  // PathItemProps the path item specific properties
    26  type PathItemProps struct {
    27  	Get        *Operation  `json:"get,omitempty"`
    28  	Put        *Operation  `json:"put,omitempty"`
    29  	Post       *Operation  `json:"post,omitempty"`
    30  	Delete     *Operation  `json:"delete,omitempty"`
    31  	Options    *Operation  `json:"options,omitempty"`
    32  	Head       *Operation  `json:"head,omitempty"`
    33  	Patch      *Operation  `json:"patch,omitempty"`
    34  	Parameters []Parameter `json:"parameters,omitempty"`
    35  }
    36  
    37  // PathItem describes the operations available on a single path.
    38  // A Path Item may be empty, due to [ACL constraints](http://goo.gl/8us55a#securityFiltering).
    39  // The path itself is still exposed to the documentation viewer but they will
    40  // not know which operations and parameters are available.
    41  //
    42  // For more information: http://goo.gl/8us55a#pathItemObject
    43  type PathItem struct {
    44  	Refable
    45  	VendorExtensible
    46  	PathItemProps
    47  }
    48  
    49  // UnmarshalJSON hydrates this items instance with the data from JSON
    50  func (p *PathItem) UnmarshalJSON(data []byte) error {
    51  	if internal.UseOptimizedJSONUnmarshaling {
    52  		return jsonv2.Unmarshal(data, p)
    53  	}
    54  
    55  	if err := json.Unmarshal(data, &p.Refable); err != nil {
    56  		return err
    57  	}
    58  	if err := json.Unmarshal(data, &p.VendorExtensible); err != nil {
    59  		return err
    60  	}
    61  	return json.Unmarshal(data, &p.PathItemProps)
    62  }
    63  
    64  func (p *PathItem) UnmarshalNextJSON(opts jsonv2.UnmarshalOptions, dec *jsonv2.Decoder) error {
    65  	var x struct {
    66  		Extensions
    67  		PathItemProps
    68  	}
    69  
    70  	if err := opts.UnmarshalNext(dec, &x); err != nil {
    71  		return err
    72  	}
    73  	if err := p.Refable.Ref.fromMap(x.Extensions); err != nil {
    74  		return err
    75  	}
    76  	p.Extensions = internal.SanitizeExtensions(x.Extensions)
    77  	p.PathItemProps = x.PathItemProps
    78  
    79  	return nil
    80  }
    81  
    82  // MarshalJSON converts this items object to JSON
    83  func (p PathItem) MarshalJSON() ([]byte, error) {
    84  	if internal.UseOptimizedJSONMarshaling {
    85  		return internal.DeterministicMarshal(p)
    86  	}
    87  	b3, err := json.Marshal(p.Refable)
    88  	if err != nil {
    89  		return nil, err
    90  	}
    91  	b4, err := json.Marshal(p.VendorExtensible)
    92  	if err != nil {
    93  		return nil, err
    94  	}
    95  	b5, err := json.Marshal(p.PathItemProps)
    96  	if err != nil {
    97  		return nil, err
    98  	}
    99  	concated := swag.ConcatJSON(b3, b4, b5)
   100  	return concated, nil
   101  }
   102  
   103  func (p PathItem) MarshalNextJSON(opts jsonv2.MarshalOptions, enc *jsonv2.Encoder) error {
   104  	var x struct {
   105  		Ref string `json:"$ref,omitempty"`
   106  		Extensions
   107  		PathItemProps
   108  	}
   109  	x.Ref = p.Refable.Ref.String()
   110  	x.Extensions = internal.SanitizeExtensions(p.Extensions)
   111  	x.PathItemProps = p.PathItemProps
   112  	return opts.MarshalNext(enc, x)
   113  }