Debian Codegraph @

pkg/spec3/request_body.go

raw
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/internal/third_party/go-json-experiment/json/jsontext"
26 "k8s.io/kube-openapi/pkg/validation/spec"
27 )
28
29 // RequestBody describes a single request body, more at https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#requestBodyObject
30 //
31 // Note that this struct is actually a thin wrapper around RequestBodyProps to make it referable and extensible
32 type RequestBody struct {
33 spec.Refable
34 RequestBodyProps
35 spec.VendorExtensible
36 }
37
38 // MarshalJSON is a custom marshal function that knows how to encode RequestBody as JSON
39 func (r *RequestBody) MarshalJSON() ([]byte, error) {
40 if internal.UseOptimizedJSONMarshalingV3 {
41 return internal.DeterministicMarshal(r)
42 }
43 b1, err := json.Marshal(r.Refable)
44 if err != nil {
45 return nil, err
46 }
47 b2, err := json.Marshal(r.RequestBodyProps)
48 if err != nil {
49 return nil, err
50 }
51 b3, err := json.Marshal(r.VendorExtensible)
52 if err != nil {
53 return nil, err
54 }
55 return swag.ConcatJSON(b1, b2, b3), nil
56 }
57
58 func (r *RequestBody) MarshalJSONTo(enc *jsontext.Encoder) error {
59 var x struct {
60 Ref string `json:"$ref,omitempty"`
61 RequestBodyProps requestBodyPropsOmitZero `json:",inline"`
62 Extensions spec.Extensions `json:",inline"`
63 }
64 x.Ref = r.Refable.Ref.String()
65 x.Extensions = internal.SanitizeExtensions(r.Extensions)
66 x.RequestBodyProps = requestBodyPropsOmitZero(r.RequestBodyProps)
67 return jsonv2.MarshalEncode(enc, x)
68 }
69
70 func (r *RequestBody) UnmarshalJSON(data []byte) error {
71 if internal.UseOptimizedJSONUnmarshalingV3 {
72 return jsonv2.Unmarshal(data, r)
73 }
74 if err := json.Unmarshal(data, &r.Refable); err != nil {
75 return err
76 }
77 if err := json.Unmarshal(data, &r.RequestBodyProps); err != nil {
78 return err
79 }
80 if err := json.Unmarshal(data, &r.VendorExtensible); err != nil {
81 return err
82 }
83 return nil
84 }
85
86 // RequestBodyProps describes a single request body, more at https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#requestBodyObject
87 type RequestBodyProps struct {
88 // Description holds a brief description of the request body
89 Description string `json:"description,omitempty"`
90 // Content is the content of the request body. The key is a media type or media type range and the value describes it
91 Content map[string]*MediaType `json:"content,omitempty"`
92 // Required determines if the request body is required in the request
93 Required bool `json:"required,omitempty"`
94 }
95
96 type requestBodyPropsOmitZero struct {
97 Description string `json:"description,omitempty"`
98 Content map[string]*MediaType `json:"content,omitempty"`
99 Required bool `json:"required,omitzero"`
100 }
101
102 func (r *RequestBody) UnmarshalJSONFrom(dec *jsontext.Decoder) error {
103 var x struct {
104 Extensions spec.Extensions `json:",inline"`
105 RequestBodyProps
106 }
107 if err := jsonv2.UnmarshalDecode(dec, &x); err != nil {
108 return err
109 }
110 if err := internal.JSONRefFromMap(&r.Ref.Ref, x.Extensions); err != nil {
111 return err
112 }
113 r.Extensions = internal.SanitizeExtensions(x.Extensions)
114 r.RequestBodyProps = x.RequestBodyProps
115 return nil
116 }