k8s.io/kube-openapi@v0.0.0-20240228011516-70dd3763d340/pkg/validation/spec/response.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  // ResponseProps properties specific to a response
    26  type ResponseProps struct {
    27  	Description string                 `json:"description,omitempty"`
    28  	Schema      *Schema                `json:"schema,omitempty"`
    29  	Headers     map[string]Header      `json:"headers,omitempty"`
    30  	Examples    map[string]interface{} `json:"examples,omitempty"`
    31  }
    32  
    33  // Marshaling structure only, always edit along with corresponding
    34  // struct (or compilation will fail).
    35  type responsePropsOmitZero struct {
    36  	Description string                 `json:"description,omitempty"`
    37  	Schema      *Schema                `json:"schema,omitzero"`
    38  	Headers     map[string]Header      `json:"headers,omitempty"`
    39  	Examples    map[string]interface{} `json:"examples,omitempty"`
    40  }
    41  
    42  // Response describes a single response from an API Operation.
    43  //
    44  // For more information: http://goo.gl/8us55a#responseObject
    45  type Response struct {
    46  	Refable
    47  	ResponseProps
    48  	VendorExtensible
    49  }
    50  
    51  // UnmarshalJSON hydrates this items instance with the data from JSON
    52  func (r *Response) UnmarshalJSON(data []byte) error {
    53  	if internal.UseOptimizedJSONUnmarshaling {
    54  		return jsonv2.Unmarshal(data, r)
    55  	}
    56  
    57  	if err := json.Unmarshal(data, &r.ResponseProps); err != nil {
    58  		return err
    59  	}
    60  	if err := json.Unmarshal(data, &r.Refable); err != nil {
    61  		return err
    62  	}
    63  	if err := json.Unmarshal(data, &r.VendorExtensible); err != nil {
    64  		return err
    65  	}
    66  
    67  	return nil
    68  }
    69  
    70  func (r *Response) UnmarshalNextJSON(opts jsonv2.UnmarshalOptions, dec *jsonv2.Decoder) error {
    71  	var x struct {
    72  		ResponseProps
    73  		Extensions
    74  	}
    75  
    76  	if err := opts.UnmarshalNext(dec, &x); err != nil {
    77  		return err
    78  	}
    79  
    80  	if err := r.Refable.Ref.fromMap(x.Extensions); err != nil {
    81  		return err
    82  	}
    83  	r.Extensions = internal.SanitizeExtensions(x.Extensions)
    84  	r.ResponseProps = x.ResponseProps
    85  
    86  	return nil
    87  }
    88  
    89  // MarshalJSON converts this items object to JSON
    90  func (r Response) MarshalJSON() ([]byte, error) {
    91  	if internal.UseOptimizedJSONMarshaling {
    92  		return internal.DeterministicMarshal(r)
    93  	}
    94  	b1, err := json.Marshal(r.ResponseProps)
    95  	if err != nil {
    96  		return nil, err
    97  	}
    98  	b2, err := json.Marshal(r.Refable)
    99  	if err != nil {
   100  		return nil, err
   101  	}
   102  	b3, err := json.Marshal(r.VendorExtensible)
   103  	if err != nil {
   104  		return nil, err
   105  	}
   106  	return swag.ConcatJSON(b1, b2, b3), nil
   107  }
   108  
   109  func (r Response) MarshalNextJSON(opts jsonv2.MarshalOptions, enc *jsonv2.Encoder) error {
   110  	var x struct {
   111  		Ref string `json:"$ref,omitempty"`
   112  		Extensions
   113  		ResponseProps responsePropsOmitZero `json:",inline"`
   114  	}
   115  	x.Ref = r.Refable.Ref.String()
   116  	x.Extensions = internal.SanitizeExtensions(r.Extensions)
   117  	x.ResponseProps = responsePropsOmitZero(r.ResponseProps)
   118  	return opts.MarshalNext(enc, x)
   119  }
   120  
   121  // NewResponse creates a new response instance
   122  func NewResponse() *Response {
   123  	return new(Response)
   124  }
   125  
   126  // ResponseRef creates a response as a json reference
   127  func ResponseRef(url string) *Response {
   128  	resp := NewResponse()
   129  	resp.Ref = MustCreateRef(url)
   130  	return resp
   131  }