Debian Codegraph @

pkg/spec3/request_body.go

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