external_documentation.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 ExternalDocumentation struct {
  22. ExternalDocumentationProps
  23. spec.VendorExtensible
  24. }
  25. type ExternalDocumentationProps 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. }
  31. // MarshalJSON is a custom marshal function that knows how to encode Responses as JSON
  32. func (e *ExternalDocumentation) MarshalJSON() ([]byte, error) {
  33. if internal.UseOptimizedJSONMarshalingV3 {
  34. return internal.DeterministicMarshal(e)
  35. }
  36. b1, err := json.Marshal(e.ExternalDocumentationProps)
  37. if err != nil {
  38. return nil, err
  39. }
  40. b2, err := json.Marshal(e.VendorExtensible)
  41. if err != nil {
  42. return nil, err
  43. }
  44. return swag.ConcatJSON(b1, b2), nil
  45. }
  46. func (e *ExternalDocumentation) MarshalNextJSON(opts jsonv2.MarshalOptions, enc *jsonv2.Encoder) error {
  47. var x struct {
  48. ExternalDocumentationProps `json:",inline"`
  49. spec.Extensions
  50. }
  51. x.Extensions = internal.SanitizeExtensions(e.Extensions)
  52. x.ExternalDocumentationProps = e.ExternalDocumentationProps
  53. return opts.MarshalNext(enc, x)
  54. }
  55. func (e *ExternalDocumentation) UnmarshalJSON(data []byte) error {
  56. if internal.UseOptimizedJSONUnmarshalingV3 {
  57. return jsonv2.Unmarshal(data, e)
  58. }
  59. if err := json.Unmarshal(data, &e.ExternalDocumentationProps); err != nil {
  60. return err
  61. }
  62. if err := json.Unmarshal(data, &e.VendorExtensible); err != nil {
  63. return err
  64. }
  65. return nil
  66. }
  67. func (e *ExternalDocumentation) UnmarshalNextJSON(opts jsonv2.UnmarshalOptions, dec *jsonv2.Decoder) error {
  68. var x struct {
  69. spec.Extensions
  70. ExternalDocumentationProps
  71. }
  72. if err := opts.UnmarshalNext(dec, &x); err != nil {
  73. return err
  74. }
  75. e.Extensions = internal.SanitizeExtensions(x.Extensions)
  76. e.ExternalDocumentationProps = x.ExternalDocumentationProps
  77. return nil
  78. }