Debian Codegraph @

pkg/schemaconv/openapi.go

raw
1 /*
2 Copyright 2022 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 schemaconv
18
19 import (
20 "errors"
21 "path"
22 "strings"
23
24 "k8s.io/kube-openapi/pkg/validation/spec"
25 "sigs.k8s.io/structured-merge-diff/v6/schema"
26 )
27
28 // ToSchemaFromOpenAPI converts a directory of OpenAPI schemas to an smd Schema.
29 // - models: a map from definition name to OpenAPI V3 structural schema for each definition.
30 // Key in map is used to resolve references in the schema.
31 // - preserveUnknownFields: flag indicating whether unknown fields in all schemas should be preserved.
32 // - returns: nil and an error if there is a parse error, or if schema does not satisfy a
33 // required structural schema invariant for conversion. If no error, returns
34 // a new smd schema.
35 //
36 // Schema should be validated as structural before using with this function, or
37 // there may be information lost.
38 func ToSchemaFromOpenAPI(models map[string]*spec.Schema, preserveUnknownFields bool) (*schema.Schema, error) {
39 c := convert{
40 preserveUnknownFields: preserveUnknownFields,
41 output: &schema.Schema{},
42 }
43
44 for name, spec := range models {
45 // Skip/Ignore top-level references
46 if len(spec.Ref.String()) > 0 {
47 continue
48 }
49
50 var a schema.Atom
51
52 // Hard-coded schemas for now as proto_models implementation functions.
53 // https://github.com/kubernetes/kube-openapi/issues/364
54 if name == quantityResource {
55 a = schema.Atom{
56 Scalar: untypedDef.Atom.Scalar,
57 }
58 } else if name == rawExtensionResource {
59 a = untypedDef.Atom
60 } else {
61 c2 := c.push(name, &a)
62 c2.visitSpec(spec)
63 c.pop(c2)
64 }
65
66 c.insertTypeDef(name, a)
67 }
68
69 if len(c.errorMessages) > 0 {
70 return nil, errors.New(strings.Join(c.errorMessages, "\n"))
71 }
72
73 c.addCommonTypes()
74 return c.output, nil
75 }
76
77 func (c *convert) visitSpec(m *spec.Schema) {
78 // Check if this schema opts its descendants into preserve-unknown-fields
79 if p, ok := m.Extensions["x-kubernetes-preserve-unknown-fields"]; ok && p == true {
80 c.preserveUnknownFields = true
81 }
82 a := c.top()
83 *a = c.parseSchema(m)
84 }
85
86 func (c *convert) parseSchema(m *spec.Schema) schema.Atom {
87 // k8s-generated OpenAPI specs have historically used only one value for
88 // type and starting with OpenAPIV3 it is only allowed to be
89 // a single string.
90 typ := ""
91 if len(m.Type) > 0 {
92 typ = m.Type[0]
93 }
94
95 // Structural Schemas produced by kubernetes follow very specific rules which
96 // we can use to infer the SMD type:
97 switch typ {
98 case "":
99 // According to Swagger docs:
100 // https://swagger.io/docs/specification/data-models/data-types/#any
101 //
102 // If no type is specified, it is equivalent to accepting any type.
103 return schema.Atom{
104 Scalar: ptr(schema.Scalar("untyped")),
105 List: c.parseList(m),
106 Map: c.parseObject(m),
107 }
108
109 case "object":
110 return schema.Atom{
111 Map: c.parseObject(m),
112 }
113 case "array":
114 return schema.Atom{
115 List: c.parseList(m),
116 }
117 case "integer", "boolean", "number", "string":
118 return convertPrimitive(typ, m.Format)
119 default:
120 c.reportError("unrecognized type: '%v'", typ)
121 return schema.Atom{
122 Scalar: ptr(schema.Scalar("untyped")),
123 }
124 }
125 }
126
127 func (c *convert) makeOpenAPIRef(specSchema *spec.Schema) schema.TypeRef {
128 refString := specSchema.Ref.String()
129
130 // Special-case handling for $ref stored inside a single-element allOf
131 if len(refString) == 0 && len(specSchema.AllOf) == 1 && len(specSchema.AllOf[0].Ref.String()) > 0 {
132 refString = specSchema.AllOf[0].Ref.String()
133 }
134
135 if _, n := path.Split(refString); len(n) > 0 {
136 //!TODO: Refactor the field ElementRelationship override
137 // we can generate the types with overrides ahead of time rather than
138 // requiring the hacky runtime support
139 // (could just create a normalized key struct containing all customizations
140 // to deduplicate)
141 mapRelationship, err := getMapElementRelationship(specSchema.Extensions)
142 if err != nil {
143 c.reportError("%v", err)
144 }
145
146 if len(mapRelationship) > 0 {
147 return schema.TypeRef{
148 NamedType: &n,
149 ElementRelationship: &mapRelationship,
150 Nullable: specSchema.Nullable,
151 }
152 }
153
154 return schema.TypeRef{
155 NamedType: &n,
156 Nullable: specSchema.Nullable,
157 }
158
159 }
160 var inlined schema.Atom
161
162 // compute the type inline
163 c2 := c.push("inlined in "+c.currentName, &inlined)
164 c2.preserveUnknownFields = c.preserveUnknownFields
165 c2.visitSpec(specSchema)
166 c.pop(c2)
167
168 return schema.TypeRef{
169 Inlined: inlined,
170 Nullable: specSchema.Nullable,
171 }
172 }
173
174 func (c *convert) parseObject(s *spec.Schema) *schema.Map {
175 var fields []schema.StructField
176 for name, member := range s.Properties {
177 fields = append(fields, schema.StructField{
178 Name: name,
179 Type: c.makeOpenAPIRef(&member),
180 Default: member.Default,
181 })
182 }
183
184 // AdditionalProperties informs the schema of any "unknown" keys
185 // Unknown keys are enforced by the ElementType field.
186 elementType := func() schema.TypeRef {
187 if s.AdditionalProperties == nil {
188 // According to openAPI spec, an object without properties and without
189 // additionalProperties is assumed to be a free-form object.
190 if c.preserveUnknownFields || len(s.Properties) == 0 {
191 return schema.TypeRef{
192 NamedType: &deducedName,
193 }
194 }
195
196 // If properties are specified, do not implicitly allow unknown
197 // fields
198 return schema.TypeRef{}
199 } else if s.AdditionalProperties.Schema != nil {
200 // Unknown fields use the referred schema
201 return c.makeOpenAPIRef(s.AdditionalProperties.Schema)
202
203 } else if s.AdditionalProperties.Allows {
204 // A boolean instead of a schema was provided. Deduce the
205 // type from the value provided at runtime.
206 return schema.TypeRef{
207 NamedType: &deducedName,
208 }
209 } else {
210 // Additional Properties are explicitly disallowed by the user.
211 // Ensure element type is empty.
212 return schema.TypeRef{}
213 }
214 }()
215
216 relationship, err := getMapElementRelationship(s.Extensions)
217 if err != nil {
218 c.reportError("%v", err)
219 }
220
221 return &schema.Map{
222 Fields: fields,
223 ElementRelationship: relationship,
224 ElementType: elementType,
225 }
226 }
227
228 func (c *convert) parseList(s *spec.Schema) *schema.List {
229 relationship, mapKeys, err := getListElementRelationship(s.Extensions)
230 if err != nil {
231 c.reportError("%v", err)
232 }
233 elementType := func() schema.TypeRef {
234 if s.Items != nil {
235 if s.Items.Schema == nil || s.Items.Len() != 1 {
236 c.reportError("structural schema arrays must have exactly one member subtype")
237 return schema.TypeRef{
238 NamedType: &deducedName,
239 }
240 }
241
242 subSchema := s.Items.Schema
243 if subSchema == nil {
244 subSchema = &s.Items.Schemas[0]
245 }
246 return c.makeOpenAPIRef(subSchema)
247 } else if len(s.Type) > 0 && len(s.Type[0]) > 0 {
248 c.reportError("`items` must be specified on arrays")
249 }
250
251 // A list with no items specified is treated as "untyped".
252 return schema.TypeRef{
253 NamedType: &untypedName,
254 }
255
256 }()
257
258 return &schema.List{
259 ElementRelationship: relationship,
260 Keys: mapKeys,
261 ElementType: elementType,
262 }
263 }