example.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. // Example https://swagger.io/specification/#example-object
  22. type Example struct {
  23. spec.Refable
  24. ExampleProps
  25. spec.VendorExtensible
  26. }
  27. // MarshalJSON is a custom marshal function that knows how to encode RequestBody as JSON
  28. func (e *Example) MarshalJSON() ([]byte, error) {
  29. if internal.UseOptimizedJSONMarshalingV3 {
  30. return internal.DeterministicMarshal(e)
  31. }
  32. b1, err := json.Marshal(e.Refable)
  33. if err != nil {
  34. return nil, err
  35. }
  36. b2, err := json.Marshal(e.ExampleProps)
  37. if err != nil {
  38. return nil, err
  39. }
  40. b3, err := json.Marshal(e.VendorExtensible)
  41. if err != nil {
  42. return nil, err
  43. }
  44. return swag.ConcatJSON(b1, b2, b3), nil
  45. }
  46. func (e *Example) MarshalNextJSON(opts jsonv2.MarshalOptions, enc *jsonv2.Encoder) error {
  47. var x struct {
  48. Ref string `json:"$ref,omitempty"`
  49. ExampleProps `json:",inline"`
  50. spec.Extensions
  51. }
  52. x.Ref = e.Refable.Ref.String()
  53. x.Extensions = internal.SanitizeExtensions(e.Extensions)
  54. x.ExampleProps = e.ExampleProps
  55. return opts.MarshalNext(enc, x)
  56. }
  57. func (e *Example) UnmarshalJSON(data []byte) error {
  58. if internal.UseOptimizedJSONUnmarshalingV3 {
  59. return jsonv2.Unmarshal(data, e)
  60. }
  61. if err := json.Unmarshal(data, &e.Refable); err != nil {
  62. return err
  63. }
  64. if err := json.Unmarshal(data, &e.ExampleProps); err != nil {
  65. return err
  66. }
  67. if err := json.Unmarshal(data, &e.VendorExtensible); err != nil {
  68. return err
  69. }
  70. return nil
  71. }
  72. func (e *Example) UnmarshalNextJSON(opts jsonv2.UnmarshalOptions, dec *jsonv2.Decoder) error {
  73. var x struct {
  74. spec.Extensions
  75. ExampleProps
  76. }
  77. if err := opts.UnmarshalNext(dec, &x); err != nil {
  78. return err
  79. }
  80. if err := internal.JSONRefFromMap(&e.Ref.Ref, x.Extensions); err != nil {
  81. return err
  82. }
  83. e.Extensions = internal.SanitizeExtensions(x.Extensions)
  84. e.ExampleProps = x.ExampleProps
  85. return nil
  86. }
  87. type ExampleProps struct {
  88. // Summary holds a short description of the example
  89. Summary string `json:"summary,omitempty"`
  90. // Description holds a long description of the example
  91. Description string `json:"description,omitempty"`
  92. // Embedded literal example.
  93. Value interface{} `json:"value,omitempty"`
  94. // A URL that points to the literal example. This provides the capability to reference examples that cannot easily be included in JSON or YAML documents.
  95. ExternalValue string `json:"externalValue,omitempty"`
  96. }