Debian Codegraph @

pkg/validation/spec/response.go

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