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