Debian Codegraph @

pkg/spec3/media_type.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 // MediaType a struct that allows you to specify content format, more at https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#mediaTypeObject
29 //
30 // Note that this struct is actually a thin wrapper around MediaTypeProps to make it referable and extensible
31 type MediaType struct {
32 MediaTypeProps
33 spec.VendorExtensible
34 }
35
36 // MarshalJSON is a custom marshal function that knows how to encode MediaType as JSON
37 func (m *MediaType) MarshalJSON() ([]byte, error) {
38 if internal.UseOptimizedJSONMarshalingV3 {
39 return internal.DeterministicMarshal(m)
40 }
41 b1, err := json.Marshal(m.MediaTypeProps)
42 if err != nil {
43 return nil, err
44 }
45 b2, err := json.Marshal(m.VendorExtensible)
46 if err != nil {
47 return nil, err
48 }
49 return swag.ConcatJSON(b1, b2), nil
50 }
51
52 func (e *MediaType) MarshalNextJSON(opts jsonv2.MarshalOptions, enc *jsonv2.Encoder) error {
53 var x struct {
54 MediaTypeProps mediaTypePropsOmitZero `json:",inline"`
55 spec.Extensions
56 }
57 x.Extensions = internal.SanitizeExtensions(e.Extensions)
58 x.MediaTypeProps = mediaTypePropsOmitZero(e.MediaTypeProps)
59 return opts.MarshalNext(enc, x)
60 }
61
62 func (m *MediaType) UnmarshalJSON(data []byte) error {
63 if internal.UseOptimizedJSONUnmarshalingV3 {
64 return jsonv2.Unmarshal(data, m)
65 }
66 if err := json.Unmarshal(data, &m.MediaTypeProps); err != nil {
67 return err
68 }
69 if err := json.Unmarshal(data, &m.VendorExtensible); err != nil {
70 return err
71 }
72 return nil
73 }
74
75 func (m *MediaType) UnmarshalNextJSON(opts jsonv2.UnmarshalOptions, dec *jsonv2.Decoder) error {
76 var x struct {
77 spec.Extensions
78 MediaTypeProps
79 }
80 if err := opts.UnmarshalNext(dec, &x); err != nil {
81 return err
82 }
83 m.Extensions = internal.SanitizeExtensions(x.Extensions)
84 m.MediaTypeProps = x.MediaTypeProps
85
86 return nil
87 }
88
89 // MediaTypeProps a struct that allows you to specify content format, more at https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#mediaTypeObject
90 type MediaTypeProps struct {
91 // Schema holds the schema defining the type used for the media type
92 Schema *spec.Schema `json:"schema,omitempty"`
93 // Example of the media type
94 Example interface{} `json:"example,omitempty"`
95 // Examples of the media type. Each example object should match the media type and specific schema if present
96 Examples map[string]*Example `json:"examples,omitempty"`
97 // A map between a property name and its encoding information. The key, being the property name, MUST exist in the schema as a property. The encoding object SHALL only apply to requestBody objects when the media type is multipart or application/x-www-form-urlencoded
98 Encoding map[string]*Encoding `json:"encoding,omitempty"`
99 }
100
101 type mediaTypePropsOmitZero struct {
102 Schema *spec.Schema `json:"schema,omitzero"`
103 Example interface{} `json:"example,omitempty"`
104 Examples map[string]*Example `json:"examples,omitempty"`
105 Encoding map[string]*Encoding `json:"encoding,omitempty"`
106 }