k8s.io/kube-openapi@v0.0.0-20240228011516-70dd3763d340/pkg/spec3/server.go (about) 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 type Server struct { 29 ServerProps 30 spec.VendorExtensible 31 } 32 33 type ServerProps struct { 34 // Description is a short description of the target documentation. CommonMark syntax MAY be used for rich text representation. 35 Description string `json:"description,omitempty"` 36 // URL is the URL for the target documentation. 37 URL string `json:"url"` 38 // Variables contains a map between a variable name and its value. The value is used for substitution in the server's URL templeate 39 Variables map[string]*ServerVariable `json:"variables,omitempty"` 40 } 41 42 // MarshalJSON is a custom marshal function that knows how to encode Responses as JSON 43 func (s *Server) MarshalJSON() ([]byte, error) { 44 if internal.UseOptimizedJSONMarshalingV3 { 45 return internal.DeterministicMarshal(s) 46 } 47 b1, err := json.Marshal(s.ServerProps) 48 if err != nil { 49 return nil, err 50 } 51 b2, err := json.Marshal(s.VendorExtensible) 52 if err != nil { 53 return nil, err 54 } 55 return swag.ConcatJSON(b1, b2), nil 56 } 57 58 func (s *Server) MarshalNextJSON(opts jsonv2.MarshalOptions, enc *jsonv2.Encoder) error { 59 var x struct { 60 ServerProps `json:",inline"` 61 spec.Extensions 62 } 63 x.Extensions = internal.SanitizeExtensions(s.Extensions) 64 x.ServerProps = s.ServerProps 65 return opts.MarshalNext(enc, x) 66 } 67 68 func (s *Server) UnmarshalJSON(data []byte) error { 69 if internal.UseOptimizedJSONUnmarshalingV3 { 70 return jsonv2.Unmarshal(data, s) 71 } 72 73 if err := json.Unmarshal(data, &s.ServerProps); err != nil { 74 return err 75 } 76 if err := json.Unmarshal(data, &s.VendorExtensible); err != nil { 77 return err 78 } 79 return nil 80 } 81 82 func (s *Server) UnmarshalNextJSON(opts jsonv2.UnmarshalOptions, dec *jsonv2.Decoder) error { 83 var x struct { 84 spec.Extensions 85 ServerProps 86 } 87 if err := opts.UnmarshalNext(dec, &x); err != nil { 88 return err 89 } 90 s.Extensions = internal.SanitizeExtensions(x.Extensions) 91 s.ServerProps = x.ServerProps 92 93 return nil 94 } 95 96 type ServerVariable struct { 97 ServerVariableProps 98 spec.VendorExtensible 99 } 100 101 type ServerVariableProps struct { 102 // Enum is an enumeration of string values to be used if the substitution options are from a limited set 103 Enum []string `json:"enum,omitempty"` 104 // Default is the default value to use for substitution, which SHALL be sent if an alternate value is not supplied 105 Default string `json:"default"` 106 // Description is a description for the server variable 107 Description string `json:"description,omitempty"` 108 } 109 110 // MarshalJSON is a custom marshal function that knows how to encode Responses as JSON 111 func (s *ServerVariable) MarshalJSON() ([]byte, error) { 112 if internal.UseOptimizedJSONMarshalingV3 { 113 return internal.DeterministicMarshal(s) 114 } 115 b1, err := json.Marshal(s.ServerVariableProps) 116 if err != nil { 117 return nil, err 118 } 119 b2, err := json.Marshal(s.VendorExtensible) 120 if err != nil { 121 return nil, err 122 } 123 return swag.ConcatJSON(b1, b2), nil 124 } 125 126 func (s *ServerVariable) MarshalNextJSON(opts jsonv2.MarshalOptions, enc *jsonv2.Encoder) error { 127 var x struct { 128 ServerVariableProps `json:",inline"` 129 spec.Extensions 130 } 131 x.Extensions = internal.SanitizeExtensions(s.Extensions) 132 x.ServerVariableProps = s.ServerVariableProps 133 return opts.MarshalNext(enc, x) 134 } 135 136 func (s *ServerVariable) UnmarshalJSON(data []byte) error { 137 if internal.UseOptimizedJSONUnmarshalingV3 { 138 return jsonv2.Unmarshal(data, s) 139 } 140 if err := json.Unmarshal(data, &s.ServerVariableProps); err != nil { 141 return err 142 } 143 if err := json.Unmarshal(data, &s.VendorExtensible); err != nil { 144 return err 145 } 146 return nil 147 } 148 149 func (s *ServerVariable) UnmarshalNextJSON(opts jsonv2.UnmarshalOptions, dec *jsonv2.Decoder) error { 150 var x struct { 151 spec.Extensions 152 ServerVariableProps 153 } 154 if err := opts.UnmarshalNext(dec, &x); err != nil { 155 return err 156 } 157 s.Extensions = internal.SanitizeExtensions(x.Extensions) 158 s.ServerVariableProps = x.ServerVariableProps 159 160 return nil 161 }