spec.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. "k8s.io/kube-openapi/pkg/internal"
  17. jsonv2 "k8s.io/kube-openapi/pkg/internal/third_party/go-json-experiment/json"
  18. "k8s.io/kube-openapi/pkg/validation/spec"
  19. )
  20. // OpenAPI is an object that describes an API and conforms to the OpenAPI Specification.
  21. type OpenAPI struct {
  22. // Version represents the semantic version number of the OpenAPI Specification that this document uses
  23. Version string `json:"openapi"`
  24. // Info provides metadata about the API
  25. Info *spec.Info `json:"info"`
  26. // Paths holds the available target and operations for the API
  27. Paths *Paths `json:"paths,omitempty"`
  28. // Servers is an array of Server objects which provide connectivity information to a target server
  29. Servers []*Server `json:"servers,omitempty"`
  30. // Components hold various schemas for the specification
  31. Components *Components `json:"components,omitempty"`
  32. // SecurityRequirement holds a declaration of which security mechanisms can be used across the API
  33. SecurityRequirement []map[string][]string `json:"security,omitempty"`
  34. // ExternalDocs holds additional external documentation
  35. ExternalDocs *ExternalDocumentation `json:"externalDocs,omitempty"`
  36. }
  37. func (o *OpenAPI) UnmarshalJSON(data []byte) error {
  38. type OpenAPIWithNoFunctions OpenAPI
  39. p := (*OpenAPIWithNoFunctions)(o)
  40. if internal.UseOptimizedJSONUnmarshalingV3 {
  41. return jsonv2.Unmarshal(data, &p)
  42. }
  43. return json.Unmarshal(data, &p)
  44. }
  45. func (o *OpenAPI) MarshalJSON() ([]byte, error) {
  46. if internal.UseOptimizedJSONMarshalingV3 {
  47. return internal.DeterministicMarshal(o)
  48. }
  49. type OpenAPIWithNoFunctions OpenAPI
  50. p := (*OpenAPIWithNoFunctions)(o)
  51. return json.Marshal(&p)
  52. }
  53. func (o *OpenAPI) MarshalNextJSON(opts jsonv2.MarshalOptions, enc *jsonv2.Encoder) error {
  54. type OpenAPIOmitZero struct {
  55. Version string `json:"openapi"`
  56. Info *spec.Info `json:"info"`
  57. Paths *Paths `json:"paths,omitzero"`
  58. Servers []*Server `json:"servers,omitempty"`
  59. Components *Components `json:"components,omitzero"`
  60. SecurityRequirement []map[string][]string `json:"security,omitempty"`
  61. ExternalDocs *ExternalDocumentation `json:"externalDocs,omitzero"`
  62. }
  63. x := (*OpenAPIOmitZero)(o)
  64. return opts.MarshalNext(enc, x)
  65. }