k8s.io/kube-openapi@v0.0.0-20240228011516-70dd3763d340/pkg/validation/spec/operation.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  // OperationProps describes an operation
    26  //
    27  // NOTES:
    28  // - schemes, when present must be from [http, https, ws, wss]: see validate
    29  // - Security is handled as a special case: see MarshalJSON function
    30  type OperationProps struct {
    31  	Description  string                 `json:"description,omitempty"`
    32  	Consumes     []string               `json:"consumes,omitempty"`
    33  	Produces     []string               `json:"produces,omitempty"`
    34  	Schemes      []string               `json:"schemes,omitempty"`
    35  	Tags         []string               `json:"tags,omitempty"`
    36  	Summary      string                 `json:"summary,omitempty"`
    37  	ExternalDocs *ExternalDocumentation `json:"externalDocs,omitempty"`
    38  	ID           string                 `json:"operationId,omitempty"`
    39  	Deprecated   bool                   `json:"deprecated,omitempty"`
    40  	Security     []map[string][]string  `json:"security,omitempty"`
    41  	Parameters   []Parameter            `json:"parameters,omitempty"`
    42  	Responses    *Responses             `json:"responses,omitempty"`
    43  }
    44  
    45  // Marshaling structure only, always edit along with corresponding
    46  // struct (or compilation will fail).
    47  type operationPropsOmitZero struct {
    48  	Description  string                 `json:"description,omitempty"`
    49  	Consumes     []string               `json:"consumes,omitempty"`
    50  	Produces     []string               `json:"produces,omitempty"`
    51  	Schemes      []string               `json:"schemes,omitempty"`
    52  	Tags         []string               `json:"tags,omitempty"`
    53  	Summary      string                 `json:"summary,omitempty"`
    54  	ExternalDocs *ExternalDocumentation `json:"externalDocs,omitzero"`
    55  	ID           string                 `json:"operationId,omitempty"`
    56  	Deprecated   bool                   `json:"deprecated,omitempty,omitzero"`
    57  	Security     []map[string][]string  `json:"security,omitempty"`
    58  	Parameters   []Parameter            `json:"parameters,omitempty"`
    59  	Responses    *Responses             `json:"responses,omitzero"`
    60  }
    61  
    62  // MarshalJSON takes care of serializing operation properties to JSON
    63  //
    64  // We use a custom marhaller here to handle a special cases related to
    65  // the Security field. We need to preserve zero length slice
    66  // while omitting the field when the value is nil/unset.
    67  func (op OperationProps) MarshalJSON() ([]byte, error) {
    68  	type Alias OperationProps
    69  	if op.Security == nil {
    70  		return json.Marshal(&struct {
    71  			Security []map[string][]string `json:"security,omitempty"`
    72  			*Alias
    73  		}{
    74  			Security: op.Security,
    75  			Alias:    (*Alias)(&op),
    76  		})
    77  	}
    78  	return json.Marshal(&struct {
    79  		Security []map[string][]string `json:"security"`
    80  		*Alias
    81  	}{
    82  		Security: op.Security,
    83  		Alias:    (*Alias)(&op),
    84  	})
    85  }
    86  
    87  // Operation describes a single API operation on a path.
    88  //
    89  // For more information: http://goo.gl/8us55a#operationObject
    90  type Operation struct {
    91  	VendorExtensible
    92  	OperationProps
    93  }
    94  
    95  // UnmarshalJSON hydrates this items instance with the data from JSON
    96  func (o *Operation) UnmarshalJSON(data []byte) error {
    97  	if internal.UseOptimizedJSONUnmarshaling {
    98  		return jsonv2.Unmarshal(data, o)
    99  	}
   100  
   101  	if err := json.Unmarshal(data, &o.OperationProps); err != nil {
   102  		return err
   103  	}
   104  	return json.Unmarshal(data, &o.VendorExtensible)
   105  }
   106  
   107  func (o *Operation) UnmarshalNextJSON(opts jsonv2.UnmarshalOptions, dec *jsonv2.Decoder) error {
   108  	type OperationPropsNoMethods OperationProps // strip MarshalJSON method
   109  	var x struct {
   110  		Extensions
   111  		OperationPropsNoMethods
   112  	}
   113  	if err := opts.UnmarshalNext(dec, &x); err != nil {
   114  		return err
   115  	}
   116  	o.Extensions = internal.SanitizeExtensions(x.Extensions)
   117  	o.OperationProps = OperationProps(x.OperationPropsNoMethods)
   118  	return nil
   119  }
   120  
   121  // MarshalJSON converts this items object to JSON
   122  func (o Operation) MarshalJSON() ([]byte, error) {
   123  	if internal.UseOptimizedJSONMarshaling {
   124  		return internal.DeterministicMarshal(o)
   125  	}
   126  	b1, err := json.Marshal(o.OperationProps)
   127  	if err != nil {
   128  		return nil, err
   129  	}
   130  	b2, err := json.Marshal(o.VendorExtensible)
   131  	if err != nil {
   132  		return nil, err
   133  	}
   134  	concated := swag.ConcatJSON(b1, b2)
   135  	return concated, nil
   136  }
   137  
   138  func (o Operation) MarshalNextJSON(opts jsonv2.MarshalOptions, enc *jsonv2.Encoder) error {
   139  	var x struct {
   140  		Extensions
   141  		OperationProps operationPropsOmitZero `json:",inline"`
   142  	}
   143  	x.Extensions = internal.SanitizeExtensions(o.Extensions)
   144  	x.OperationProps = operationPropsOmitZero(o.OperationProps)
   145  	return opts.MarshalNext(enc, x)
   146  }