Debian Codegraph @

pkg/spec3/media_type.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 // 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
30 //
31 // Note that this struct is actually a thin wrapper around MediaTypeProps to make it referable and extensible
32 type MediaType struct {
33 MediaTypeProps
34 spec.VendorExtensible
35 }
36
37 // MarshalJSON is a custom marshal function that knows how to encode MediaType as JSON
38 func (m *MediaType) MarshalJSON() ([]byte, error) {
39 if internal.UseOptimizedJSONMarshalingV3 {
40 return internal.DeterministicMarshal(m)
41 }
42 b1, err := json.Marshal(m.MediaTypeProps)
43 if err != nil {
44 return nil, err
45 }
46 b2, err := json.Marshal(m.VendorExtensible)
47 if err != nil {
48 return nil, err
49 }
50 return swag.ConcatJSON(b1, b2), nil
51 }
52
53 func (e *MediaType) MarshalJSONTo(enc *jsontext.Encoder) error {
54 var x struct {
55 MediaTypeProps mediaTypePropsOmitZero `json:",inline"`
56 Extensions spec.Extensions `json:",inline"`
57 }
58 x.Extensions = internal.SanitizeExtensions(e.Extensions)
59 x.MediaTypeProps = mediaTypePropsOmitZero(e.MediaTypeProps)
60 return jsonv2.MarshalEncode(enc, x)
61 }
62
63 func (m *MediaType) UnmarshalJSON(data []byte) error {
64 if internal.UseOptimizedJSONUnmarshalingV3 {
65 return jsonv2.Unmarshal(data, m)
66 }
67 if err := json.Unmarshal(data, &m.MediaTypeProps); err != nil {
68 return err
69 }
70 if err := json.Unmarshal(data, &m.VendorExtensible); err != nil {
71 return err
72 }
73 return nil
74 }
75
76 func (m *MediaType) UnmarshalJSONFrom(dec *jsontext.Decoder) error {
77 var x struct {
78 Extensions spec.Extensions `json:",inline"`
79 MediaTypeProps
80 }
81 if err := jsonv2.UnmarshalDecode(dec, &x); err != nil {
82 return err
83 }
84 m.Extensions = internal.SanitizeExtensions(x.Extensions)
85 m.MediaTypeProps = x.MediaTypeProps
86
87 return nil
88 }
89
90 // 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
91 type MediaTypeProps struct {
92 // Schema holds the schema defining the type used for the media type
93 Schema *spec.Schema `json:"schema,omitempty"`
94 // Example of the media type
95 Example interface{} `json:"example,omitempty"`
96 // Examples of the media type. Each example object should match the media type and specific schema if present
97 Examples map[string]*Example `json:"examples,omitempty"`
98 // 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
99 Encoding map[string]*Encoding `json:"encoding,omitempty"`
100 }
101
102 type mediaTypePropsOmitZero struct {
103 Schema *spec.Schema `json:"schema,omitzero"`
104 Example interface{} `json:"example,omitempty"`
105 Examples map[string]*Example `json:"examples,omitempty"`
106 Encoding map[string]*Encoding `json:"encoding,omitempty"`
107 }