server.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. Copyright 2021 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package spec3
  14. import (
  15. "encoding/json"
  16. "github.com/go-openapi/swag"
  17. "k8s.io/kube-openapi/pkg/internal"
  18. jsonv2 "k8s.io/kube-openapi/pkg/internal/third_party/go-json-experiment/json"
  19. "k8s.io/kube-openapi/pkg/validation/spec"
  20. )
  21. type Server struct {
  22. ServerProps
  23. spec.VendorExtensible
  24. }
  25. type ServerProps struct {
  26. // Description is a short description of the target documentation. CommonMark syntax MAY be used for rich text representation.
  27. Description string `json:"description,omitempty"`
  28. // URL is the URL for the target documentation.
  29. URL string `json:"url"`
  30. // Variables contains a map between a variable name and its value. The value is used for substitution in the server's URL templeate
  31. Variables map[string]*ServerVariable `json:"variables,omitempty"`
  32. }
  33. // MarshalJSON is a custom marshal function that knows how to encode Responses as JSON
  34. func (s *Server) MarshalJSON() ([]byte, error) {
  35. if internal.UseOptimizedJSONMarshalingV3 {
  36. return internal.DeterministicMarshal(s)
  37. }
  38. b1, err := json.Marshal(s.ServerProps)
  39. if err != nil {
  40. return nil, err
  41. }
  42. b2, err := json.Marshal(s.VendorExtensible)
  43. if err != nil {
  44. return nil, err
  45. }
  46. return swag.ConcatJSON(b1, b2), nil
  47. }
  48. func (s *Server) MarshalNextJSON(opts jsonv2.MarshalOptions, enc *jsonv2.Encoder) error {
  49. var x struct {
  50. ServerProps `json:",inline"`
  51. spec.Extensions
  52. }
  53. x.Extensions = internal.SanitizeExtensions(s.Extensions)
  54. x.ServerProps = s.ServerProps
  55. return opts.MarshalNext(enc, x)
  56. }
  57. func (s *Server) UnmarshalJSON(data []byte) error {
  58. if internal.UseOptimizedJSONUnmarshalingV3 {
  59. return jsonv2.Unmarshal(data, s)
  60. }
  61. if err := json.Unmarshal(data, &s.ServerProps); err != nil {
  62. return err
  63. }
  64. if err := json.Unmarshal(data, &s.VendorExtensible); err != nil {
  65. return err
  66. }
  67. return nil
  68. }
  69. func (s *Server) UnmarshalNextJSON(opts jsonv2.UnmarshalOptions, dec *jsonv2.Decoder) error {
  70. var x struct {
  71. spec.Extensions
  72. ServerProps
  73. }
  74. if err := opts.UnmarshalNext(dec, &x); err != nil {
  75. return err
  76. }
  77. s.Extensions = internal.SanitizeExtensions(x.Extensions)
  78. s.ServerProps = x.ServerProps
  79. return nil
  80. }
  81. type ServerVariable struct {
  82. ServerVariableProps
  83. spec.VendorExtensible
  84. }
  85. type ServerVariableProps struct {
  86. // Enum is an enumeration of string values to be used if the substitution options are from a limited set
  87. Enum []string `json:"enum,omitempty"`
  88. // Default is the default value to use for substitution, which SHALL be sent if an alternate value is not supplied
  89. Default string `json:"default"`
  90. // Description is a description for the server variable
  91. Description string `json:"description,omitempty"`
  92. }
  93. // MarshalJSON is a custom marshal function that knows how to encode Responses as JSON
  94. func (s *ServerVariable) MarshalJSON() ([]byte, error) {
  95. if internal.UseOptimizedJSONMarshalingV3 {
  96. return internal.DeterministicMarshal(s)
  97. }
  98. b1, err := json.Marshal(s.ServerVariableProps)
  99. if err != nil {
  100. return nil, err
  101. }
  102. b2, err := json.Marshal(s.VendorExtensible)
  103. if err != nil {
  104. return nil, err
  105. }
  106. return swag.ConcatJSON(b1, b2), nil
  107. }
  108. func (s *ServerVariable) MarshalNextJSON(opts jsonv2.MarshalOptions, enc *jsonv2.Encoder) error {
  109. var x struct {
  110. ServerVariableProps `json:",inline"`
  111. spec.Extensions
  112. }
  113. x.Extensions = internal.SanitizeExtensions(s.Extensions)
  114. x.ServerVariableProps = s.ServerVariableProps
  115. return opts.MarshalNext(enc, x)
  116. }
  117. func (s *ServerVariable) UnmarshalJSON(data []byte) error {
  118. if internal.UseOptimizedJSONUnmarshalingV3 {
  119. return jsonv2.Unmarshal(data, s)
  120. }
  121. if err := json.Unmarshal(data, &s.ServerVariableProps); err != nil {
  122. return err
  123. }
  124. if err := json.Unmarshal(data, &s.VendorExtensible); err != nil {
  125. return err
  126. }
  127. return nil
  128. }
  129. func (s *ServerVariable) UnmarshalNextJSON(opts jsonv2.UnmarshalOptions, dec *jsonv2.Decoder) error {
  130. var x struct {
  131. spec.Extensions
  132. ServerVariableProps
  133. }
  134. if err := opts.UnmarshalNext(dec, &x); err != nil {
  135. return err
  136. }
  137. s.Extensions = internal.SanitizeExtensions(x.Extensions)
  138. s.ServerVariableProps = x.ServerVariableProps
  139. return nil
  140. }