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