| 1 |
From: "Dr. Tobias Quathamer" <[email protected]> |
| 2 |
Date: Sat, 14 Feb 2026 22:59:04 +0100 |
| 3 |
Subject: Remove test which relies on packagestest |
| 4 |
|
| 5 |
The "packagestest" is deprecated and has been removed from |
| 6 |
golang-golang-x-tools. The testfile below is removed to |
| 7 |
fix the FTBFS with the latest upload of golang-golang-x-tools. |
| 8 |
--- |
| 9 |
pkg/generators/openapi_test.go | 3104 ---------------------------------------- |
| 10 |
1 file changed, 3104 deletions(-) |
| 11 |
delete mode 100644 pkg/generators/openapi_test.go |
| 12 |
|
| 13 |
diff --git a/pkg/generators/openapi_test.go b/pkg/generators/openapi_test.go |
| 14 |
deleted file mode 100644 |
| 15 |
index 55be4c5..0000000 |
| 16 |
--- a/pkg/generators/openapi_test.go |
| 17 |
+++ /dev/null |
| 18 |
@@ -1,3104 +0,0 @@ |
| 19 |
-/* |
| 20 |
-Copyright 2016 The Kubernetes Authors. |
| 21 |
- |
| 22 |
-Licensed under the Apache License, Version 2.0 (the "License"); |
| 23 |
-you may not use this file except in compliance with the License. |
| 24 |
-You may obtain a copy of the License at |
| 25 |
- |
| 26 |
- http://www.apache.org/licenses/LICENSE-2.0 |
| 27 |
- |
| 28 |
-Unless required by applicable law or agreed to in writing, software |
| 29 |
-distributed under the License is distributed on an "AS IS" BASIS, |
| 30 |
-WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 31 |
-See the License for the specific language governing permissions and |
| 32 |
-limitations under the License. |
| 33 |
-*/ |
| 34 |
- |
| 35 |
-package generators |
| 36 |
- |
| 37 |
-import ( |
| 38 |
- "bytes" |
| 39 |
- "fmt" |
| 40 |
- "go/format" |
| 41 |
- "path" |
| 42 |
- "strings" |
| 43 |
- "testing" |
| 44 |
- |
| 45 |
- "github.com/google/go-cmp/cmp" |
| 46 |
- "golang.org/x/tools/go/packages" |
| 47 |
- "golang.org/x/tools/go/packages/packagestest" |
| 48 |
- |
| 49 |
- "k8s.io/gengo/v2/generator" |
| 50 |
- "k8s.io/gengo/v2/namer" |
| 51 |
- "k8s.io/gengo/v2/parser" |
| 52 |
- "k8s.io/gengo/v2/types" |
| 53 |
-) |
| 54 |
- |
| 55 |
-func construct(t *testing.T, cfg *packages.Config, nameSystems namer.NameSystems, defaultSystem string, pkg string) *generator.Context { |
| 56 |
- p := parser.New() |
| 57 |
- if err := p.LoadPackagesWithConfigForTesting(cfg, pkg); err != nil { |
| 58 |
- t.Fatalf("failed to load package: %v", err) |
| 59 |
- } |
| 60 |
- c, err := generator.NewContext(p, nameSystems, defaultSystem) |
| 61 |
- if err != nil { |
| 62 |
- t.Fatalf("failed to make a context: %v", err) |
| 63 |
- } |
| 64 |
- return c |
| 65 |
-} |
| 66 |
- |
| 67 |
-func testOpenAPITypeWriter(t *testing.T, cfg *packages.Config) (error, error, *bytes.Buffer, *bytes.Buffer, []string) { |
| 68 |
- pkgBase := "example.com/base" |
| 69 |
- // `path` vs. `filepath` because packages use '/' |
| 70 |
- inputPkg := path.Join(pkgBase, "foo") |
| 71 |
- outputPkg := path.Join(pkgBase, "output") |
| 72 |
- imports := generator.NewImportTrackerForPackage(outputPkg) |
| 73 |
- rawNamer := namer.NewRawNamer(outputPkg, imports) |
| 74 |
- namers := namer.NameSystems{ |
| 75 |
- "raw": rawNamer, |
| 76 |
- "private": &namer.NameStrategy{ |
| 77 |
- Join: func(pre string, in []string, post string) string { |
| 78 |
- return strings.Join(in, "_") |
| 79 |
- }, |
| 80 |
- PrependPackageNames: 4, // enough to fully qualify from k8s.io/api/... |
| 81 |
- }, |
| 82 |
- } |
| 83 |
- context := construct(t, cfg, namers, "raw", inputPkg) |
| 84 |
- universe := context.Universe |
| 85 |
- blahT := universe.Type(types.Name{Package: inputPkg, Name: "Blah"}) |
| 86 |
- |
| 87 |
- callBuffer := &bytes.Buffer{} |
| 88 |
- callSW := generator.NewSnippetWriter(callBuffer, context, "$", "$") |
| 89 |
- callError := newOpenAPITypeWriter(callSW, context).generateCall(blahT) |
| 90 |
- |
| 91 |
- funcBuffer := &bytes.Buffer{} |
| 92 |
- funcSW := generator.NewSnippetWriter(funcBuffer, context, "$", "$") |
| 93 |
- funcError := newOpenAPITypeWriter(funcSW, context).generate(blahT) |
| 94 |
- |
| 95 |
- return callError, funcError, callBuffer, funcBuffer, imports.ImportLines() |
| 96 |
-} |
| 97 |
- |
| 98 |
-// NOTE: the usual order of arguments for an assertion would be want, got, but |
| 99 |
-// this helper function flips that in favor of callsite readability. |
| 100 |
-func assertEqual(t *testing.T, got, want string) { |
| 101 |
- t.Helper() |
| 102 |
- want = strings.TrimSpace(want) |
| 103 |
- got = strings.TrimSpace(got) |
| 104 |
- if !cmp.Equal(want, got) { |
| 105 |
- t.Errorf("Wrong result:\n%s", cmp.Diff(want, got)) |
| 106 |
- } |
| 107 |
-} |
| 108 |
- |
| 109 |
-func TestSimple(t *testing.T) { |
| 110 |
- inputFile := ` |
| 111 |
- package foo |
| 112 |
- |
| 113 |
- // Blah is a test. |
| 114 |
- // +k8s:openapi-gen=true |
| 115 |
- // +k8s:openapi-gen=x-kubernetes-type-tag:type_test |
| 116 |
- type Blah struct { |
| 117 |
- // A simple string |
| 118 |
- String string |
| 119 |
- // A simple int |
| 120 |
- Int int ` + "`" + `json:",omitempty"` + "`" + ` |
| 121 |
- // An int considered string simple int |
| 122 |
- IntString int ` + "`" + `json:",string"` + "`" + ` |
| 123 |
- // A simple int64 |
| 124 |
- Int64 int64 |
| 125 |
- // A simple int32 |
| 126 |
- Int32 int32 |
| 127 |
- // A simple int16 |
| 128 |
- Int16 int16 |
| 129 |
- // A simple int8 |
| 130 |
- Int8 int8 |
| 131 |
- // A simple int |
| 132 |
- Uint uint |
| 133 |
- // A simple int64 |
| 134 |
- Uint64 uint64 |
| 135 |
- // A simple int32 |
| 136 |
- Uint32 uint32 |
| 137 |
- // A simple int16 |
| 138 |
- Uint16 uint16 |
| 139 |
- // A simple int8 |
| 140 |
- Uint8 uint8 |
| 141 |
- // A simple byte |
| 142 |
- Byte byte |
| 143 |
- // A simple boolean |
| 144 |
- Bool bool |
| 145 |
- // A simple float64 |
| 146 |
- Float64 float64 |
| 147 |
- // A simple float32 |
| 148 |
- Float32 float32 |
| 149 |
- // a base64 encoded characters |
| 150 |
- ByteArray []byte |
| 151 |
- // a member with an extension |
| 152 |
- // +k8s:openapi-gen=x-kubernetes-member-tag:member_test |
| 153 |
- WithExtension string |
| 154 |
- // a member with struct tag as extension |
| 155 |
- // +patchStrategy=merge |
| 156 |
- // +patchMergeKey=pmk |
| 157 |
- WithStructTagExtension string ` + "`" + `patchStrategy:"merge" patchMergeKey:"pmk"` + "`" + ` |
| 158 |
- // a member with a list type |
| 159 |
- // +listType=atomic |
| 160 |
- // +default=["foo", "bar"] |
| 161 |
- WithListType []string |
| 162 |
- // a member with a map type |
| 163 |
- // +listType=atomic |
| 164 |
- // +default={"foo": "bar", "fizz": "buzz"} |
| 165 |
- Map map[string]string |
| 166 |
- // a member with a string pointer |
| 167 |
- // +default="foo" |
| 168 |
- StringPointer *string |
| 169 |
- // an int member with a default |
| 170 |
- // +default=1 |
| 171 |
- OmittedInt int ` + "`" + `json:"omitted,omitempty"` + "`" + ` |
| 172 |
- // a field with an invalid escape sequence in comment |
| 173 |
- // ex) regexp:^.*\.yaml$ |
| 174 |
- InvalidEscapeSequenceInComment string |
| 175 |
- }` |
| 176 |
- |
| 177 |
- packagestest.TestAll(t, func(t *testing.T, x packagestest.Exporter) { |
| 178 |
- e := packagestest.Export(t, x, []packagestest.Module{{ |
| 179 |
- Name: "example.com/base/foo", |
| 180 |
- Files: map[string]interface{}{ |
| 181 |
- "foo.go": inputFile, |
| 182 |
- }, |
| 183 |
- }}) |
| 184 |
- defer e.Cleanup() |
| 185 |
- |
| 186 |
- callErr, funcErr, callBuffer, funcBuffer, _ := testOpenAPITypeWriter(t, e.Config) |
| 187 |
- if callErr != nil { |
| 188 |
- t.Fatal(callErr) |
| 189 |
- } |
| 190 |
- if funcErr != nil { |
| 191 |
- t.Fatal(funcErr) |
| 192 |
- } |
| 193 |
- assertEqual(t, callBuffer.String(), |
| 194 |
- `"example.com/base/foo.Blah": schema_examplecom_base_foo_Blah(ref),`) |
| 195 |
- |
| 196 |
- assertEqual(t, funcBuffer.String(), |
| 197 |
- `func schema_examplecom_base_foo_Blah(ref common.ReferenceCallback) common.OpenAPIDefinition { |
| 198 |
-return common.OpenAPIDefinition{ |
| 199 |
-Schema: spec.Schema{ |
| 200 |
-SchemaProps: spec.SchemaProps{ |
| 201 |
-Description: "Blah is a test.", |
| 202 |
-Type: []string{"object"}, |
| 203 |
-Properties: map[string]spec.Schema{ |
| 204 |
-"String": { |
| 205 |
-SchemaProps: spec.SchemaProps{ |
| 206 |
-Description: "A simple string", |
| 207 |
-Default: "", |
| 208 |
-Type: []string{"string"}, |
| 209 |
-Format: "", |
| 210 |
-}, |
| 211 |
-}, |
| 212 |
-"Int64": { |
| 213 |
-SchemaProps: spec.SchemaProps{ |
| 214 |
-Description: "A simple int64", |
| 215 |
-Default: 0, |
| 216 |
-Type: []string{"integer"}, |
| 217 |
-Format: "int64", |
| 218 |
-}, |
| 219 |
-}, |
| 220 |
-"Int32": { |
| 221 |
-SchemaProps: spec.SchemaProps{ |
| 222 |
-Description: "A simple int32", |
| 223 |
-Default: 0, |
| 224 |
-Type: []string{"integer"}, |
| 225 |
-Format: "int32", |
| 226 |
-}, |
| 227 |
-}, |
| 228 |
-"Int16": { |
| 229 |
-SchemaProps: spec.SchemaProps{ |
| 230 |
-Description: "A simple int16", |
| 231 |
-Default: 0, |
| 232 |
-Type: []string{"integer"}, |
| 233 |
-Format: "int32", |
| 234 |
-}, |
| 235 |
-}, |
| 236 |
-"Int8": { |
| 237 |
-SchemaProps: spec.SchemaProps{ |
| 238 |
-Description: "A simple int8", |
| 239 |
-Default: 0, |
| 240 |
-Type: []string{"integer"}, |
| 241 |
-Format: "byte", |
| 242 |
-}, |
| 243 |
-}, |
| 244 |
-"Uint": { |
| 245 |
-SchemaProps: spec.SchemaProps{ |
| 246 |
-Description: "A simple int", |
| 247 |
-Default: 0, |
| 248 |
-Type: []string{"integer"}, |
| 249 |
-Format: "int32", |
| 250 |
-}, |
| 251 |
-}, |
| 252 |
-"Uint64": { |
| 253 |
-SchemaProps: spec.SchemaProps{ |
| 254 |
-Description: "A simple int64", |
| 255 |
-Default: 0, |
| 256 |
-Type: []string{"integer"}, |
| 257 |
-Format: "int64", |
| 258 |
-}, |
| 259 |
-}, |
| 260 |
-"Uint32": { |
| 261 |
-SchemaProps: spec.SchemaProps{ |
| 262 |
-Description: "A simple int32", |
| 263 |
-Default: 0, |
| 264 |
-Type: []string{"integer"}, |
| 265 |
-Format: "int64", |
| 266 |
-}, |
| 267 |
-}, |
| 268 |
-"Uint16": { |
| 269 |
-SchemaProps: spec.SchemaProps{ |
| 270 |
-Description: "A simple int16", |
| 271 |
-Default: 0, |
| 272 |
-Type: []string{"integer"}, |
| 273 |
-Format: "int32", |
| 274 |
-}, |
| 275 |
-}, |
| 276 |
-"Uint8": { |
| 277 |
-SchemaProps: spec.SchemaProps{ |
| 278 |
-Description: "A simple int8", |
| 279 |
-Default: 0, |
| 280 |
-Type: []string{"integer"}, |
| 281 |
-Format: "byte", |
| 282 |
-}, |
| 283 |
-}, |
| 284 |
-"Byte": { |
| 285 |
-SchemaProps: spec.SchemaProps{ |
| 286 |
-Description: "A simple byte", |
| 287 |
-Default: 0, |
| 288 |
-Type: []string{"integer"}, |
| 289 |
-Format: "byte", |
| 290 |
-}, |
| 291 |
-}, |
| 292 |
-"Bool": { |
| 293 |
-SchemaProps: spec.SchemaProps{ |
| 294 |
-Description: "A simple boolean", |
| 295 |
-Default: false, |
| 296 |
-Type: []string{"boolean"}, |
| 297 |
-Format: "", |
| 298 |
-}, |
| 299 |
-}, |
| 300 |
-"Float64": { |
| 301 |
-SchemaProps: spec.SchemaProps{ |
| 302 |
-Description: "A simple float64", |
| 303 |
-Default: 0, |
| 304 |
-Type: []string{"number"}, |
| 305 |
-Format: "double", |
| 306 |
-}, |
| 307 |
-}, |
| 308 |
-"Float32": { |
| 309 |
-SchemaProps: spec.SchemaProps{ |
| 310 |
-Description: "A simple float32", |
| 311 |
-Default: 0, |
| 312 |
-Type: []string{"number"}, |
| 313 |
-Format: "float", |
| 314 |
-}, |
| 315 |
-}, |
| 316 |
-"ByteArray": { |
| 317 |
-SchemaProps: spec.SchemaProps{ |
| 318 |
-Description: "a base64 encoded characters", |
| 319 |
-Type: []string{"string"}, |
| 320 |
-Format: "byte", |
| 321 |
-}, |
| 322 |
-}, |
| 323 |
-"WithExtension": { |
| 324 |
-VendorExtensible: spec.VendorExtensible{ |
| 325 |
-Extensions: spec.Extensions{ |
| 326 |
-"x-kubernetes-member-tag": "member_test", |
| 327 |
-}, |
| 328 |
-}, |
| 329 |
-SchemaProps: spec.SchemaProps{ |
| 330 |
-Description: "a member with an extension", |
| 331 |
-Default: "", |
| 332 |
-Type: []string{"string"}, |
| 333 |
-Format: "", |
| 334 |
-}, |
| 335 |
-}, |
| 336 |
-"WithStructTagExtension": { |
| 337 |
-VendorExtensible: spec.VendorExtensible{ |
| 338 |
-Extensions: spec.Extensions{ |
| 339 |
-"x-kubernetes-patch-merge-key": "pmk", |
| 340 |
-"x-kubernetes-patch-strategy": "merge", |
| 341 |
-}, |
| 342 |
-}, |
| 343 |
-SchemaProps: spec.SchemaProps{ |
| 344 |
-Description: "a member with struct tag as extension", |
| 345 |
-Default: "", |
| 346 |
-Type: []string{"string"}, |
| 347 |
-Format: "", |
| 348 |
-}, |
| 349 |
-}, |
| 350 |
-"WithListType": { |
| 351 |
-VendorExtensible: spec.VendorExtensible{ |
| 352 |
-Extensions: spec.Extensions{ |
| 353 |
-"x-kubernetes-list-type": "atomic", |
| 354 |
-}, |
| 355 |
-}, |
| 356 |
-SchemaProps: spec.SchemaProps{ |
| 357 |
-Description: "a member with a list type", |
| 358 |
-Default: []interface {}{"foo", "bar"}, |
| 359 |
-Type: []string{"array"}, |
| 360 |
-Items: &spec.SchemaOrArray{ |
| 361 |
-Schema: &spec.Schema{ |
| 362 |
-SchemaProps: spec.SchemaProps{ |
| 363 |
-Default: "", |
| 364 |
-Type: []string{"string"}, |
| 365 |
-Format: "", |
| 366 |
-}, |
| 367 |
-}, |
| 368 |
-}, |
| 369 |
-}, |
| 370 |
-}, |
| 371 |
-"Map": { |
| 372 |
-VendorExtensible: spec.VendorExtensible{ |
| 373 |
-Extensions: spec.Extensions{ |
| 374 |
-"x-kubernetes-list-type": "atomic", |
| 375 |
-}, |
| 376 |
-}, |
| 377 |
-SchemaProps: spec.SchemaProps{ |
| 378 |
-Description: "a member with a map type", |
| 379 |
-Default: map[string]interface {}{"fizz":"buzz", "foo":"bar"}, |
| 380 |
-Type: []string{"object"}, |
| 381 |
-AdditionalProperties: &spec.SchemaOrBool{ |
| 382 |
-Allows: true, |
| 383 |
-Schema: &spec.Schema{ |
| 384 |
-SchemaProps: spec.SchemaProps{ |
| 385 |
-Default: "", |
| 386 |
-Type: []string{"string"}, |
| 387 |
-Format: "", |
| 388 |
-}, |
| 389 |
-}, |
| 390 |
-}, |
| 391 |
-}, |
| 392 |
-}, |
| 393 |
-"StringPointer": { |
| 394 |
-SchemaProps: spec.SchemaProps{ |
| 395 |
-Description: "a member with a string pointer", |
| 396 |
-Default: "foo", |
| 397 |
-Type: []string{"string"}, |
| 398 |
-Format: "", |
| 399 |
-}, |
| 400 |
-}, |
| 401 |
-"omitted": { |
| 402 |
-SchemaProps: spec.SchemaProps{ |
| 403 |
-Description: "an int member with a default", |
| 404 |
-Default: 1, |
| 405 |
-Type: []string{"integer"}, |
| 406 |
-Format: "int32", |
| 407 |
-}, |
| 408 |
-}, |
| 409 |
-"InvalidEscapeSequenceInComment": { |
| 410 |
-SchemaProps: spec.SchemaProps{ |
| 411 |
-Description: "a field with an invalid escape sequence in comment ex) regexp:^.*\\.yaml$", |
| 412 |
-Default: "", |
| 413 |
-Type: []string{"string"}, |
| 414 |
-Format: "", |
| 415 |
-}, |
| 416 |
-}, |
| 417 |
-}, |
| 418 |
-Required: []string{"String","Int64","Int32","Int16","Int8","Uint","Uint64","Uint32","Uint16","Uint8","Byte","Bool","Float64","Float32","ByteArray","WithExtension","WithStructTagExtension","WithListType","Map","StringPointer","InvalidEscapeSequenceInComment"}, |
| 419 |
-}, |
| 420 |
-VendorExtensible: spec.VendorExtensible{ |
| 421 |
-Extensions: spec.Extensions{ |
| 422 |
-"x-kubernetes-type-tag": "type_test", |
| 423 |
-}, |
| 424 |
-}, |
| 425 |
-}, |
| 426 |
-} |
| 427 |
-}`) |
| 428 |
- }) |
| 429 |
-} |
| 430 |
- |
| 431 |
-func TestEmptyProperties(t *testing.T) { |
| 432 |
- inputFile := ` |
| 433 |
- package foo |
| 434 |
- |
| 435 |
- // Blah demonstrate a struct without fields. |
| 436 |
- type Blah struct { |
| 437 |
- }` |
| 438 |
- |
| 439 |
- packagestest.TestAll(t, func(t *testing.T, x packagestest.Exporter) { |
| 440 |
- e := packagestest.Export(t, x, []packagestest.Module{{ |
| 441 |
- Name: "example.com/base/foo", |
| 442 |
- Files: map[string]interface{}{ |
| 443 |
- "foo.go": inputFile, |
| 444 |
- }, |
| 445 |
- }}) |
| 446 |
- defer e.Cleanup() |
| 447 |
- |
| 448 |
- callErr, funcErr, callBuffer, funcBuffer, _ := testOpenAPITypeWriter(t, e.Config) |
| 449 |
- if callErr != nil { |
| 450 |
- t.Fatal(callErr) |
| 451 |
- } |
| 452 |
- if funcErr != nil { |
| 453 |
- t.Fatal(funcErr) |
| 454 |
- } |
| 455 |
- assertEqual(t, callBuffer.String(), |
| 456 |
- `"example.com/base/foo.Blah": schema_examplecom_base_foo_Blah(ref),`) |
| 457 |
- assertEqual(t, funcBuffer.String(), |
| 458 |
- `func schema_examplecom_base_foo_Blah(ref common.ReferenceCallback) common.OpenAPIDefinition { |
| 459 |
-return common.OpenAPIDefinition{ |
| 460 |
-Schema: spec.Schema{ |
| 461 |
-SchemaProps: spec.SchemaProps{ |
| 462 |
-Description: "Blah demonstrate a struct without fields.", |
| 463 |
-Type: []string{"object"}, |
| 464 |
-}, |
| 465 |
-}, |
| 466 |
-} |
| 467 |
-}`) |
| 468 |
- }) |
| 469 |
-} |
| 470 |
- |
| 471 |
-func TestNestedStruct(t *testing.T) { |
| 472 |
- inputFile := ` |
| 473 |
- package foo |
| 474 |
- |
| 475 |
- // Nested is used as struct field |
| 476 |
- type Nested struct { |
| 477 |
- // A simple string |
| 478 |
- String string |
| 479 |
- } |
| 480 |
- |
| 481 |
- // Blah demonstrate a struct with struct field. |
| 482 |
- type Blah struct { |
| 483 |
- // A struct field |
| 484 |
- Field Nested |
| 485 |
- }` |
| 486 |
- |
| 487 |
- packagestest.TestAll(t, func(t *testing.T, x packagestest.Exporter) { |
| 488 |
- e := packagestest.Export(t, x, []packagestest.Module{{ |
| 489 |
- Name: "example.com/base/foo", |
| 490 |
- Files: map[string]interface{}{ |
| 491 |
- "foo.go": inputFile, |
| 492 |
- }, |
| 493 |
- }}) |
| 494 |
- defer e.Cleanup() |
| 495 |
- |
| 496 |
- callErr, funcErr, callBuffer, funcBuffer, _ := testOpenAPITypeWriter(t, e.Config) |
| 497 |
- if callErr != nil { |
| 498 |
- t.Fatal(callErr) |
| 499 |
- } |
| 500 |
- if funcErr != nil { |
| 501 |
- t.Fatal(funcErr) |
| 502 |
- } |
| 503 |
- assertEqual(t, callBuffer.String(), |
| 504 |
- `"example.com/base/foo.Blah": schema_examplecom_base_foo_Blah(ref),`) |
| 505 |
- assertEqual(t, funcBuffer.String(), |
| 506 |
- `func schema_examplecom_base_foo_Blah(ref common.ReferenceCallback) common.OpenAPIDefinition { |
| 507 |
-return common.OpenAPIDefinition{ |
| 508 |
-Schema: spec.Schema{ |
| 509 |
-SchemaProps: spec.SchemaProps{ |
| 510 |
-Description: "Blah demonstrate a struct with struct field.", |
| 511 |
-Type: []string{"object"}, |
| 512 |
-Properties: map[string]spec.Schema{ |
| 513 |
-"Field": { |
| 514 |
-SchemaProps: spec.SchemaProps{ |
| 515 |
-Description: "A struct field", |
| 516 |
-Default: map[string]interface {}{}, |
| 517 |
-Ref: ref("example.com/base/foo.Nested"), |
| 518 |
-}, |
| 519 |
-}, |
| 520 |
-}, |
| 521 |
-Required: []string{"Field"}, |
| 522 |
-}, |
| 523 |
-}, |
| 524 |
-Dependencies: []string{ |
| 525 |
-"example.com/base/foo.Nested",}, |
| 526 |
-} |
| 527 |
-}`) |
| 528 |
- }) |
| 529 |
-} |
| 530 |
- |
| 531 |
-func TestNestedStructPointer(t *testing.T) { |
| 532 |
- inputFile := ` |
| 533 |
- package foo |
| 534 |
- |
| 535 |
- // Nested is used as struct pointer field |
| 536 |
- type Nested struct { |
| 537 |
- // A simple string |
| 538 |
- String string |
| 539 |
- } |
| 540 |
- |
| 541 |
- // Blah demonstrate a struct with struct pointer field. |
| 542 |
- type Blah struct { |
| 543 |
- // A struct pointer field |
| 544 |
- Field *Nested |
| 545 |
- }` |
| 546 |
- |
| 547 |
- packagestest.TestAll(t, func(t *testing.T, x packagestest.Exporter) { |
| 548 |
- e := packagestest.Export(t, x, []packagestest.Module{{ |
| 549 |
- Name: "example.com/base/foo", |
| 550 |
- Files: map[string]interface{}{ |
| 551 |
- "foo.go": inputFile, |
| 552 |
- }, |
| 553 |
- }}) |
| 554 |
- defer e.Cleanup() |
| 555 |
- |
| 556 |
- callErr, funcErr, callBuffer, funcBuffer, _ := testOpenAPITypeWriter(t, e.Config) |
| 557 |
- |
| 558 |
- if callErr != nil { |
| 559 |
- t.Fatal(callErr) |
| 560 |
- } |
| 561 |
- if funcErr != nil { |
| 562 |
- t.Fatal(funcErr) |
| 563 |
- } |
| 564 |
- assertEqual(t, callBuffer.String(), |
| 565 |
- `"example.com/base/foo.Blah": schema_examplecom_base_foo_Blah(ref),`) |
| 566 |
- assertEqual(t, funcBuffer.String(), |
| 567 |
- `func schema_examplecom_base_foo_Blah(ref common.ReferenceCallback) common.OpenAPIDefinition { |
| 568 |
-return common.OpenAPIDefinition{ |
| 569 |
-Schema: spec.Schema{ |
| 570 |
-SchemaProps: spec.SchemaProps{ |
| 571 |
-Description: "Blah demonstrate a struct with struct pointer field.", |
| 572 |
-Type: []string{"object"}, |
| 573 |
-Properties: map[string]spec.Schema{ |
| 574 |
-"Field": { |
| 575 |
-SchemaProps: spec.SchemaProps{ |
| 576 |
-Description: "A struct pointer field", |
| 577 |
-Ref: ref("example.com/base/foo.Nested"), |
| 578 |
-}, |
| 579 |
-}, |
| 580 |
-}, |
| 581 |
-Required: []string{"Field"}, |
| 582 |
-}, |
| 583 |
-}, |
| 584 |
-Dependencies: []string{ |
| 585 |
-"example.com/base/foo.Nested",}, |
| 586 |
-} |
| 587 |
-}`) |
| 588 |
- }) |
| 589 |
-} |
| 590 |
- |
| 591 |
-func TestEmbeddedStruct(t *testing.T) { |
| 592 |
- inputFile := ` |
| 593 |
- package foo |
| 594 |
- |
| 595 |
- // Nested is used as embedded struct field |
| 596 |
- type Nested struct { |
| 597 |
- // A simple string |
| 598 |
- String string |
| 599 |
- } |
| 600 |
- |
| 601 |
- // Blah demonstrate a struct with embedded struct field. |
| 602 |
- type Blah struct { |
| 603 |
- // An embedded struct field |
| 604 |
- Nested |
| 605 |
- }` |
| 606 |
- |
| 607 |
- packagestest.TestAll(t, func(t *testing.T, x packagestest.Exporter) { |
| 608 |
- e := packagestest.Export(t, x, []packagestest.Module{{ |
| 609 |
- Name: "example.com/base/foo", |
| 610 |
- Files: map[string]interface{}{ |
| 611 |
- "foo.go": inputFile, |
| 612 |
- }, |
| 613 |
- }}) |
| 614 |
- defer e.Cleanup() |
| 615 |
- |
| 616 |
- callErr, funcErr, callBuffer, funcBuffer, _ := testOpenAPITypeWriter(t, e.Config) |
| 617 |
- if callErr != nil { |
| 618 |
- t.Fatal(callErr) |
| 619 |
- } |
| 620 |
- if funcErr != nil { |
| 621 |
- t.Fatal(funcErr) |
| 622 |
- } |
| 623 |
- assertEqual(t, callBuffer.String(), |
| 624 |
- `"example.com/base/foo.Blah": schema_examplecom_base_foo_Blah(ref),`) |
| 625 |
- assertEqual(t, funcBuffer.String(), |
| 626 |
- `func schema_examplecom_base_foo_Blah(ref common.ReferenceCallback) common.OpenAPIDefinition { |
| 627 |
-return common.OpenAPIDefinition{ |
| 628 |
-Schema: spec.Schema{ |
| 629 |
-SchemaProps: spec.SchemaProps{ |
| 630 |
-Description: "Blah demonstrate a struct with embedded struct field.", |
| 631 |
-Type: []string{"object"}, |
| 632 |
-Properties: map[string]spec.Schema{ |
| 633 |
-"Nested": { |
| 634 |
-SchemaProps: spec.SchemaProps{ |
| 635 |
-Description: "An embedded struct field", |
| 636 |
-Default: map[string]interface {}{}, |
| 637 |
-Ref: ref("example.com/base/foo.Nested"), |
| 638 |
-}, |
| 639 |
-}, |
| 640 |
-}, |
| 641 |
-Required: []string{"Nested"}, |
| 642 |
-}, |
| 643 |
-}, |
| 644 |
-Dependencies: []string{ |
| 645 |
-"example.com/base/foo.Nested",}, |
| 646 |
-} |
| 647 |
-}`) |
| 648 |
- }) |
| 649 |
-} |
| 650 |
- |
| 651 |
-func TestSingleEmbeddedStruct(t *testing.T) { |
| 652 |
- inputFile := ` |
| 653 |
- package foo |
| 654 |
- |
| 655 |
- import "time" |
| 656 |
- |
| 657 |
- // Nested is used as embedded struct field |
| 658 |
- type Nested struct { |
| 659 |
- // A simple string |
| 660 |
- time.Duration |
| 661 |
- } |
| 662 |
- |
| 663 |
- // Blah demonstrate a struct with embedded struct field. |
| 664 |
- type Blah struct { |
| 665 |
- // An embedded struct field |
| 666 |
- // +default="10ms" |
| 667 |
- Nested ` + "`" + `json:"nested,omitempty" protobuf:"bytes,5,opt,name=nested"` + "`" + ` |
| 668 |
- }` |
| 669 |
- |
| 670 |
- packagestest.TestAll(t, func(t *testing.T, x packagestest.Exporter) { |
| 671 |
- e := packagestest.Export(t, x, []packagestest.Module{{ |
| 672 |
- Name: "example.com/base/foo", |
| 673 |
- Files: map[string]interface{}{ |
| 674 |
- "foo.go": inputFile, |
| 675 |
- }, |
| 676 |
- }}) |
| 677 |
- defer e.Cleanup() |
| 678 |
- |
| 679 |
- callErr, funcErr, callBuffer, funcBuffer, _ := testOpenAPITypeWriter(t, e.Config) |
| 680 |
- if callErr != nil { |
| 681 |
- t.Fatal(callErr) |
| 682 |
- } |
| 683 |
- if funcErr != nil { |
| 684 |
- t.Fatal(funcErr) |
| 685 |
- } |
| 686 |
- assertEqual(t, callBuffer.String(), |
| 687 |
- `"example.com/base/foo.Blah": schema_examplecom_base_foo_Blah(ref),`) |
| 688 |
- assertEqual(t, funcBuffer.String(), |
| 689 |
- `func schema_examplecom_base_foo_Blah(ref common.ReferenceCallback) common.OpenAPIDefinition { |
| 690 |
-return common.OpenAPIDefinition{ |
| 691 |
-Schema: spec.Schema{ |
| 692 |
-SchemaProps: spec.SchemaProps{ |
| 693 |
-Description: "Blah demonstrate a struct with embedded struct field.", |
| 694 |
-Type: []string{"object"}, |
| 695 |
-Properties: map[string]spec.Schema{ |
| 696 |
-"nested": { |
| 697 |
-SchemaProps: spec.SchemaProps{ |
| 698 |
-Description: "An embedded struct field", |
| 699 |
-Default: "10ms", |
| 700 |
-Ref: ref("example.com/base/foo.Nested"), |
| 701 |
-}, |
| 702 |
-}, |
| 703 |
-}, |
| 704 |
-}, |
| 705 |
-}, |
| 706 |
-Dependencies: []string{ |
| 707 |
-"example.com/base/foo.Nested",}, |
| 708 |
-} |
| 709 |
-}`) |
| 710 |
- }) |
| 711 |
-} |
| 712 |
- |
| 713 |
-func TestEmbeddedInlineStruct(t *testing.T) { |
| 714 |
- inputFile := ` |
| 715 |
- package foo |
| 716 |
- |
| 717 |
- // Nested is used as embedded inline struct field |
| 718 |
- type Nested struct { |
| 719 |
- // A simple string |
| 720 |
- String string |
| 721 |
- } |
| 722 |
- |
| 723 |
- // Blah demonstrate a struct with embedded inline struct field. |
| 724 |
- type Blah struct { |
| 725 |
- // An embedded inline struct field |
| 726 |
- Nested ` + "`" + `json:",inline,omitempty"` + "`" + ` |
| 727 |
- }` |
| 728 |
- |
| 729 |
- packagestest.TestAll(t, func(t *testing.T, x packagestest.Exporter) { |
| 730 |
- e := packagestest.Export(t, x, []packagestest.Module{{ |
| 731 |
- Name: "example.com/base/foo", |
| 732 |
- Files: map[string]interface{}{ |
| 733 |
- "foo.go": inputFile, |
| 734 |
- }, |
| 735 |
- }}) |
| 736 |
- defer e.Cleanup() |
| 737 |
- |
| 738 |
- callErr, funcErr, callBuffer, funcBuffer, _ := testOpenAPITypeWriter(t, e.Config) |
| 739 |
- if callErr != nil { |
| 740 |
- t.Fatal(callErr) |
| 741 |
- } |
| 742 |
- if funcErr != nil { |
| 743 |
- t.Fatal(funcErr) |
| 744 |
- } |
| 745 |
- assertEqual(t, callBuffer.String(), |
| 746 |
- `"example.com/base/foo.Blah": schema_examplecom_base_foo_Blah(ref),`) |
| 747 |
- assertEqual(t, funcBuffer.String(), |
| 748 |
- `func schema_examplecom_base_foo_Blah(ref common.ReferenceCallback) common.OpenAPIDefinition { |
| 749 |
-return common.OpenAPIDefinition{ |
| 750 |
-Schema: spec.Schema{ |
| 751 |
-SchemaProps: spec.SchemaProps{ |
| 752 |
-Description: "Blah demonstrate a struct with embedded inline struct field.", |
| 753 |
-Type: []string{"object"}, |
| 754 |
-Properties: map[string]spec.Schema{ |
| 755 |
-"String": { |
| 756 |
-SchemaProps: spec.SchemaProps{ |
| 757 |
-Description: "A simple string", |
| 758 |
-Default: "", |
| 759 |
-Type: []string{"string"}, |
| 760 |
-Format: "", |
| 761 |
-}, |
| 762 |
-}, |
| 763 |
-}, |
| 764 |
-Required: []string{"String"}, |
| 765 |
-}, |
| 766 |
-}, |
| 767 |
-} |
| 768 |
-}`) |
| 769 |
- }) |
| 770 |
-} |
| 771 |
- |
| 772 |
-func TestEmbeddedInlineStructPointer(t *testing.T) { |
| 773 |
- inputFile := ` |
| 774 |
- package foo |
| 775 |
- |
| 776 |
- // Nested is used as embedded inline struct pointer field. |
| 777 |
- type Nested struct { |
| 778 |
- // A simple string |
| 779 |
- String string |
| 780 |
- } |
| 781 |
- |
| 782 |
- // Blah demonstrate a struct with embedded inline struct pointer field. |
| 783 |
- type Blah struct { |
| 784 |
- // An embedded inline struct pointer field |
| 785 |
- *Nested ` + "`" + `json:",inline,omitempty"` + "`" + ` |
| 786 |
- }` |
| 787 |
- |
| 788 |
- packagestest.TestAll(t, func(t *testing.T, x packagestest.Exporter) { |
| 789 |
- e := packagestest.Export(t, x, []packagestest.Module{{ |
| 790 |
- Name: "example.com/base/foo", |
| 791 |
- Files: map[string]interface{}{ |
| 792 |
- "foo.go": inputFile, |
| 793 |
- }, |
| 794 |
- }}) |
| 795 |
- defer e.Cleanup() |
| 796 |
- |
| 797 |
- callErr, funcErr, callBuffer, funcBuffer, _ := testOpenAPITypeWriter(t, e.Config) |
| 798 |
- if callErr != nil { |
| 799 |
- t.Fatal(callErr) |
| 800 |
- } |
| 801 |
- if funcErr != nil { |
| 802 |
- t.Fatal(funcErr) |
| 803 |
- } |
| 804 |
- assertEqual(t, callBuffer.String(), |
| 805 |
- `"example.com/base/foo.Blah": schema_examplecom_base_foo_Blah(ref),`) |
| 806 |
- assertEqual(t, funcBuffer.String(), |
| 807 |
- `func schema_examplecom_base_foo_Blah(ref common.ReferenceCallback) common.OpenAPIDefinition { |
| 808 |
-return common.OpenAPIDefinition{ |
| 809 |
-Schema: spec.Schema{ |
| 810 |
-SchemaProps: spec.SchemaProps{ |
| 811 |
-Description: "Blah demonstrate a struct with embedded inline struct pointer field.", |
| 812 |
-Type: []string{"object"}, |
| 813 |
-Properties: map[string]spec.Schema{ |
| 814 |
-"String": { |
| 815 |
-SchemaProps: spec.SchemaProps{ |
| 816 |
-Description: "A simple string", |
| 817 |
-Default: "", |
| 818 |
-Type: []string{"string"}, |
| 819 |
-Format: "", |
| 820 |
-}, |
| 821 |
-}, |
| 822 |
-}, |
| 823 |
-Required: []string{"String"}, |
| 824 |
-}, |
| 825 |
-}, |
| 826 |
-} |
| 827 |
-}`) |
| 828 |
- }) |
| 829 |
-} |
| 830 |
- |
| 831 |
-func TestNestedMapString(t *testing.T) { |
| 832 |
- inputFile := ` |
| 833 |
- package foo |
| 834 |
- |
| 835 |
- // Map sample tests openAPIGen.generateMapProperty method. |
| 836 |
- type Blah struct { |
| 837 |
- // A sample String to String map |
| 838 |
- StringToArray map[string]map[string]string |
| 839 |
- }` |
| 840 |
- |
| 841 |
- packagestest.TestAll(t, func(t *testing.T, x packagestest.Exporter) { |
| 842 |
- e := packagestest.Export(t, x, []packagestest.Module{{ |
| 843 |
- Name: "example.com/base/foo", |
| 844 |
- Files: map[string]interface{}{ |
| 845 |
- "foo.go": inputFile, |
| 846 |
- }, |
| 847 |
- }}) |
| 848 |
- defer e.Cleanup() |
| 849 |
- |
| 850 |
- callErr, funcErr, callBuffer, funcBuffer, _ := testOpenAPITypeWriter(t, e.Config) |
| 851 |
- if callErr != nil { |
| 852 |
- t.Fatal(callErr) |
| 853 |
- } |
| 854 |
- if funcErr != nil { |
| 855 |
- t.Fatal(funcErr) |
| 856 |
- } |
| 857 |
- assertEqual(t, callBuffer.String(), |
| 858 |
- `"example.com/base/foo.Blah": schema_examplecom_base_foo_Blah(ref),`) |
| 859 |
- assertEqual(t, funcBuffer.String(), |
| 860 |
- `func schema_examplecom_base_foo_Blah(ref common.ReferenceCallback) common.OpenAPIDefinition { |
| 861 |
-return common.OpenAPIDefinition{ |
| 862 |
-Schema: spec.Schema{ |
| 863 |
-SchemaProps: spec.SchemaProps{ |
| 864 |
-Description: "Map sample tests openAPIGen.generateMapProperty method.", |
| 865 |
-Type: []string{"object"}, |
| 866 |
-Properties: map[string]spec.Schema{ |
| 867 |
-"StringToArray": { |
| 868 |
-SchemaProps: spec.SchemaProps{ |
| 869 |
-Description: "A sample String to String map", |
| 870 |
-Type: []string{"object"}, |
| 871 |
-AdditionalProperties: &spec.SchemaOrBool{ |
| 872 |
-Allows: true, |
| 873 |
-Schema: &spec.Schema{ |
| 874 |
-SchemaProps: spec.SchemaProps{ |
| 875 |
-Type: []string{"object"}, |
| 876 |
-AdditionalProperties: &spec.SchemaOrBool{ |
| 877 |
-Allows: true, |
| 878 |
-Schema: &spec.Schema{ |
| 879 |
-SchemaProps: spec.SchemaProps{ |
| 880 |
-Default: "", |
| 881 |
-Type: []string{"string"}, |
| 882 |
-Format: "", |
| 883 |
-}, |
| 884 |
-}, |
| 885 |
-}, |
| 886 |
-}, |
| 887 |
-}, |
| 888 |
-}, |
| 889 |
-}, |
| 890 |
-}, |
| 891 |
-}, |
| 892 |
-Required: []string{"StringToArray"}, |
| 893 |
-}, |
| 894 |
-}, |
| 895 |
-} |
| 896 |
-}`) |
| 897 |
- }) |
| 898 |
-} |
| 899 |
- |
| 900 |
-func TestNestedMapInt(t *testing.T) { |
| 901 |
- inputFile := ` |
| 902 |
- package foo |
| 903 |
- |
| 904 |
- // Map sample tests openAPIGen.generateMapProperty method. |
| 905 |
- type Blah struct { |
| 906 |
- // A sample String to String map |
| 907 |
- StringToArray map[string]map[string]int |
| 908 |
- }` |
| 909 |
- |
| 910 |
- packagestest.TestAll(t, func(t *testing.T, x packagestest.Exporter) { |
| 911 |
- e := packagestest.Export(t, x, []packagestest.Module{{ |
| 912 |
- Name: "example.com/base/foo", |
| 913 |
- Files: map[string]interface{}{ |
| 914 |
- "foo.go": inputFile, |
| 915 |
- }, |
| 916 |
- }}) |
| 917 |
- defer e.Cleanup() |
| 918 |
- |
| 919 |
- callErr, funcErr, callBuffer, funcBuffer, _ := testOpenAPITypeWriter(t, e.Config) |
| 920 |
- if callErr != nil { |
| 921 |
- t.Fatal(callErr) |
| 922 |
- } |
| 923 |
- if funcErr != nil { |
| 924 |
- t.Fatal(funcErr) |
| 925 |
- } |
| 926 |
- assertEqual(t, callBuffer.String(), |
| 927 |
- `"example.com/base/foo.Blah": schema_examplecom_base_foo_Blah(ref),`) |
| 928 |
- assertEqual(t, funcBuffer.String(), |
| 929 |
- `func schema_examplecom_base_foo_Blah(ref common.ReferenceCallback) common.OpenAPIDefinition { |
| 930 |
-return common.OpenAPIDefinition{ |
| 931 |
-Schema: spec.Schema{ |
| 932 |
-SchemaProps: spec.SchemaProps{ |
| 933 |
-Description: "Map sample tests openAPIGen.generateMapProperty method.", |
| 934 |
-Type: []string{"object"}, |
| 935 |
-Properties: map[string]spec.Schema{ |
| 936 |
-"StringToArray": { |
| 937 |
-SchemaProps: spec.SchemaProps{ |
| 938 |
-Description: "A sample String to String map", |
| 939 |
-Type: []string{"object"}, |
| 940 |
-AdditionalProperties: &spec.SchemaOrBool{ |
| 941 |
-Allows: true, |
| 942 |
-Schema: &spec.Schema{ |
| 943 |
-SchemaProps: spec.SchemaProps{ |
| 944 |
-Type: []string{"object"}, |
| 945 |
-AdditionalProperties: &spec.SchemaOrBool{ |
| 946 |
-Allows: true, |
| 947 |
-Schema: &spec.Schema{ |
| 948 |
-SchemaProps: spec.SchemaProps{ |
| 949 |
-Default: 0, |
| 950 |
-Type: []string{"integer"}, |
| 951 |
-Format: "int32", |
| 952 |
-}, |
| 953 |
-}, |
| 954 |
-}, |
| 955 |
-}, |
| 956 |
-}, |
| 957 |
-}, |
| 958 |
-}, |
| 959 |
-}, |
| 960 |
-}, |
| 961 |
-Required: []string{"StringToArray"}, |
| 962 |
-}, |
| 963 |
-}, |
| 964 |
-} |
| 965 |
-}`) |
| 966 |
- }) |
| 967 |
-} |
| 968 |
- |
| 969 |
-func TestNestedMapBoolean(t *testing.T) { |
| 970 |
- inputFile := ` |
| 971 |
- package foo |
| 972 |
- |
| 973 |
- // Map sample tests openAPIGen.generateMapProperty method. |
| 974 |
- type Blah struct { |
| 975 |
- // A sample String to String map |
| 976 |
- StringToArray map[string]map[string]bool |
| 977 |
- }` |
| 978 |
- |
| 979 |
- packagestest.TestAll(t, func(t *testing.T, x packagestest.Exporter) { |
| 980 |
- e := packagestest.Export(t, x, []packagestest.Module{{ |
| 981 |
- Name: "example.com/base/foo", |
| 982 |
- Files: map[string]interface{}{ |
| 983 |
- "foo.go": inputFile, |
| 984 |
- }, |
| 985 |
- }}) |
| 986 |
- defer e.Cleanup() |
| 987 |
- |
| 988 |
- callErr, funcErr, callBuffer, funcBuffer, _ := testOpenAPITypeWriter(t, e.Config) |
| 989 |
- if callErr != nil { |
| 990 |
- t.Fatal(callErr) |
| 991 |
- } |
| 992 |
- if funcErr != nil { |
| 993 |
- t.Fatal(funcErr) |
| 994 |
- } |
| 995 |
- assertEqual(t, callBuffer.String(), |
| 996 |
- `"example.com/base/foo.Blah": schema_examplecom_base_foo_Blah(ref),`) |
| 997 |
- assertEqual(t, funcBuffer.String(), |
| 998 |
- `func schema_examplecom_base_foo_Blah(ref common.ReferenceCallback) common.OpenAPIDefinition { |
| 999 |
-return common.OpenAPIDefinition{ |
| 1000 |
-Schema: spec.Schema{ |
| 1001 |
-SchemaProps: spec.SchemaProps{ |
| 1002 |
-Description: "Map sample tests openAPIGen.generateMapProperty method.", |
| 1003 |
-Type: []string{"object"}, |
| 1004 |
-Properties: map[string]spec.Schema{ |
| 1005 |
-"StringToArray": { |
| 1006 |
-SchemaProps: spec.SchemaProps{ |
| 1007 |
-Description: "A sample String to String map", |
| 1008 |
-Type: []string{"object"}, |
| 1009 |
-AdditionalProperties: &spec.SchemaOrBool{ |
| 1010 |
-Allows: true, |
| 1011 |
-Schema: &spec.Schema{ |
| 1012 |
-SchemaProps: spec.SchemaProps{ |
| 1013 |
-Type: []string{"object"}, |
| 1014 |
-AdditionalProperties: &spec.SchemaOrBool{ |
| 1015 |
-Allows: true, |
| 1016 |
-Schema: &spec.Schema{ |
| 1017 |
-SchemaProps: spec.SchemaProps{ |
| 1018 |
-Default: false, |
| 1019 |
-Type: []string{"boolean"}, |
| 1020 |
-Format: "", |
| 1021 |
-}, |
| 1022 |
-}, |
| 1023 |
-}, |
| 1024 |
-}, |
| 1025 |
-}, |
| 1026 |
-}, |
| 1027 |
-}, |
| 1028 |
-}, |
| 1029 |
-}, |
| 1030 |
-Required: []string{"StringToArray"}, |
| 1031 |
-}, |
| 1032 |
-}, |
| 1033 |
-} |
| 1034 |
-}`) |
| 1035 |
- }) |
| 1036 |
-} |
| 1037 |
- |
| 1038 |
-func TestFailingSample1(t *testing.T) { |
| 1039 |
- inputFile := ` |
| 1040 |
- package foo |
| 1041 |
- |
| 1042 |
- // Map sample tests openAPIGen.generateMapProperty method. |
| 1043 |
- type Blah struct { |
| 1044 |
- // A sample String to String map |
| 1045 |
- StringToArray map[string]map[string]map[int]string |
| 1046 |
- }` |
| 1047 |
- |
| 1048 |
- packagestest.TestAll(t, func(t *testing.T, x packagestest.Exporter) { |
| 1049 |
- e := packagestest.Export(t, x, []packagestest.Module{{ |
| 1050 |
- Name: "example.com/base/foo", |
| 1051 |
- Files: map[string]interface{}{ |
| 1052 |
- "foo.go": inputFile, |
| 1053 |
- }, |
| 1054 |
- }}) |
| 1055 |
- defer e.Cleanup() |
| 1056 |
- |
| 1057 |
- _, funcErr, _, _, _ := testOpenAPITypeWriter(t, e.Config) |
| 1058 |
- if funcErr == nil { |
| 1059 |
- t.Fatalf("An error was expected") |
| 1060 |
- } |
| 1061 |
- assertEqual(t, |
| 1062 |
- "failed to generate map property in example.com/base/foo.Blah: StringToArray: map with non-string keys are not supported by OpenAPI in map[int]string", |
| 1063 |
- funcErr.Error()) |
| 1064 |
- }) |
| 1065 |
-} |
| 1066 |
- |
| 1067 |
-func TestFailingSample2(t *testing.T) { |
| 1068 |
- inputFile := ` |
| 1069 |
- package foo |
| 1070 |
- |
| 1071 |
- // Map sample tests openAPIGen.generateMapProperty method. |
| 1072 |
- type Blah struct { |
| 1073 |
- // A sample String to String map |
| 1074 |
- StringToArray map[int]string |
| 1075 |
- }` |
| 1076 |
- |
| 1077 |
- packagestest.TestAll(t, func(t *testing.T, x packagestest.Exporter) { |
| 1078 |
- e := packagestest.Export(t, x, []packagestest.Module{{ |
| 1079 |
- Name: "example.com/base/foo", |
| 1080 |
- Files: map[string]interface{}{ |
| 1081 |
- "foo.go": inputFile, |
| 1082 |
- }, |
| 1083 |
- }}) |
| 1084 |
- defer e.Cleanup() |
| 1085 |
- |
| 1086 |
- _, funcErr, _, _, _ := testOpenAPITypeWriter(t, e.Config) |
| 1087 |
- if funcErr == nil { |
| 1088 |
- t.Fatalf("An error was expected") |
| 1089 |
- } |
| 1090 |
- assertEqual(t, |
| 1091 |
- "failed to generate map property in example.com/base/foo.Blah: StringToArray: map with non-string keys are not supported by OpenAPI in map[int]string", |
| 1092 |
- funcErr.Error()) |
| 1093 |
- }) |
| 1094 |
-} |
| 1095 |
- |
| 1096 |
-func TestFailingDefaultEnforced(t *testing.T) { |
| 1097 |
- tests := []struct { |
| 1098 |
- definition string |
| 1099 |
- expectedError string |
| 1100 |
- }{{ |
| 1101 |
- definition: ` |
| 1102 |
- package foo |
| 1103 |
- |
| 1104 |
- type Blah struct { |
| 1105 |
- // +default=5 |
| 1106 |
- Int int |
| 1107 |
- }`, |
| 1108 |
- expectedError: "failed to generate default in example.com/base/foo.Blah: Int: invalid default value (5) for non-pointer/non-omitempty. If specified, must be: 0", |
| 1109 |
- }, { |
| 1110 |
- definition: ` |
| 1111 |
- package foo |
| 1112 |
- |
| 1113 |
- type Blah struct { |
| 1114 |
- // +default={"foo": 5} |
| 1115 |
- Struct struct{ |
| 1116 |
- foo int |
| 1117 |
- } |
| 1118 |
- }`, |
| 1119 |
- expectedError: `failed to generate default in example.com/base/foo.Blah: Struct: invalid default value (map[string]interface {}{"foo":5}) for non-pointer/non-omitempty. If specified, must be: {}`, |
| 1120 |
- }, { |
| 1121 |
- definition: ` |
| 1122 |
- package foo |
| 1123 |
- |
| 1124 |
- type Blah struct { |
| 1125 |
- List []Item |
| 1126 |
- |
| 1127 |
- } |
| 1128 |
- |
| 1129 |
- // +default="foo" |
| 1130 |
- type Item string`, |
| 1131 |
- expectedError: `failed to generate slice property in example.com/base/foo.Blah: List: invalid default value ("foo") for non-pointer/non-omitempty. If specified, must be: ""`, |
| 1132 |
- }, { |
| 1133 |
- definition: ` |
| 1134 |
- package foo |
| 1135 |
- |
| 1136 |
- type Blah struct { |
| 1137 |
- Map map[string]Item |
| 1138 |
- |
| 1139 |
- } |
| 1140 |
- |
| 1141 |
- // +default="foo" |
| 1142 |
- type Item string`, |
| 1143 |
- expectedError: `failed to generate map property in example.com/base/foo.Blah: Map: invalid default value ("foo") for non-pointer/non-omitempty. If specified, must be: ""`, |
| 1144 |
- }} |
| 1145 |
- |
| 1146 |
- for i, test := range tests { |
| 1147 |
- t.Run(fmt.Sprintf("%d", i), func(t *testing.T) { |
| 1148 |
- packagestest.TestAll(t, func(t *testing.T, x packagestest.Exporter) { |
| 1149 |
- e := packagestest.Export(t, x, []packagestest.Module{{ |
| 1150 |
- Name: "example.com/base/foo", |
| 1151 |
- Files: map[string]interface{}{ |
| 1152 |
- "foo.go": test.definition, |
| 1153 |
- }, |
| 1154 |
- }}) |
| 1155 |
- defer e.Cleanup() |
| 1156 |
- |
| 1157 |
- _, funcErr, _, _, _ := testOpenAPITypeWriter(t, e.Config) |
| 1158 |
- if funcErr == nil { |
| 1159 |
- t.Fatalf("An error was expected") |
| 1160 |
- } |
| 1161 |
- assertEqual(t, test.expectedError, funcErr.Error()) |
| 1162 |
- }) |
| 1163 |
- }) |
| 1164 |
- } |
| 1165 |
-} |
| 1166 |
- |
| 1167 |
-func TestCustomDef(t *testing.T) { |
| 1168 |
- inputFile := ` |
| 1169 |
- package foo |
| 1170 |
- |
| 1171 |
- import openapi "k8s.io/kube-openapi/pkg/common" |
| 1172 |
- |
| 1173 |
- type Blah struct { |
| 1174 |
- } |
| 1175 |
- |
| 1176 |
- func (_ Blah) OpenAPIDefinition() openapi.OpenAPIDefinition { |
| 1177 |
- return openapi.OpenAPIDefinition{ |
| 1178 |
- Schema: spec.Schema{ |
| 1179 |
- SchemaProps: spec.SchemaProps{ |
| 1180 |
- Type: []string{"string"}, |
| 1181 |
- Format: "date-time", |
| 1182 |
- }, |
| 1183 |
- }, |
| 1184 |
- } |
| 1185 |
- }` |
| 1186 |
- commonFile := `package common |
| 1187 |
- |
| 1188 |
- type OpenAPIDefinition struct {}` |
| 1189 |
- |
| 1190 |
- packagestest.TestAll(t, func(t *testing.T, x packagestest.Exporter) { |
| 1191 |
- e := packagestest.Export(t, x, []packagestest.Module{{ |
| 1192 |
- Name: "example.com/base/foo", |
| 1193 |
- Files: map[string]interface{}{ |
| 1194 |
- "foo.go": inputFile, |
| 1195 |
- }, |
| 1196 |
- }, { |
| 1197 |
- Name: "k8s.io/kube-openapi/pkg/common", |
| 1198 |
- Files: map[string]interface{}{ |
| 1199 |
- "common.go": commonFile, |
| 1200 |
- }, |
| 1201 |
- }}) |
| 1202 |
- defer e.Cleanup() |
| 1203 |
- |
| 1204 |
- callErr, funcErr, callBuffer, funcBuffer, _ := testOpenAPITypeWriter(t, e.Config) |
| 1205 |
- if callErr != nil { |
| 1206 |
- t.Fatal(callErr) |
| 1207 |
- } |
| 1208 |
- if funcErr != nil { |
| 1209 |
- t.Fatal(funcErr) |
| 1210 |
- } |
| 1211 |
- assertEqual(t, callBuffer.String(), |
| 1212 |
- `"example.com/base/foo.Blah": foo.Blah{}.OpenAPIDefinition(),`) |
| 1213 |
- assertEqual(t, "", funcBuffer.String()) |
| 1214 |
- }) |
| 1215 |
-} |
| 1216 |
- |
| 1217 |
-func TestCustomDefV3(t *testing.T) { |
| 1218 |
- inputFile := ` |
| 1219 |
- package foo |
| 1220 |
- |
| 1221 |
- import openapi "k8s.io/kube-openapi/pkg/common" |
| 1222 |
- |
| 1223 |
- type Blah struct { |
| 1224 |
- } |
| 1225 |
- |
| 1226 |
- func (_ Blah) OpenAPIV3Definition() openapi.OpenAPIDefinition { |
| 1227 |
- return openapi.OpenAPIDefinition{ |
| 1228 |
- Schema: spec.Schema{ |
| 1229 |
- SchemaProps: spec.SchemaProps{ |
| 1230 |
- Type: []string{"string"}, |
| 1231 |
- Format: "date-time", |
| 1232 |
- }, |
| 1233 |
- }, |
| 1234 |
- } |
| 1235 |
- }` |
| 1236 |
- commonFile := `package common |
| 1237 |
- |
| 1238 |
- type OpenAPIDefinition struct {}` |
| 1239 |
- |
| 1240 |
- packagestest.TestAll(t, func(t *testing.T, x packagestest.Exporter) { |
| 1241 |
- e := packagestest.Export(t, x, []packagestest.Module{{ |
| 1242 |
- Name: "example.com/base/foo", |
| 1243 |
- Files: map[string]interface{}{ |
| 1244 |
- "foo.go": inputFile, |
| 1245 |
- }, |
| 1246 |
- }, { |
| 1247 |
- Name: "k8s.io/kube-openapi/pkg/common", |
| 1248 |
- Files: map[string]interface{}{ |
| 1249 |
- "common.go": commonFile, |
| 1250 |
- }, |
| 1251 |
- }}) |
| 1252 |
- defer e.Cleanup() |
| 1253 |
- |
| 1254 |
- callErr, funcErr, callBuffer, funcBuffer, _ := testOpenAPITypeWriter(t, e.Config) |
| 1255 |
- if callErr != nil { |
| 1256 |
- t.Fatal(callErr) |
| 1257 |
- } |
| 1258 |
- if funcErr != nil { |
| 1259 |
- t.Fatal(funcErr) |
| 1260 |
- } |
| 1261 |
- assertEqual(t, callBuffer.String(), |
| 1262 |
- `"example.com/base/foo.Blah": foo.Blah{}.OpenAPIV3Definition(),`) |
| 1263 |
- assertEqual(t, "", funcBuffer.String()) |
| 1264 |
- }) |
| 1265 |
-} |
| 1266 |
- |
| 1267 |
-func TestCustomDefV2AndV3(t *testing.T) { |
| 1268 |
- inputFile := ` |
| 1269 |
- package foo |
| 1270 |
- |
| 1271 |
- import openapi "k8s.io/kube-openapi/pkg/common" |
| 1272 |
- |
| 1273 |
- type Blah struct { |
| 1274 |
- } |
| 1275 |
- |
| 1276 |
- func (_ Blah) OpenAPIV3Definition() openapi.OpenAPIDefinition { |
| 1277 |
- return openapi.OpenAPIDefinition{ |
| 1278 |
- Schema: spec.Schema{ |
| 1279 |
- SchemaProps: spec.SchemaProps{ |
| 1280 |
- Type: []string{"string"}, |
| 1281 |
- Format: "date-time", |
| 1282 |
- }, |
| 1283 |
- }, |
| 1284 |
- } |
| 1285 |
- } |
| 1286 |
- |
| 1287 |
- func (_ Blah) OpenAPIDefinition() openapi.OpenAPIDefinition { |
| 1288 |
- return openapi.OpenAPIDefinition{ |
| 1289 |
- Schema: spec.Schema{ |
| 1290 |
- SchemaProps: spec.SchemaProps{ |
| 1291 |
- Type: []string{"string"}, |
| 1292 |
- Format: "date-time", |
| 1293 |
- }, |
| 1294 |
- }, |
| 1295 |
- } |
| 1296 |
- }` |
| 1297 |
- commonFile := `package common |
| 1298 |
- |
| 1299 |
- type OpenAPIDefinition struct {}` |
| 1300 |
- |
| 1301 |
- packagestest.TestAll(t, func(t *testing.T, x packagestest.Exporter) { |
| 1302 |
- e := packagestest.Export(t, x, []packagestest.Module{{ |
| 1303 |
- Name: "example.com/base/foo", |
| 1304 |
- Files: map[string]interface{}{ |
| 1305 |
- "foo.go": inputFile, |
| 1306 |
- }, |
| 1307 |
- }, { |
| 1308 |
- Name: "k8s.io/kube-openapi/pkg/common", |
| 1309 |
- Files: map[string]interface{}{ |
| 1310 |
- "common.go": commonFile, |
| 1311 |
- }, |
| 1312 |
- }}) |
| 1313 |
- defer e.Cleanup() |
| 1314 |
- |
| 1315 |
- callErr, funcErr, callBuffer, funcBuffer, _ := testOpenAPITypeWriter(t, e.Config) |
| 1316 |
- if callErr != nil { |
| 1317 |
- t.Fatal(callErr) |
| 1318 |
- } |
| 1319 |
- if funcErr != nil { |
| 1320 |
- t.Fatal(funcErr) |
| 1321 |
- } |
| 1322 |
- assertEqual(t, callBuffer.String(), |
| 1323 |
- `"example.com/base/foo.Blah": common.EmbedOpenAPIDefinitionIntoV2Extension(foo.Blah{}.OpenAPIV3Definition(), foo.Blah{}.OpenAPIDefinition()),`) |
| 1324 |
- assertEqual(t, "", funcBuffer.String()) |
| 1325 |
- }) |
| 1326 |
-} |
| 1327 |
- |
| 1328 |
-func TestCustomDefs(t *testing.T) { |
| 1329 |
- inputFile := ` |
| 1330 |
- package foo |
| 1331 |
- |
| 1332 |
- // Blah is a custom type |
| 1333 |
- type Blah struct { |
| 1334 |
- } |
| 1335 |
- |
| 1336 |
- func (_ Blah) OpenAPISchemaType() []string { return []string{"string"} } |
| 1337 |
- func (_ Blah) OpenAPISchemaFormat() string { return "date-time" }` |
| 1338 |
- |
| 1339 |
- packagestest.TestAll(t, func(t *testing.T, x packagestest.Exporter) { |
| 1340 |
- e := packagestest.Export(t, x, []packagestest.Module{{ |
| 1341 |
- Name: "example.com/base/foo", |
| 1342 |
- Files: map[string]interface{}{ |
| 1343 |
- "foo.go": inputFile, |
| 1344 |
- }, |
| 1345 |
- }}) |
| 1346 |
- defer e.Cleanup() |
| 1347 |
- |
| 1348 |
- callErr, funcErr, callBuffer, funcBuffer, _ := testOpenAPITypeWriter(t, e.Config) |
| 1349 |
- if callErr != nil { |
| 1350 |
- t.Fatal(callErr) |
| 1351 |
- } |
| 1352 |
- if funcErr != nil { |
| 1353 |
- t.Fatal(funcErr) |
| 1354 |
- } |
| 1355 |
- assertEqual(t, callBuffer.String(), |
| 1356 |
- `"example.com/base/foo.Blah": schema_examplecom_base_foo_Blah(ref),`) |
| 1357 |
- assertEqual(t, funcBuffer.String(), |
| 1358 |
- `func schema_examplecom_base_foo_Blah(ref common.ReferenceCallback) common.OpenAPIDefinition { |
| 1359 |
-return common.OpenAPIDefinition{ |
| 1360 |
-Schema: spec.Schema{ |
| 1361 |
-SchemaProps: spec.SchemaProps{ |
| 1362 |
-Description: "Blah is a custom type", |
| 1363 |
-Type:foo.Blah{}.OpenAPISchemaType(), |
| 1364 |
-Format:foo.Blah{}.OpenAPISchemaFormat(), |
| 1365 |
-}, |
| 1366 |
-}, |
| 1367 |
-} |
| 1368 |
-}`) |
| 1369 |
- }) |
| 1370 |
-} |
| 1371 |
- |
| 1372 |
-func TestCustomDefsV3(t *testing.T) { |
| 1373 |
- inputFile := ` |
| 1374 |
- package foo |
| 1375 |
- |
| 1376 |
- import openapi "k8s.io/kube-openapi/pkg/common" |
| 1377 |
- |
| 1378 |
- // Blah is a custom type |
| 1379 |
- type Blah struct { |
| 1380 |
- } |
| 1381 |
- |
| 1382 |
- func (_ Blah) OpenAPIV3Definition() openapi.OpenAPIDefinition { |
| 1383 |
- return openapi.OpenAPIDefinition{ |
| 1384 |
- Schema: spec.Schema{ |
| 1385 |
- SchemaProps: spec.SchemaProps{ |
| 1386 |
- Type: []string{"string"}, |
| 1387 |
- Format: "date-time", |
| 1388 |
- }, |
| 1389 |
- }, |
| 1390 |
- } |
| 1391 |
- } |
| 1392 |
- |
| 1393 |
- func (_ Blah) OpenAPISchemaType() []string { return []string{"string"} } |
| 1394 |
- func (_ Blah) OpenAPISchemaFormat() string { return "date-time" }` |
| 1395 |
- commonFile := `package common |
| 1396 |
- |
| 1397 |
- type OpenAPIDefinition struct {}` |
| 1398 |
- |
| 1399 |
- packagestest.TestAll(t, func(t *testing.T, x packagestest.Exporter) { |
| 1400 |
- e := packagestest.Export(t, x, []packagestest.Module{{ |
| 1401 |
- Name: "example.com/base/foo", |
| 1402 |
- Files: map[string]interface{}{ |
| 1403 |
- "foo.go": inputFile, |
| 1404 |
- }, |
| 1405 |
- }, { |
| 1406 |
- Name: "k8s.io/kube-openapi/pkg/common", |
| 1407 |
- Files: map[string]interface{}{ |
| 1408 |
- "common.go": commonFile, |
| 1409 |
- }, |
| 1410 |
- }}) |
| 1411 |
- defer e.Cleanup() |
| 1412 |
- |
| 1413 |
- callErr, funcErr, callBuffer, funcBuffer, _ := testOpenAPITypeWriter(t, e.Config) |
| 1414 |
- if callErr != nil { |
| 1415 |
- t.Fatal(callErr) |
| 1416 |
- } |
| 1417 |
- if funcErr != nil { |
| 1418 |
- t.Fatal(funcErr) |
| 1419 |
- } |
| 1420 |
- assertEqual(t, callBuffer.String(), |
| 1421 |
- `"example.com/base/foo.Blah": schema_examplecom_base_foo_Blah(ref),`) |
| 1422 |
- assertEqual(t, funcBuffer.String(), |
| 1423 |
- `func schema_examplecom_base_foo_Blah(ref common.ReferenceCallback) common.OpenAPIDefinition { |
| 1424 |
-return common.EmbedOpenAPIDefinitionIntoV2Extension(foo.Blah{}.OpenAPIV3Definition(), common.OpenAPIDefinition{ |
| 1425 |
-Schema: spec.Schema{ |
| 1426 |
-SchemaProps: spec.SchemaProps{ |
| 1427 |
-Description: "Blah is a custom type", |
| 1428 |
-Type:foo.Blah{}.OpenAPISchemaType(), |
| 1429 |
-Format:foo.Blah{}.OpenAPISchemaFormat(), |
| 1430 |
-}, |
| 1431 |
-}, |
| 1432 |
-}) |
| 1433 |
-}`) |
| 1434 |
- }) |
| 1435 |
-} |
| 1436 |
- |
| 1437 |
-func TestV3OneOfTypes(t *testing.T) { |
| 1438 |
- inputFile := ` |
| 1439 |
- package foo |
| 1440 |
- |
| 1441 |
- // Blah is a custom type |
| 1442 |
- type Blah struct { |
| 1443 |
- } |
| 1444 |
- |
| 1445 |
- func (_ Blah) OpenAPISchemaType() []string { return []string{"string"} } |
| 1446 |
- func (_ Blah) OpenAPISchemaFormat() string { return "date-time" } |
| 1447 |
- func (_ Blah) OpenAPIV3OneOfTypes() []string { return []string{"string", "number"} }` |
| 1448 |
- |
| 1449 |
- packagestest.TestAll(t, func(t *testing.T, x packagestest.Exporter) { |
| 1450 |
- e := packagestest.Export(t, x, []packagestest.Module{{ |
| 1451 |
- Name: "example.com/base/foo", |
| 1452 |
- Files: map[string]interface{}{ |
| 1453 |
- "foo.go": inputFile, |
| 1454 |
- }, |
| 1455 |
- }}) |
| 1456 |
- defer e.Cleanup() |
| 1457 |
- |
| 1458 |
- callErr, funcErr, callBuffer, funcBuffer, _ := testOpenAPITypeWriter(t, e.Config) |
| 1459 |
- if callErr != nil { |
| 1460 |
- t.Fatal(callErr) |
| 1461 |
- } |
| 1462 |
- if funcErr != nil { |
| 1463 |
- t.Fatal(funcErr) |
| 1464 |
- } |
| 1465 |
- assertEqual(t, callBuffer.String(), |
| 1466 |
- `"example.com/base/foo.Blah": schema_examplecom_base_foo_Blah(ref),`) |
| 1467 |
- assertEqual(t, funcBuffer.String(), |
| 1468 |
- `func schema_examplecom_base_foo_Blah(ref common.ReferenceCallback) common.OpenAPIDefinition { |
| 1469 |
-return common.EmbedOpenAPIDefinitionIntoV2Extension(common.OpenAPIDefinition{ |
| 1470 |
-Schema: spec.Schema{ |
| 1471 |
-SchemaProps: spec.SchemaProps{ |
| 1472 |
-Description: "Blah is a custom type", |
| 1473 |
-OneOf:common.GenerateOpenAPIV3OneOfSchema(foo.Blah{}.OpenAPIV3OneOfTypes()), |
| 1474 |
-Format:foo.Blah{}.OpenAPISchemaFormat(), |
| 1475 |
-}, |
| 1476 |
-}, |
| 1477 |
-},common.OpenAPIDefinition{ |
| 1478 |
-Schema: spec.Schema{ |
| 1479 |
-SchemaProps: spec.SchemaProps{ |
| 1480 |
-Description: "Blah is a custom type", |
| 1481 |
-Type:foo.Blah{}.OpenAPISchemaType(), |
| 1482 |
-Format:foo.Blah{}.OpenAPISchemaFormat(), |
| 1483 |
-}, |
| 1484 |
-}, |
| 1485 |
-}) |
| 1486 |
-}`) |
| 1487 |
- }) |
| 1488 |
-} |
| 1489 |
- |
| 1490 |
-func TestPointer(t *testing.T) { |
| 1491 |
- inputFile := ` |
| 1492 |
- package foo |
| 1493 |
- |
| 1494 |
- // PointerSample demonstrate pointer's properties |
| 1495 |
- type Blah struct { |
| 1496 |
- // A string pointer |
| 1497 |
- StringPointer *string |
| 1498 |
- // A struct pointer |
| 1499 |
- StructPointer *Blah |
| 1500 |
- // A slice pointer |
| 1501 |
- SlicePointer *[]string |
| 1502 |
- // A map pointer |
| 1503 |
- MapPointer *map[string]string |
| 1504 |
- }` |
| 1505 |
- packagestest.TestAll(t, func(t *testing.T, x packagestest.Exporter) { |
| 1506 |
- e := packagestest.Export(t, x, []packagestest.Module{{ |
| 1507 |
- Name: "example.com/base/foo", |
| 1508 |
- Files: map[string]interface{}{ |
| 1509 |
- "foo.go": inputFile, |
| 1510 |
- }, |
| 1511 |
- }}) |
| 1512 |
- defer e.Cleanup() |
| 1513 |
- |
| 1514 |
- callErr, funcErr, callBuffer, funcBuffer, _ := testOpenAPITypeWriter(t, e.Config) |
| 1515 |
- if callErr != nil { |
| 1516 |
- t.Fatal(callErr) |
| 1517 |
- } |
| 1518 |
- if funcErr != nil { |
| 1519 |
- t.Fatal(funcErr) |
| 1520 |
- } |
| 1521 |
- assertEqual(t, callBuffer.String(), |
| 1522 |
- `"example.com/base/foo.Blah": schema_examplecom_base_foo_Blah(ref),`) |
| 1523 |
- assertEqual(t, funcBuffer.String(), |
| 1524 |
- `func schema_examplecom_base_foo_Blah(ref common.ReferenceCallback) common.OpenAPIDefinition { |
| 1525 |
-return common.OpenAPIDefinition{ |
| 1526 |
-Schema: spec.Schema{ |
| 1527 |
-SchemaProps: spec.SchemaProps{ |
| 1528 |
-Description: "PointerSample demonstrate pointer's properties", |
| 1529 |
-Type: []string{"object"}, |
| 1530 |
-Properties: map[string]spec.Schema{ |
| 1531 |
-"StringPointer": { |
| 1532 |
-SchemaProps: spec.SchemaProps{ |
| 1533 |
-Description: "A string pointer", |
| 1534 |
-Type: []string{"string"}, |
| 1535 |
-Format: "", |
| 1536 |
-}, |
| 1537 |
-}, |
| 1538 |
-"StructPointer": { |
| 1539 |
-SchemaProps: spec.SchemaProps{ |
| 1540 |
-Description: "A struct pointer", |
| 1541 |
-Ref: ref("example.com/base/foo.Blah"), |
| 1542 |
-}, |
| 1543 |
-}, |
| 1544 |
-"SlicePointer": { |
| 1545 |
-SchemaProps: spec.SchemaProps{ |
| 1546 |
-Description: "A slice pointer", |
| 1547 |
-Type: []string{"array"}, |
| 1548 |
-Items: &spec.SchemaOrArray{ |
| 1549 |
-Schema: &spec.Schema{ |
| 1550 |
-SchemaProps: spec.SchemaProps{ |
| 1551 |
-Default: "", |
| 1552 |
-Type: []string{"string"}, |
| 1553 |
-Format: "", |
| 1554 |
-}, |
| 1555 |
-}, |
| 1556 |
-}, |
| 1557 |
-}, |
| 1558 |
-}, |
| 1559 |
-"MapPointer": { |
| 1560 |
-SchemaProps: spec.SchemaProps{ |
| 1561 |
-Description: "A map pointer", |
| 1562 |
-Type: []string{"object"}, |
| 1563 |
-AdditionalProperties: &spec.SchemaOrBool{ |
| 1564 |
-Allows: true, |
| 1565 |
-Schema: &spec.Schema{ |
| 1566 |
-SchemaProps: spec.SchemaProps{ |
| 1567 |
-Default: "", |
| 1568 |
-Type: []string{"string"}, |
| 1569 |
-Format: "", |
| 1570 |
-}, |
| 1571 |
-}, |
| 1572 |
-}, |
| 1573 |
-}, |
| 1574 |
-}, |
| 1575 |
-}, |
| 1576 |
-Required: []string{"StringPointer","StructPointer","SlicePointer","MapPointer"}, |
| 1577 |
-}, |
| 1578 |
-}, |
| 1579 |
-Dependencies: []string{ |
| 1580 |
-"example.com/base/foo.Blah",}, |
| 1581 |
-} |
| 1582 |
-}`) |
| 1583 |
- }) |
| 1584 |
-} |
| 1585 |
- |
| 1586 |
-func TestNestedLists(t *testing.T) { |
| 1587 |
- inputFile := ` |
| 1588 |
- package foo |
| 1589 |
- |
| 1590 |
- // Blah is a test. |
| 1591 |
- // +k8s:openapi-gen=true |
| 1592 |
- // +k8s:openapi-gen=x-kubernetes-type-tag:type_test |
| 1593 |
- type Blah struct { |
| 1594 |
- // Nested list |
| 1595 |
- NestedList [][]int64 |
| 1596 |
- }` |
| 1597 |
- packagestest.TestAll(t, func(t *testing.T, x packagestest.Exporter) { |
| 1598 |
- e := packagestest.Export(t, x, []packagestest.Module{{ |
| 1599 |
- Name: "example.com/base/foo", |
| 1600 |
- Files: map[string]interface{}{ |
| 1601 |
- "foo.go": inputFile, |
| 1602 |
- }, |
| 1603 |
- }}) |
| 1604 |
- defer e.Cleanup() |
| 1605 |
- |
| 1606 |
- callErr, funcErr, callBuffer, funcBuffer, _ := testOpenAPITypeWriter(t, e.Config) |
| 1607 |
- if callErr != nil { |
| 1608 |
- t.Fatal(callErr) |
| 1609 |
- } |
| 1610 |
- if funcErr != nil { |
| 1611 |
- t.Fatal(funcErr) |
| 1612 |
- } |
| 1613 |
- assertEqual(t, callBuffer.String(), |
| 1614 |
- `"example.com/base/foo.Blah": schema_examplecom_base_foo_Blah(ref),`) |
| 1615 |
- assertEqual(t, funcBuffer.String(), |
| 1616 |
- `func schema_examplecom_base_foo_Blah(ref common.ReferenceCallback) common.OpenAPIDefinition { |
| 1617 |
-return common.OpenAPIDefinition{ |
| 1618 |
-Schema: spec.Schema{ |
| 1619 |
-SchemaProps: spec.SchemaProps{ |
| 1620 |
-Description: "Blah is a test.", |
| 1621 |
-Type: []string{"object"}, |
| 1622 |
-Properties: map[string]spec.Schema{ |
| 1623 |
-"NestedList": { |
| 1624 |
-SchemaProps: spec.SchemaProps{ |
| 1625 |
-Description: "Nested list", |
| 1626 |
-Type: []string{"array"}, |
| 1627 |
-Items: &spec.SchemaOrArray{ |
| 1628 |
-Schema: &spec.Schema{ |
| 1629 |
-SchemaProps: spec.SchemaProps{ |
| 1630 |
-Type: []string{"array"}, |
| 1631 |
-Items: &spec.SchemaOrArray{ |
| 1632 |
-Schema: &spec.Schema{ |
| 1633 |
-SchemaProps: spec.SchemaProps{ |
| 1634 |
-Default: 0, |
| 1635 |
-Type: []string{"integer"}, |
| 1636 |
-Format: "int64", |
| 1637 |
-}, |
| 1638 |
-}, |
| 1639 |
-}, |
| 1640 |
-}, |
| 1641 |
-}, |
| 1642 |
-}, |
| 1643 |
-}, |
| 1644 |
-}, |
| 1645 |
-}, |
| 1646 |
-Required: []string{"NestedList"}, |
| 1647 |
-}, |
| 1648 |
-VendorExtensible: spec.VendorExtensible{ |
| 1649 |
-Extensions: spec.Extensions{ |
| 1650 |
-"x-kubernetes-type-tag": "type_test", |
| 1651 |
-}, |
| 1652 |
-}, |
| 1653 |
-}, |
| 1654 |
-} |
| 1655 |
-}`) |
| 1656 |
- }) |
| 1657 |
-} |
| 1658 |
- |
| 1659 |
-func TestNestListOfMaps(t *testing.T) { |
| 1660 |
- inputFile := ` |
| 1661 |
- package foo |
| 1662 |
- |
| 1663 |
- // Blah is a test. |
| 1664 |
- // +k8s:openapi-gen=true |
| 1665 |
- // +k8s:openapi-gen=x-kubernetes-type-tag:type_test |
| 1666 |
- type Blah struct { |
| 1667 |
- // Nested list of maps |
| 1668 |
- NestedListOfMaps [][]map[string]string |
| 1669 |
- }` |
| 1670 |
- packagestest.TestAll(t, func(t *testing.T, x packagestest.Exporter) { |
| 1671 |
- e := packagestest.Export(t, x, []packagestest.Module{{ |
| 1672 |
- Name: "example.com/base/foo", |
| 1673 |
- Files: map[string]interface{}{ |
| 1674 |
- "foo.go": inputFile, |
| 1675 |
- }, |
| 1676 |
- }}) |
| 1677 |
- defer e.Cleanup() |
| 1678 |
- |
| 1679 |
- callErr, funcErr, callBuffer, funcBuffer, _ := testOpenAPITypeWriter(t, e.Config) |
| 1680 |
- if callErr != nil { |
| 1681 |
- t.Fatal(callErr) |
| 1682 |
- } |
| 1683 |
- if funcErr != nil { |
| 1684 |
- t.Fatal(funcErr) |
| 1685 |
- } |
| 1686 |
- assertEqual(t, callBuffer.String(), |
| 1687 |
- `"example.com/base/foo.Blah": schema_examplecom_base_foo_Blah(ref),`) |
| 1688 |
- assertEqual(t, funcBuffer.String(), |
| 1689 |
- `func schema_examplecom_base_foo_Blah(ref common.ReferenceCallback) common.OpenAPIDefinition { |
| 1690 |
-return common.OpenAPIDefinition{ |
| 1691 |
-Schema: spec.Schema{ |
| 1692 |
-SchemaProps: spec.SchemaProps{ |
| 1693 |
-Description: "Blah is a test.", |
| 1694 |
-Type: []string{"object"}, |
| 1695 |
-Properties: map[string]spec.Schema{ |
| 1696 |
-"NestedListOfMaps": { |
| 1697 |
-SchemaProps: spec.SchemaProps{ |
| 1698 |
-Description: "Nested list of maps", |
| 1699 |
-Type: []string{"array"}, |
| 1700 |
-Items: &spec.SchemaOrArray{ |
| 1701 |
-Schema: &spec.Schema{ |
| 1702 |
-SchemaProps: spec.SchemaProps{ |
| 1703 |
-Type: []string{"array"}, |
| 1704 |
-Items: &spec.SchemaOrArray{ |
| 1705 |
-Schema: &spec.Schema{ |
| 1706 |
-SchemaProps: spec.SchemaProps{ |
| 1707 |
-Type: []string{"object"}, |
| 1708 |
-AdditionalProperties: &spec.SchemaOrBool{ |
| 1709 |
-Allows: true, |
| 1710 |
-Schema: &spec.Schema{ |
| 1711 |
-SchemaProps: spec.SchemaProps{ |
| 1712 |
-Default: "", |
| 1713 |
-Type: []string{"string"}, |
| 1714 |
-Format: "", |
| 1715 |
-}, |
| 1716 |
-}, |
| 1717 |
-}, |
| 1718 |
-}, |
| 1719 |
-}, |
| 1720 |
-}, |
| 1721 |
-}, |
| 1722 |
-}, |
| 1723 |
-}, |
| 1724 |
-}, |
| 1725 |
-}, |
| 1726 |
-}, |
| 1727 |
-Required: []string{"NestedListOfMaps"}, |
| 1728 |
-}, |
| 1729 |
-VendorExtensible: spec.VendorExtensible{ |
| 1730 |
-Extensions: spec.Extensions{ |
| 1731 |
-"x-kubernetes-type-tag": "type_test", |
| 1732 |
-}, |
| 1733 |
-}, |
| 1734 |
-}, |
| 1735 |
-} |
| 1736 |
-}`) |
| 1737 |
- }) |
| 1738 |
-} |
| 1739 |
- |
| 1740 |
-func TestExtensions(t *testing.T) { |
| 1741 |
- inputFile := ` |
| 1742 |
- package foo |
| 1743 |
- |
| 1744 |
- // Blah is a test. |
| 1745 |
- // +k8s:openapi-gen=true |
| 1746 |
- // +k8s:openapi-gen=x-kubernetes-type-tag:type_test |
| 1747 |
- type Blah struct { |
| 1748 |
- // a member with a list type with two map keys |
| 1749 |
- // +listType=map |
| 1750 |
- // +listMapKey=port |
| 1751 |
- // +listMapKey=protocol |
| 1752 |
- WithListField []string |
| 1753 |
- |
| 1754 |
- // another member with a list type with one map key |
| 1755 |
- // +listType=map |
| 1756 |
- // +listMapKey=port |
| 1757 |
- WithListField2 []string |
| 1758 |
- }` |
| 1759 |
- packagestest.TestAll(t, func(t *testing.T, x packagestest.Exporter) { |
| 1760 |
- e := packagestest.Export(t, x, []packagestest.Module{{ |
| 1761 |
- Name: "example.com/base/foo", |
| 1762 |
- Files: map[string]interface{}{ |
| 1763 |
- "foo.go": inputFile, |
| 1764 |
- }, |
| 1765 |
- }}) |
| 1766 |
- defer e.Cleanup() |
| 1767 |
- |
| 1768 |
- callErr, funcErr, callBuffer, funcBuffer, _ := testOpenAPITypeWriter(t, e.Config) |
| 1769 |
- if callErr != nil { |
| 1770 |
- t.Fatal(callErr) |
| 1771 |
- } |
| 1772 |
- if funcErr != nil { |
| 1773 |
- t.Fatal(funcErr) |
| 1774 |
- } |
| 1775 |
- assertEqual(t, callBuffer.String(), |
| 1776 |
- `"example.com/base/foo.Blah": schema_examplecom_base_foo_Blah(ref),`) |
| 1777 |
- assertEqual(t, funcBuffer.String(), |
| 1778 |
- `func schema_examplecom_base_foo_Blah(ref common.ReferenceCallback) common.OpenAPIDefinition { |
| 1779 |
-return common.OpenAPIDefinition{ |
| 1780 |
-Schema: spec.Schema{ |
| 1781 |
-SchemaProps: spec.SchemaProps{ |
| 1782 |
-Description: "Blah is a test.", |
| 1783 |
-Type: []string{"object"}, |
| 1784 |
-Properties: map[string]spec.Schema{ |
| 1785 |
-"WithListField": { |
| 1786 |
-VendorExtensible: spec.VendorExtensible{ |
| 1787 |
-Extensions: spec.Extensions{ |
| 1788 |
-"x-kubernetes-list-map-keys": []interface{}{ |
| 1789 |
-"port", |
| 1790 |
-"protocol", |
| 1791 |
-}, |
| 1792 |
-"x-kubernetes-list-type": "map", |
| 1793 |
-}, |
| 1794 |
-}, |
| 1795 |
-SchemaProps: spec.SchemaProps{ |
| 1796 |
-Description: "a member with a list type with two map keys", |
| 1797 |
-Type: []string{"array"}, |
| 1798 |
-Items: &spec.SchemaOrArray{ |
| 1799 |
-Schema: &spec.Schema{ |
| 1800 |
-SchemaProps: spec.SchemaProps{ |
| 1801 |
-Default: "", |
| 1802 |
-Type: []string{"string"}, |
| 1803 |
-Format: "", |
| 1804 |
-}, |
| 1805 |
-}, |
| 1806 |
-}, |
| 1807 |
-}, |
| 1808 |
-}, |
| 1809 |
-"WithListField2": { |
| 1810 |
-VendorExtensible: spec.VendorExtensible{ |
| 1811 |
-Extensions: spec.Extensions{ |
| 1812 |
-"x-kubernetes-list-map-keys": []interface{}{ |
| 1813 |
-"port", |
| 1814 |
-}, |
| 1815 |
-"x-kubernetes-list-type": "map", |
| 1816 |
-}, |
| 1817 |
-}, |
| 1818 |
-SchemaProps: spec.SchemaProps{ |
| 1819 |
-Description: "another member with a list type with one map key", |
| 1820 |
-Type: []string{"array"}, |
| 1821 |
-Items: &spec.SchemaOrArray{ |
| 1822 |
-Schema: &spec.Schema{ |
| 1823 |
-SchemaProps: spec.SchemaProps{ |
| 1824 |
-Default: "", |
| 1825 |
-Type: []string{"string"}, |
| 1826 |
-Format: "", |
| 1827 |
-}, |
| 1828 |
-}, |
| 1829 |
-}, |
| 1830 |
-}, |
| 1831 |
-}, |
| 1832 |
-}, |
| 1833 |
-Required: []string{"WithListField","WithListField2"}, |
| 1834 |
-}, |
| 1835 |
-VendorExtensible: spec.VendorExtensible{ |
| 1836 |
-Extensions: spec.Extensions{ |
| 1837 |
-"x-kubernetes-type-tag": "type_test", |
| 1838 |
-}, |
| 1839 |
-}, |
| 1840 |
-}, |
| 1841 |
-} |
| 1842 |
-}`) |
| 1843 |
- }) |
| 1844 |
-} |
| 1845 |
- |
| 1846 |
-func TestUnion(t *testing.T) { |
| 1847 |
- inputFile := ` |
| 1848 |
- package foo |
| 1849 |
- |
| 1850 |
- // Blah is a test. |
| 1851 |
- // +k8s:openapi-gen=true |
| 1852 |
- // +k8s:openapi-gen=x-kubernetes-type-tag:type_test |
| 1853 |
- // +union |
| 1854 |
- type Blah struct { |
| 1855 |
- // +unionDiscriminator |
| 1856 |
- Discriminator *string ` + "`" + `json:"discriminator"` + "`" + ` |
| 1857 |
- // +optional |
| 1858 |
- Numeric int ` + "`" + `json:"numeric"` + "`" + ` |
| 1859 |
- // +optional |
| 1860 |
- String string ` + "`" + `json:"string"` + "`" + ` |
| 1861 |
- // +optional |
| 1862 |
- Float float64 ` + "`" + `json:"float"` + "`" + ` |
| 1863 |
- }` |
| 1864 |
- packagestest.TestAll(t, func(t *testing.T, x packagestest.Exporter) { |
| 1865 |
- e := packagestest.Export(t, x, []packagestest.Module{{ |
| 1866 |
- Name: "example.com/base/foo", |
| 1867 |
- Files: map[string]interface{}{ |
| 1868 |
- "foo.go": inputFile, |
| 1869 |
- }, |
| 1870 |
- }}) |
| 1871 |
- defer e.Cleanup() |
| 1872 |
- |
| 1873 |
- callErr, funcErr, callBuffer, funcBuffer, _ := testOpenAPITypeWriter(t, e.Config) |
| 1874 |
- if callErr != nil { |
| 1875 |
- t.Fatal(callErr) |
| 1876 |
- } |
| 1877 |
- if funcErr != nil { |
| 1878 |
- t.Fatal(funcErr) |
| 1879 |
- } |
| 1880 |
- assertEqual(t, callBuffer.String(), |
| 1881 |
- `"example.com/base/foo.Blah": schema_examplecom_base_foo_Blah(ref),`) |
| 1882 |
- assertEqual(t, funcBuffer.String(), |
| 1883 |
- `func schema_examplecom_base_foo_Blah(ref common.ReferenceCallback) common.OpenAPIDefinition { |
| 1884 |
-return common.OpenAPIDefinition{ |
| 1885 |
-Schema: spec.Schema{ |
| 1886 |
-SchemaProps: spec.SchemaProps{ |
| 1887 |
-Description: "Blah is a test.", |
| 1888 |
-Type: []string{"object"}, |
| 1889 |
-Properties: map[string]spec.Schema{ |
| 1890 |
-"discriminator": { |
| 1891 |
-SchemaProps: spec.SchemaProps{ |
| 1892 |
-Type: []string{"string"}, |
| 1893 |
-Format: "", |
| 1894 |
-}, |
| 1895 |
-}, |
| 1896 |
-"numeric": { |
| 1897 |
-SchemaProps: spec.SchemaProps{ |
| 1898 |
-Default: 0, |
| 1899 |
-Type: []string{"integer"}, |
| 1900 |
-Format: "int32", |
| 1901 |
-}, |
| 1902 |
-}, |
| 1903 |
-"string": { |
| 1904 |
-SchemaProps: spec.SchemaProps{ |
| 1905 |
-Default: "", |
| 1906 |
-Type: []string{"string"}, |
| 1907 |
-Format: "", |
| 1908 |
-}, |
| 1909 |
-}, |
| 1910 |
-"float": { |
| 1911 |
-SchemaProps: spec.SchemaProps{ |
| 1912 |
-Default: 0, |
| 1913 |
-Type: []string{"number"}, |
| 1914 |
-Format: "double", |
| 1915 |
-}, |
| 1916 |
-}, |
| 1917 |
-}, |
| 1918 |
-Required: []string{"discriminator"}, |
| 1919 |
-}, |
| 1920 |
-VendorExtensible: spec.VendorExtensible{ |
| 1921 |
-Extensions: spec.Extensions{ |
| 1922 |
-"x-kubernetes-type-tag": "type_test", |
| 1923 |
-"x-kubernetes-unions": []interface{}{ |
| 1924 |
-map[string]interface{}{ |
| 1925 |
-"discriminator": "discriminator", |
| 1926 |
-"fields-to-discriminateBy": map[string]interface{}{ |
| 1927 |
-"float": "Float", |
| 1928 |
-"numeric": "Numeric", |
| 1929 |
-"string": "String", |
| 1930 |
-}, |
| 1931 |
-}, |
| 1932 |
-}, |
| 1933 |
-}, |
| 1934 |
-}, |
| 1935 |
-}, |
| 1936 |
-} |
| 1937 |
-}`) |
| 1938 |
- }) |
| 1939 |
-} |
| 1940 |
- |
| 1941 |
-func TestEnumAlias(t *testing.T) { |
| 1942 |
- inputFile := ` |
| 1943 |
- package foo |
| 1944 |
- |
| 1945 |
- import "example.com/base/bar" |
| 1946 |
- |
| 1947 |
- // EnumType is the enumType. |
| 1948 |
- // +enum |
| 1949 |
- type EnumType = bar.EnumType |
| 1950 |
- |
| 1951 |
- // EnumA is a. |
| 1952 |
- const EnumA EnumType = bar.EnumA |
| 1953 |
- // EnumB is b. |
| 1954 |
- const EnumB EnumType = bar.EnumB |
| 1955 |
- |
| 1956 |
- // Blah is a test. |
| 1957 |
- // +k8s:openapi-gen=true |
| 1958 |
- type Blah struct { |
| 1959 |
- // Value is the value. |
| 1960 |
- Value EnumType |
| 1961 |
- }` |
| 1962 |
- otherFile := ` |
| 1963 |
- package bar |
| 1964 |
- |
| 1965 |
- // EnumType is the enumType. |
| 1966 |
- // +enum |
| 1967 |
- type EnumType string |
| 1968 |
- |
| 1969 |
- // EnumA is a. |
| 1970 |
- const EnumA EnumType = "a" |
| 1971 |
- // EnumB is b. |
| 1972 |
- const EnumB EnumType = "b"` |
| 1973 |
- |
| 1974 |
- packagestest.TestAll(t, func(t *testing.T, x packagestest.Exporter) { |
| 1975 |
- e := packagestest.Export(t, x, []packagestest.Module{{ |
| 1976 |
- Name: "example.com/base/foo", |
| 1977 |
- Files: map[string]interface{}{ |
| 1978 |
- "foo.go": inputFile, |
| 1979 |
- }, |
| 1980 |
- }, { |
| 1981 |
- Name: "example.com/base/bar", |
| 1982 |
- Files: map[string]interface{}{ |
| 1983 |
- "bar.go": otherFile, |
| 1984 |
- }, |
| 1985 |
- }}) |
| 1986 |
- defer e.Cleanup() |
| 1987 |
- |
| 1988 |
- callErr, funcErr, _, funcBuffer, _ := testOpenAPITypeWriter(t, e.Config) |
| 1989 |
- if callErr != nil { |
| 1990 |
- t.Fatal(callErr) |
| 1991 |
- } |
| 1992 |
- if funcErr != nil { |
| 1993 |
- t.Fatal(funcErr) |
| 1994 |
- } |
| 1995 |
- assertEqual(t, funcBuffer.String(), |
| 1996 |
- `func schema_examplecom_base_foo_Blah(ref common.ReferenceCallback) common.OpenAPIDefinition { |
| 1997 |
-return common.OpenAPIDefinition{ |
| 1998 |
-Schema: spec.Schema{ |
| 1999 |
-SchemaProps: spec.SchemaProps{ |
| 2000 |
-Description: "Blah is a test.", |
| 2001 |
-Type: []string{"object"}, |
| 2002 |
-Properties: map[string]spec.Schema{ |
| 2003 |
-"Value": { |
| 2004 |
-SchemaProps: spec.SchemaProps{`+"\n"+ |
| 2005 |
- "Description: \"Value is the value.\\n\\nPossible enum values:\\n - `\\\"a\\\"` is a.\\n - `\\\"b\\\"` is b.\","+` |
| 2006 |
-Default: "", |
| 2007 |
-Type: []string{"string"}, |
| 2008 |
-Format: "", |
| 2009 |
-Enum: []interface{}{"a", "b"}, |
| 2010 |
-}, |
| 2011 |
-}, |
| 2012 |
-}, |
| 2013 |
-Required: []string{"Value"}, |
| 2014 |
-}, |
| 2015 |
-}, |
| 2016 |
-} |
| 2017 |
-}`) |
| 2018 |
- }) |
| 2019 |
-} |
| 2020 |
- |
| 2021 |
-func TestEnum(t *testing.T) { |
| 2022 |
- inputFile := ` |
| 2023 |
- package foo |
| 2024 |
- |
| 2025 |
- // EnumType is the enumType. |
| 2026 |
- // +enum |
| 2027 |
- type EnumType string |
| 2028 |
- |
| 2029 |
- // EnumA is a. |
| 2030 |
- const EnumA EnumType = "a" |
| 2031 |
- // EnumB is b. |
| 2032 |
- const EnumB EnumType = "b" |
| 2033 |
- |
| 2034 |
- // Blah is a test. |
| 2035 |
- // +k8s:openapi-gen=true |
| 2036 |
- // +k8s:openapi-gen=x-kubernetes-type-tag:type_test |
| 2037 |
- type Blah struct { |
| 2038 |
- // Value is the value. |
| 2039 |
- Value EnumType |
| 2040 |
- NoCommentEnum EnumType |
| 2041 |
- // +optional |
| 2042 |
- OptionalEnum *EnumType |
| 2043 |
- List []EnumType |
| 2044 |
- Map map[string]EnumType |
| 2045 |
- }` |
| 2046 |
- |
| 2047 |
- packagestest.TestAll(t, func(t *testing.T, x packagestest.Exporter) { |
| 2048 |
- e := packagestest.Export(t, x, []packagestest.Module{{ |
| 2049 |
- Name: "example.com/base/foo", |
| 2050 |
- Files: map[string]interface{}{ |
| 2051 |
- "foo.go": inputFile, |
| 2052 |
- }, |
| 2053 |
- }}) |
| 2054 |
- defer e.Cleanup() |
| 2055 |
- |
| 2056 |
- callErr, funcErr, _, funcBuffer, _ := testOpenAPITypeWriter(t, e.Config) |
| 2057 |
- if callErr != nil { |
| 2058 |
- t.Fatal(callErr) |
| 2059 |
- } |
| 2060 |
- if funcErr != nil { |
| 2061 |
- t.Fatal(funcErr) |
| 2062 |
- } |
| 2063 |
- assertEqual(t, funcBuffer.String(), |
| 2064 |
- `func schema_examplecom_base_foo_Blah(ref common.ReferenceCallback) common.OpenAPIDefinition { |
| 2065 |
-return common.OpenAPIDefinition{ |
| 2066 |
-Schema: spec.Schema{ |
| 2067 |
-SchemaProps: spec.SchemaProps{ |
| 2068 |
-Description: "Blah is a test.", |
| 2069 |
-Type: []string{"object"}, |
| 2070 |
-Properties: map[string]spec.Schema{ |
| 2071 |
-"Value": { |
| 2072 |
-SchemaProps: spec.SchemaProps{`+"\n"+ |
| 2073 |
- "Description: \"Value is the value.\\n\\nPossible enum values:\\n - `\\\"a\\\"` is a.\\n - `\\\"b\\\"` is b.\","+` |
| 2074 |
-Default: "", |
| 2075 |
-Type: []string{"string"}, |
| 2076 |
-Format: "", |
| 2077 |
-Enum: []interface{}{"a", "b"}, |
| 2078 |
-}, |
| 2079 |
-}, |
| 2080 |
-"NoCommentEnum": { |
| 2081 |
-SchemaProps: spec.SchemaProps{`+"\n"+ |
| 2082 |
- "Description: \"Possible enum values:\\n - `\\\"a\\\"` is a.\\n - `\\\"b\\\"` is b.\","+` |
| 2083 |
-Default: "", |
| 2084 |
-Type: []string{"string"}, |
| 2085 |
-Format: "", |
| 2086 |
-Enum: []interface{}{"a", "b"}, |
| 2087 |
-}, |
| 2088 |
-}, |
| 2089 |
-"OptionalEnum": { |
| 2090 |
-SchemaProps: spec.SchemaProps{`+"\n"+ |
| 2091 |
- "Description: \"Possible enum values:\\n - `\\\"a\\\"` is a.\\n - `\\\"b\\\"` is b.\","+` |
| 2092 |
-Type: []string{"string"}, |
| 2093 |
-Format: "", |
| 2094 |
-Enum: []interface{}{"a", "b"}, |
| 2095 |
-}, |
| 2096 |
-}, |
| 2097 |
-"List": { |
| 2098 |
-SchemaProps: spec.SchemaProps{ |
| 2099 |
-Type: []string{"array"}, |
| 2100 |
-Items: &spec.SchemaOrArray{ |
| 2101 |
-Schema: &spec.Schema{ |
| 2102 |
-SchemaProps: spec.SchemaProps{ |
| 2103 |
-Default: "", |
| 2104 |
-Type: []string{"string"}, |
| 2105 |
-Format: "", |
| 2106 |
-Enum: []interface{}{"a", "b"}, |
| 2107 |
-}, |
| 2108 |
-}, |
| 2109 |
-}, |
| 2110 |
-}, |
| 2111 |
-}, |
| 2112 |
-"Map": { |
| 2113 |
-SchemaProps: spec.SchemaProps{ |
| 2114 |
-Type: []string{"object"}, |
| 2115 |
-AdditionalProperties: &spec.SchemaOrBool{ |
| 2116 |
-Allows: true, |
| 2117 |
-Schema: &spec.Schema{ |
| 2118 |
-SchemaProps: spec.SchemaProps{ |
| 2119 |
-Default: "", |
| 2120 |
-Type: []string{"string"}, |
| 2121 |
-Format: "", |
| 2122 |
-Enum: []interface{}{"a", "b"}, |
| 2123 |
-}, |
| 2124 |
-}, |
| 2125 |
-}, |
| 2126 |
-}, |
| 2127 |
-}, |
| 2128 |
-}, |
| 2129 |
-Required: []string{"Value","NoCommentEnum","List","Map"}, |
| 2130 |
-}, |
| 2131 |
-VendorExtensible: spec.VendorExtensible{ |
| 2132 |
-Extensions: spec.Extensions{ |
| 2133 |
-"x-kubernetes-type-tag": "type_test", |
| 2134 |
-}, |
| 2135 |
-}, |
| 2136 |
-}, |
| 2137 |
-} |
| 2138 |
-}`) |
| 2139 |
- }) |
| 2140 |
-} |
| 2141 |
- |
| 2142 |
-func TestSymbolReference(t *testing.T) { |
| 2143 |
- inputFile := ` |
| 2144 |
- package foo |
| 2145 |
- |
| 2146 |
- // +k8s:openapi-gen=true |
| 2147 |
- type Blah struct { |
| 2148 |
- // +default="A Default Value" |
| 2149 |
- // +optional |
| 2150 |
- Value *string |
| 2151 |
- |
| 2152 |
- // User constant local to the output package fully qualified |
| 2153 |
- // +default=ref(example.com/base/output.MyConst) |
| 2154 |
- // +optional |
| 2155 |
- FullyQualifiedOutputValue *string |
| 2156 |
- |
| 2157 |
- // Local to types but not to output |
| 2158 |
- // +default=ref(MyConst) |
| 2159 |
- // +optional |
| 2160 |
- LocalValue *string |
| 2161 |
- |
| 2162 |
- // +default=ref(example.com/base/foo.MyConst) |
| 2163 |
- // +optional |
| 2164 |
- FullyQualifiedLocalValue *string |
| 2165 |
- |
| 2166 |
- // +default=ref(k8s.io/api/v1.TerminationPathDefault) |
| 2167 |
- // +optional |
| 2168 |
- FullyQualifiedExternalValue *string |
| 2169 |
- }` |
| 2170 |
- |
| 2171 |
- packagestest.TestAll(t, func(t *testing.T, x packagestest.Exporter) { |
| 2172 |
- e := packagestest.Export(t, x, []packagestest.Module{{ |
| 2173 |
- Name: "example.com/base/foo", |
| 2174 |
- Files: map[string]interface{}{ |
| 2175 |
- "foo.go": inputFile, |
| 2176 |
- }, |
| 2177 |
- }}) |
| 2178 |
- defer e.Cleanup() |
| 2179 |
- |
| 2180 |
- callErr, funcErr, _, funcBuffer, imports := testOpenAPITypeWriter(t, e.Config) |
| 2181 |
- if funcErr != nil { |
| 2182 |
- t.Fatalf("Unexpected funcErr: %v", funcErr) |
| 2183 |
- } |
| 2184 |
- if callErr != nil { |
| 2185 |
- t.Fatalf("Unexpected callErr: %v", callErr) |
| 2186 |
- } |
| 2187 |
- expImports := []string{ |
| 2188 |
- `foo "example.com/base/foo"`, |
| 2189 |
- `v1 "k8s.io/api/v1"`, |
| 2190 |
- `common "k8s.io/kube-openapi/pkg/common"`, |
| 2191 |
- `spec "k8s.io/kube-openapi/pkg/validation/spec"`, |
| 2192 |
- } |
| 2193 |
- if !cmp.Equal(imports, expImports) { |
| 2194 |
- t.Errorf("wrong imports:\n%s", cmp.Diff(expImports, imports)) |
| 2195 |
- } |
| 2196 |
- |
| 2197 |
- if formatted, err := format.Source(funcBuffer.Bytes()); err != nil { |
| 2198 |
- t.Fatal(err) |
| 2199 |
- } else { |
| 2200 |
- assertEqual(t, string(formatted), `func schema_examplecom_base_foo_Blah(ref common.ReferenceCallback) common.OpenAPIDefinition { |
| 2201 |
- return common.OpenAPIDefinition{ |
| 2202 |
- Schema: spec.Schema{ |
| 2203 |
- SchemaProps: spec.SchemaProps{ |
| 2204 |
- Type: []string{"object"}, |
| 2205 |
- Properties: map[string]spec.Schema{ |
| 2206 |
- "Value": { |
| 2207 |
- SchemaProps: spec.SchemaProps{ |
| 2208 |
- Default: "A Default Value", |
| 2209 |
- Type: []string{"string"}, |
| 2210 |
- Format: "", |
| 2211 |
- }, |
| 2212 |
- }, |
| 2213 |
- "FullyQualifiedOutputValue": { |
| 2214 |
- SchemaProps: spec.SchemaProps{ |
| 2215 |
- Description: "User constant local to the output package fully qualified", |
| 2216 |
- Default: MyConst, |
| 2217 |
- Type: []string{"string"}, |
| 2218 |
- Format: "", |
| 2219 |
- }, |
| 2220 |
- }, |
| 2221 |
- "LocalValue": { |
| 2222 |
- SchemaProps: spec.SchemaProps{ |
| 2223 |
- Description: "Local to types but not to output", |
| 2224 |
- Default: foo.MyConst, |
| 2225 |
- Type: []string{"string"}, |
| 2226 |
- Format: "", |
| 2227 |
- }, |
| 2228 |
- }, |
| 2229 |
- "FullyQualifiedLocalValue": { |
| 2230 |
- SchemaProps: spec.SchemaProps{ |
| 2231 |
- Default: foo.MyConst, |
| 2232 |
- Type: []string{"string"}, |
| 2233 |
- Format: "", |
| 2234 |
- }, |
| 2235 |
- }, |
| 2236 |
- "FullyQualifiedExternalValue": { |
| 2237 |
- SchemaProps: spec.SchemaProps{ |
| 2238 |
- Default: v1.TerminationPathDefault, |
| 2239 |
- Type: []string{"string"}, |
| 2240 |
- Format: "", |
| 2241 |
- }, |
| 2242 |
- }, |
| 2243 |
- }, |
| 2244 |
- }, |
| 2245 |
- }, |
| 2246 |
- } |
| 2247 |
-}`) |
| 2248 |
- } |
| 2249 |
- }) |
| 2250 |
-} |
| 2251 |
- |
| 2252 |
-// Show that types with unmarshalJSON in their hierarchy do not have struct |
| 2253 |
-// defaults enforced, and that aliases and embededd types are respected |
| 2254 |
-func TestMustEnforceDefaultStruct(t *testing.T) { |
| 2255 |
- inputFile := ` |
| 2256 |
- package foo |
| 2257 |
- |
| 2258 |
- type Time struct { |
| 2259 |
- value interface{} |
| 2260 |
- } |
| 2261 |
- |
| 2262 |
- |
| 2263 |
- type TimeWithoutUnmarshal struct { |
| 2264 |
- value interface{} |
| 2265 |
- } |
| 2266 |
- |
| 2267 |
- func (_ TimeWithoutUnmarshal) OpenAPISchemaType() []string { return []string{"string"} } |
| 2268 |
- func (_ TimeWithoutUnmarshal) OpenAPISchemaFormat() string { return "date-time" } |
| 2269 |
- |
| 2270 |
- func (_ Time) UnmarshalJSON([]byte) error { |
| 2271 |
- return nil |
| 2272 |
- } |
| 2273 |
- |
| 2274 |
- |
| 2275 |
- func (_ Time) OpenAPISchemaType() []string { return []string{"string"} } |
| 2276 |
- func (_ Time) OpenAPISchemaFormat() string { return "date-time" } |
| 2277 |
- |
| 2278 |
- // Time with UnmarshalJSON defined on pointer instead of struct |
| 2279 |
- type MicroTime struct { |
| 2280 |
- value interface{} |
| 2281 |
- } |
| 2282 |
- |
| 2283 |
- func (t *MicroTime) UnmarshalJSON([]byte) error { |
| 2284 |
- return nil |
| 2285 |
- } |
| 2286 |
- |
| 2287 |
- func (_ MicroTime) OpenAPISchemaType() []string { return []string{"string"} } |
| 2288 |
- func (_ MicroTime) OpenAPISchemaFormat() string { return "date-time" } |
| 2289 |
- |
| 2290 |
- type Int64 int64 |
| 2291 |
- |
| 2292 |
- type Duration struct { |
| 2293 |
- Int64 |
| 2294 |
- } |
| 2295 |
- |
| 2296 |
- func (_ Duration) OpenAPISchemaType() []string { return []string{"string"} } |
| 2297 |
- func (_ Duration) OpenAPISchemaFormat() string { return "" } |
| 2298 |
- |
| 2299 |
- type NothingSpecial struct { |
| 2300 |
- Field string |
| 2301 |
- } |
| 2302 |
- |
| 2303 |
- // +k8s:openapi-gen=true |
| 2304 |
- type Blah struct { |
| 2305 |
- Embedded Duration |
| 2306 |
- PointerUnmarshal MicroTime |
| 2307 |
- StructUnmarshal Time |
| 2308 |
- NoUnmarshal TimeWithoutUnmarshal |
| 2309 |
- Regular NothingSpecial |
| 2310 |
- }` |
| 2311 |
- |
| 2312 |
- packagestest.TestAll(t, func(t *testing.T, x packagestest.Exporter) { |
| 2313 |
- e := packagestest.Export(t, x, []packagestest.Module{{ |
| 2314 |
- Name: "example.com/base/foo", |
| 2315 |
- Files: map[string]interface{}{ |
| 2316 |
- "foo.go": inputFile, |
| 2317 |
- }, |
| 2318 |
- }}) |
| 2319 |
- defer e.Cleanup() |
| 2320 |
- |
| 2321 |
- callErr, funcErr, _, funcBuffer, imports := testOpenAPITypeWriter(t, e.Config) |
| 2322 |
- if funcErr != nil { |
| 2323 |
- t.Fatalf("Unexpected funcErr: %v", funcErr) |
| 2324 |
- } |
| 2325 |
- if callErr != nil { |
| 2326 |
- t.Fatalf("Unexpected callErr: %v", callErr) |
| 2327 |
- } |
| 2328 |
- expImports := []string{ |
| 2329 |
- `foo "example.com/base/foo"`, |
| 2330 |
- `common "k8s.io/kube-openapi/pkg/common"`, |
| 2331 |
- `spec "k8s.io/kube-openapi/pkg/validation/spec"`, |
| 2332 |
- } |
| 2333 |
- if !cmp.Equal(imports, expImports) { |
| 2334 |
- t.Errorf("wrong imports:\n%s", cmp.Diff(expImports, imports)) |
| 2335 |
- } |
| 2336 |
- |
| 2337 |
- if formatted, err := format.Source(funcBuffer.Bytes()); err != nil { |
| 2338 |
- t.Fatal(err) |
| 2339 |
- } else { |
| 2340 |
- assertEqual(t, string(formatted), `func schema_examplecom_base_foo_Blah(ref common.ReferenceCallback) common.OpenAPIDefinition { |
| 2341 |
- return common.OpenAPIDefinition{ |
| 2342 |
- Schema: spec.Schema{ |
| 2343 |
- SchemaProps: spec.SchemaProps{ |
| 2344 |
- Type: []string{"object"}, |
| 2345 |
- Properties: map[string]spec.Schema{ |
| 2346 |
- "Embedded": { |
| 2347 |
- SchemaProps: spec.SchemaProps{ |
| 2348 |
- Default: 0, |
| 2349 |
- Ref: ref("example.com/base/foo.Duration"), |
| 2350 |
- }, |
| 2351 |
- }, |
| 2352 |
- "PointerUnmarshal": { |
| 2353 |
- SchemaProps: spec.SchemaProps{ |
| 2354 |
- Ref: ref("example.com/base/foo.MicroTime"), |
| 2355 |
- }, |
| 2356 |
- }, |
| 2357 |
- "StructUnmarshal": { |
| 2358 |
- SchemaProps: spec.SchemaProps{ |
| 2359 |
- Ref: ref("example.com/base/foo.Time"), |
| 2360 |
- }, |
| 2361 |
- }, |
| 2362 |
- "NoUnmarshal": { |
| 2363 |
- SchemaProps: spec.SchemaProps{ |
| 2364 |
- Default: map[string]interface{}{}, |
| 2365 |
- Ref: ref("example.com/base/foo.TimeWithoutUnmarshal"), |
| 2366 |
- }, |
| 2367 |
- }, |
| 2368 |
- "Regular": { |
| 2369 |
- SchemaProps: spec.SchemaProps{ |
| 2370 |
- Default: map[string]interface{}{}, |
| 2371 |
- Ref: ref("example.com/base/foo.NothingSpecial"), |
| 2372 |
- }, |
| 2373 |
- }, |
| 2374 |
- }, |
| 2375 |
- Required: []string{"Embedded", "PointerUnmarshal", "StructUnmarshal", "NoUnmarshal", "Regular"}, |
| 2376 |
- }, |
| 2377 |
- }, |
| 2378 |
- Dependencies: []string{ |
| 2379 |
- "example.com/base/foo.Duration", "example.com/base/foo.MicroTime", "example.com/base/foo.NothingSpecial", "example.com/base/foo.Time", "example.com/base/foo.TimeWithoutUnmarshal"}, |
| 2380 |
- } |
| 2381 |
-}`) |
| 2382 |
- } |
| 2383 |
- }) |
| 2384 |
-} |
| 2385 |
- |
| 2386 |
-func TestMarkerComments(t *testing.T) { |
| 2387 |
- inputFile := ` |
| 2388 |
- package foo |
| 2389 |
- |
| 2390 |
- // +k8s:openapi-gen=true |
| 2391 |
- // +k8s:validation:maxProperties=10 |
| 2392 |
- // +k8s:validation:minProperties=1 |
| 2393 |
- type Blah struct { |
| 2394 |
- |
| 2395 |
- // Integer with min and max values |
| 2396 |
- // +k8s:validation:minimum=0 |
| 2397 |
- // +k8s:validation:maximum=10 |
| 2398 |
- // +k8s:validation:exclusiveMinimum |
| 2399 |
- // +k8s:validation:exclusiveMaximum |
| 2400 |
- IntValue int |
| 2401 |
- |
| 2402 |
- // String with min and max lengths |
| 2403 |
- // +k8s:validation:minLength=1 |
| 2404 |
- // +k8s:validation:maxLength=10 |
| 2405 |
- // +k8s:validation:pattern="^foo$[0-9]+" |
| 2406 |
- StringValue string |
| 2407 |
- |
| 2408 |
- // +k8s:validation:maxItems=10 |
| 2409 |
- // +k8s:validation:minItems=1 |
| 2410 |
- // +k8s:validation:uniqueItems |
| 2411 |
- ArrayValue []string |
| 2412 |
- |
| 2413 |
- // +k8s:validation:maxProperties=10 |
| 2414 |
- // +k8s:validation:minProperties=1 |
| 2415 |
- ObjValue map[string]interface{} |
| 2416 |
- }` |
| 2417 |
- |
| 2418 |
- packagestest.TestAll(t, func(t *testing.T, x packagestest.Exporter) { |
| 2419 |
- e := packagestest.Export(t, x, []packagestest.Module{{ |
| 2420 |
- Name: "example.com/base/foo", |
| 2421 |
- Files: map[string]interface{}{ |
| 2422 |
- "foo.go": inputFile, |
| 2423 |
- }, |
| 2424 |
- }}) |
| 2425 |
- defer e.Cleanup() |
| 2426 |
- |
| 2427 |
- callErr, funcErr, _, funcBuffer, imports := testOpenAPITypeWriter(t, e.Config) |
| 2428 |
- if funcErr != nil { |
| 2429 |
- t.Fatalf("Unexpected funcErr: %v", funcErr) |
| 2430 |
- } |
| 2431 |
- if callErr != nil { |
| 2432 |
- t.Fatalf("Unexpected callErr: %v", callErr) |
| 2433 |
- } |
| 2434 |
- expImports := []string{ |
| 2435 |
- `foo "example.com/base/foo"`, |
| 2436 |
- `common "k8s.io/kube-openapi/pkg/common"`, |
| 2437 |
- `spec "k8s.io/kube-openapi/pkg/validation/spec"`, |
| 2438 |
- `ptr "k8s.io/utils/ptr"`, |
| 2439 |
- } |
| 2440 |
- if !cmp.Equal(imports, expImports) { |
| 2441 |
- t.Errorf("wrong imports:\n%s", cmp.Diff(expImports, imports)) |
| 2442 |
- } |
| 2443 |
- |
| 2444 |
- if formatted, err := format.Source(funcBuffer.Bytes()); err != nil { |
| 2445 |
- t.Fatalf("%v\n%v", err, string(funcBuffer.Bytes())) |
| 2446 |
- } else { |
| 2447 |
- formatted_expected, ree := format.Source([]byte(`func schema_examplecom_base_foo_Blah(ref common.ReferenceCallback) common.OpenAPIDefinition { |
| 2448 |
- return common.OpenAPIDefinition{ |
| 2449 |
- Schema: spec.Schema{ |
| 2450 |
- SchemaProps: spec.SchemaProps{ |
| 2451 |
- Type: []string{"object"}, |
| 2452 |
- MinProperties: ptr.To[int64](1), |
| 2453 |
- MaxProperties: ptr.To[int64](10), |
| 2454 |
- Properties: map[string]spec.Schema{ |
| 2455 |
- "IntValue": { |
| 2456 |
- SchemaProps: spec.SchemaProps{ |
| 2457 |
- Description: "Integer with min and max values", |
| 2458 |
- Default: 0, |
| 2459 |
- Minimum: ptr.To[float64](0), |
| 2460 |
- Maximum: ptr.To[float64](10), |
| 2461 |
- ExclusiveMinimum: true, |
| 2462 |
- ExclusiveMaximum: true, |
| 2463 |
- Type: []string{"integer"}, |
| 2464 |
- Format: "int32", |
| 2465 |
- }, |
| 2466 |
- }, |
| 2467 |
- "StringValue": { |
| 2468 |
- SchemaProps: spec.SchemaProps{ |
| 2469 |
- Description: "String with min and max lengths", |
| 2470 |
- Default: "", |
| 2471 |
- MinLength: ptr.To[int64](1), |
| 2472 |
- MaxLength: ptr.To[int64](10), |
| 2473 |
- Pattern: "^foo$[0-9]+", |
| 2474 |
- Type: []string{"string"}, |
| 2475 |
- Format: "", |
| 2476 |
- }, |
| 2477 |
- }, |
| 2478 |
- "ArrayValue": { |
| 2479 |
- SchemaProps: spec.SchemaProps{ |
| 2480 |
- MinItems: ptr.To[int64](1), |
| 2481 |
- MaxItems: ptr.To[int64](10), |
| 2482 |
- UniqueItems: true, |
| 2483 |
- Type: []string{"array"}, |
| 2484 |
- Items: &spec.SchemaOrArray{ |
| 2485 |
- Schema: &spec.Schema{ |
| 2486 |
- SchemaProps: spec.SchemaProps{ |
| 2487 |
- Default: "", |
| 2488 |
- Type: []string{"string"}, |
| 2489 |
- Format: "", |
| 2490 |
- }, |
| 2491 |
- }, |
| 2492 |
- }, |
| 2493 |
- }, |
| 2494 |
- }, |
| 2495 |
- "ObjValue": { |
| 2496 |
- SchemaProps: spec.SchemaProps{ |
| 2497 |
- MinProperties: ptr.To[int64](1), |
| 2498 |
- MaxProperties: ptr.To[int64](10), |
| 2499 |
- Type: []string{"object"}, |
| 2500 |
- AdditionalProperties: &spec.SchemaOrBool{ |
| 2501 |
- Allows: true, |
| 2502 |
- Schema: &spec.Schema{ |
| 2503 |
- SchemaProps: spec.SchemaProps{ |
| 2504 |
- Type: []string{"object"}, |
| 2505 |
- Format: "", |
| 2506 |
- }, |
| 2507 |
- }, |
| 2508 |
- }, |
| 2509 |
- }, |
| 2510 |
- }, |
| 2511 |
- }, |
| 2512 |
- Required: []string{"IntValue", "StringValue", "ArrayValue", "ObjValue"}, |
| 2513 |
- }, |
| 2514 |
- }, |
| 2515 |
- } |
| 2516 |
- }`)) |
| 2517 |
- if ree != nil { |
| 2518 |
- t.Fatal(ree) |
| 2519 |
- } |
| 2520 |
- assertEqual(t, string(formatted), string(formatted_expected)) |
| 2521 |
- } |
| 2522 |
- }) |
| 2523 |
-} |
| 2524 |
- |
| 2525 |
-func TestCELMarkerComments(t *testing.T) { |
| 2526 |
- inputFile := ` |
| 2527 |
- package foo |
| 2528 |
- |
| 2529 |
- // +k8s:openapi-gen=true |
| 2530 |
- // +k8s:validation:cel[0]:rule="self == oldSelf" |
| 2531 |
- // +k8s:validation:cel[0]:message="message1" |
| 2532 |
- type Blah struct { |
| 2533 |
- // +k8s:validation:cel[0]:rule="self.length() > 0" |
| 2534 |
- // +k8s:validation:cel[0]:message="string message" |
| 2535 |
- // +k8s:validation:cel[1]:rule="self.length() % 2 == 0" |
| 2536 |
- // +k8s:validation:cel[1]:messageExpression="self + ' hello'" |
| 2537 |
- // +k8s:validation:cel[1]:optionalOldSelf |
| 2538 |
- // +optional |
| 2539 |
- Field string |
| 2540 |
- }` |
| 2541 |
- |
| 2542 |
- packagestest.TestAll(t, func(t *testing.T, x packagestest.Exporter) { |
| 2543 |
- e := packagestest.Export(t, x, []packagestest.Module{{ |
| 2544 |
- Name: "example.com/base/foo", |
| 2545 |
- Files: map[string]interface{}{ |
| 2546 |
- "foo.go": inputFile, |
| 2547 |
- }, |
| 2548 |
- }}) |
| 2549 |
- defer e.Cleanup() |
| 2550 |
- |
| 2551 |
- callErr, funcErr, _, funcBuffer, imports := testOpenAPITypeWriter(t, e.Config) |
| 2552 |
- if funcErr != nil { |
| 2553 |
- t.Fatalf("Unexpected funcErr: %v", funcErr) |
| 2554 |
- } |
| 2555 |
- if callErr != nil { |
| 2556 |
- t.Fatalf("Unexpected callErr: %v", callErr) |
| 2557 |
- } |
| 2558 |
- expImports := []string{ |
| 2559 |
- `foo "example.com/base/foo"`, |
| 2560 |
- `common "k8s.io/kube-openapi/pkg/common"`, |
| 2561 |
- `spec "k8s.io/kube-openapi/pkg/validation/spec"`, |
| 2562 |
- } |
| 2563 |
- if !cmp.Equal(imports, expImports) { |
| 2564 |
- t.Errorf("wrong imports:\n%s", cmp.Diff(expImports, imports)) |
| 2565 |
- } |
| 2566 |
- |
| 2567 |
- if formatted, err := format.Source(funcBuffer.Bytes()); err != nil { |
| 2568 |
- t.Fatalf("%v\n%v", err, string(funcBuffer.Bytes())) |
| 2569 |
- } else { |
| 2570 |
- formatted_expected, ree := format.Source([]byte(`func schema_examplecom_base_foo_Blah(ref common.ReferenceCallback) common.OpenAPIDefinition { |
| 2571 |
- return common.OpenAPIDefinition{ |
| 2572 |
- Schema: spec.Schema{ |
| 2573 |
- SchemaProps: spec.SchemaProps{ |
| 2574 |
- Type: []string{"object"}, |
| 2575 |
- Properties: map[string]spec.Schema{ |
| 2576 |
- "Field": { |
| 2577 |
- VendorExtensible: spec.VendorExtensible{ |
| 2578 |
- Extensions: spec.Extensions{ |
| 2579 |
- "x-kubernetes-validations": []interface{}{map[string]interface{}{"message": "string message", "rule": "self.length() > 0"}, map[string]interface{}{"messageExpression": "self + ' hello'", "optionalOldSelf": true, "rule": "self.length() % 2 == 0"}}, |
| 2580 |
- }, |
| 2581 |
- }, |
| 2582 |
- SchemaProps: spec.SchemaProps{ |
| 2583 |
- Default: "", |
| 2584 |
- Type: []string{"string"}, |
| 2585 |
- Format: "", |
| 2586 |
- }, |
| 2587 |
- }, |
| 2588 |
- }, |
| 2589 |
- }, |
| 2590 |
- VendorExtensible: spec.VendorExtensible{ |
| 2591 |
- Extensions: spec.Extensions{ |
| 2592 |
- "x-kubernetes-validations": []interface{}{map[string]interface{}{"message": "message1", "rule": "self == oldSelf"}}, |
| 2593 |
- }, |
| 2594 |
- }, |
| 2595 |
- }, |
| 2596 |
- } |
| 2597 |
- }`)) |
| 2598 |
- if ree != nil { |
| 2599 |
- t.Fatal(ree) |
| 2600 |
- } |
| 2601 |
- assertEqual(t, string(formatted_expected), string(formatted)) |
| 2602 |
- } |
| 2603 |
- }) |
| 2604 |
-} |
| 2605 |
- |
| 2606 |
-func TestMultilineCELMarkerComments(t *testing.T) { |
| 2607 |
- inputFile := ` |
| 2608 |
- package foo |
| 2609 |
- |
| 2610 |
- // +k8s:openapi-gen=true |
| 2611 |
- // +k8s:validation:cel[0]:rule="self == oldSelf" |
| 2612 |
- // +k8s:validation:cel[0]:message="message1" |
| 2613 |
- // +k8s:validation:cel[0]:fieldPath="field" |
| 2614 |
- type Blah struct { |
| 2615 |
- // +k8s:validation:cel[0]:rule="self.length() > 0" |
| 2616 |
- // +k8s:validation:cel[0]:message="string message" |
| 2617 |
- // +k8s:validation:cel[0]:reason="Invalid" |
| 2618 |
- // +k8s:validation:cel[1]:rule> !oldSelf.hasValue() || self.length() % 2 == 0 |
| 2619 |
- // +k8s:validation:cel[1]:rule> ? self.field == "even" |
| 2620 |
- // +k8s:validation:cel[1]:rule> : self.field == "odd" |
| 2621 |
- // +k8s:validation:cel[1]:messageExpression="field must be whether the length of the string is even or odd" |
| 2622 |
- // +k8s:validation:cel[1]:optionalOldSelf |
| 2623 |
- // +optional |
| 2624 |
- Field string |
| 2625 |
- }` |
| 2626 |
- |
| 2627 |
- packagestest.TestAll(t, func(t *testing.T, x packagestest.Exporter) { |
| 2628 |
- e := packagestest.Export(t, x, []packagestest.Module{{ |
| 2629 |
- Name: "example.com/base/foo", |
| 2630 |
- Files: map[string]interface{}{ |
| 2631 |
- "foo.go": inputFile, |
| 2632 |
- }, |
| 2633 |
- }}) |
| 2634 |
- defer e.Cleanup() |
| 2635 |
- |
| 2636 |
- callErr, funcErr, _, funcBuffer, imports := testOpenAPITypeWriter(t, e.Config) |
| 2637 |
- if funcErr != nil { |
| 2638 |
- t.Fatalf("Unexpected funcErr: %v", funcErr) |
| 2639 |
- } |
| 2640 |
- if callErr != nil { |
| 2641 |
- t.Fatalf("Unexpected callErr: %v", callErr) |
| 2642 |
- } |
| 2643 |
- expImports := []string{ |
| 2644 |
- `foo "example.com/base/foo"`, |
| 2645 |
- `common "k8s.io/kube-openapi/pkg/common"`, |
| 2646 |
- `spec "k8s.io/kube-openapi/pkg/validation/spec"`, |
| 2647 |
- } |
| 2648 |
- if !cmp.Equal(imports, expImports) { |
| 2649 |
- t.Errorf("wrong imports:\n%s", cmp.Diff(expImports, imports)) |
| 2650 |
- } |
| 2651 |
- |
| 2652 |
- if formatted, err := format.Source(funcBuffer.Bytes()); err != nil { |
| 2653 |
- t.Fatalf("%v\n%v", err, string(funcBuffer.Bytes())) |
| 2654 |
- } else { |
| 2655 |
- formatted_expected, ree := format.Source([]byte(`func schema_examplecom_base_foo_Blah(ref common.ReferenceCallback) common.OpenAPIDefinition { |
| 2656 |
- return common.OpenAPIDefinition{ |
| 2657 |
- Schema: spec.Schema{ |
| 2658 |
- SchemaProps: spec.SchemaProps{ |
| 2659 |
- Type: []string{"object"}, |
| 2660 |
- Properties: map[string]spec.Schema{ |
| 2661 |
- "Field": { |
| 2662 |
- VendorExtensible: spec.VendorExtensible{ |
| 2663 |
- Extensions: spec.Extensions{ |
| 2664 |
- "x-kubernetes-validations": []interface{}{map[string]interface{}{"message": "string message", "reason": "Invalid", "rule": "self.length() > 0"}, map[string]interface{}{"messageExpression": "field must be whether the length of the string is even or odd", "optionalOldSelf": true, "rule": "!oldSelf.hasValue() || self.length() % 2 == 0\n? self.field == \"even\"\n: self.field == \"odd\""}}, |
| 2665 |
- }, |
| 2666 |
- }, |
| 2667 |
- SchemaProps: spec.SchemaProps{ |
| 2668 |
- Default: "", |
| 2669 |
- Type: []string{"string"}, |
| 2670 |
- Format: "", |
| 2671 |
- }, |
| 2672 |
- }, |
| 2673 |
- }, |
| 2674 |
- }, |
| 2675 |
- VendorExtensible: spec.VendorExtensible{ |
| 2676 |
- Extensions: spec.Extensions{ |
| 2677 |
- "x-kubernetes-validations": []interface{}{map[string]interface{}{"fieldPath": "field", "message": "message1", "rule": "self == oldSelf"}}, |
| 2678 |
- }, |
| 2679 |
- }, |
| 2680 |
- }, |
| 2681 |
- } |
| 2682 |
- }`)) |
| 2683 |
- if ree != nil { |
| 2684 |
- t.Fatal(ree) |
| 2685 |
- } |
| 2686 |
- assertEqual(t, string(formatted_expected), string(formatted)) |
| 2687 |
- } |
| 2688 |
- }) |
| 2689 |
-} |
| 2690 |
- |
| 2691 |
-func TestRequired(t *testing.T) { |
| 2692 |
- inputFile := ` |
| 2693 |
- package foo |
| 2694 |
- |
| 2695 |
- // +k8s:openapi-gen=true |
| 2696 |
- type Blah struct { |
| 2697 |
- // +optional |
| 2698 |
- OptionalField string |
| 2699 |
- |
| 2700 |
- // +required |
| 2701 |
- RequiredField string |
| 2702 |
- |
| 2703 |
- // +required |
| 2704 |
- RequiredPointerField *string ` + "`json:\"requiredPointerField,omitempty\"`" + ` |
| 2705 |
- |
| 2706 |
- // +optional |
| 2707 |
- OptionalPointerField *string ` + "`json:\"optionalPointerField,omitempty\"`" + ` |
| 2708 |
- |
| 2709 |
- ImplicitlyRequiredField string |
| 2710 |
- ImplicitlyOptionalField string ` + "`json:\"implicitlyOptionalField,omitempty\"`" + ` |
| 2711 |
- }` |
| 2712 |
- |
| 2713 |
- packagestest.TestAll(t, func(t *testing.T, x packagestest.Exporter) { |
| 2714 |
- e := packagestest.Export(t, x, []packagestest.Module{{ |
| 2715 |
- Name: "example.com/base/foo", |
| 2716 |
- Files: map[string]interface{}{ |
| 2717 |
- "foo.go": inputFile, |
| 2718 |
- }, |
| 2719 |
- }}) |
| 2720 |
- defer e.Cleanup() |
| 2721 |
- |
| 2722 |
- callErr, funcErr, _, funcBuffer, imports := testOpenAPITypeWriter(t, e.Config) |
| 2723 |
- if funcErr != nil { |
| 2724 |
- t.Fatalf("Unexpected funcErr: %v", funcErr) |
| 2725 |
- } |
| 2726 |
- if callErr != nil { |
| 2727 |
- t.Fatalf("Unexpected callErr: %v", callErr) |
| 2728 |
- } |
| 2729 |
- expImports := []string{ |
| 2730 |
- `foo "example.com/base/foo"`, |
| 2731 |
- `common "k8s.io/kube-openapi/pkg/common"`, |
| 2732 |
- `spec "k8s.io/kube-openapi/pkg/validation/spec"`, |
| 2733 |
- } |
| 2734 |
- if !cmp.Equal(imports, expImports) { |
| 2735 |
- t.Errorf("wrong imports:\n%s", cmp.Diff(expImports, imports)) |
| 2736 |
- } |
| 2737 |
- |
| 2738 |
- if formatted, err := format.Source(funcBuffer.Bytes()); err != nil { |
| 2739 |
- t.Fatalf("%v\n%v", err, string(funcBuffer.Bytes())) |
| 2740 |
- } else { |
| 2741 |
- formatted_expected, ree := format.Source([]byte(`func schema_examplecom_base_foo_Blah(ref common.ReferenceCallback) common.OpenAPIDefinition { |
| 2742 |
- return common.OpenAPIDefinition{ |
| 2743 |
- Schema: spec.Schema{ |
| 2744 |
- SchemaProps: spec.SchemaProps{ |
| 2745 |
- Type: []string{"object"}, |
| 2746 |
- Properties: map[string]spec.Schema{ |
| 2747 |
- "OptionalField": { |
| 2748 |
- SchemaProps: spec.SchemaProps{ |
| 2749 |
- Default: "", |
| 2750 |
- Type: []string{"string"}, |
| 2751 |
- Format: "", |
| 2752 |
- }, |
| 2753 |
- }, |
| 2754 |
- "RequiredField": { |
| 2755 |
- SchemaProps: spec.SchemaProps{ |
| 2756 |
- Default: "", |
| 2757 |
- Type: []string{"string"}, |
| 2758 |
- Format: "", |
| 2759 |
- }, |
| 2760 |
- }, |
| 2761 |
- "requiredPointerField": { |
| 2762 |
- SchemaProps: spec.SchemaProps{ |
| 2763 |
- Type: []string{"string"}, |
| 2764 |
- Format: "", |
| 2765 |
- }, |
| 2766 |
- }, |
| 2767 |
- "optionalPointerField": { |
| 2768 |
- SchemaProps: spec.SchemaProps{ |
| 2769 |
- Type: []string{"string"}, |
| 2770 |
- Format: "", |
| 2771 |
- }, |
| 2772 |
- }, |
| 2773 |
- "ImplicitlyRequiredField": { |
| 2774 |
- SchemaProps: spec.SchemaProps{ |
| 2775 |
- Default: "", |
| 2776 |
- Type: []string{"string"}, |
| 2777 |
- Format: "", |
| 2778 |
- }, |
| 2779 |
- }, |
| 2780 |
- "implicitlyOptionalField": { |
| 2781 |
- SchemaProps: spec.SchemaProps{ |
| 2782 |
- Type: []string{"string"}, |
| 2783 |
- Format: "", |
| 2784 |
- }, |
| 2785 |
- }, |
| 2786 |
- }, |
| 2787 |
- Required: []string{"RequiredField", "requiredPointerField", "ImplicitlyRequiredField"}, |
| 2788 |
- }, |
| 2789 |
- }, |
| 2790 |
- } |
| 2791 |
- }`)) |
| 2792 |
- if ree != nil { |
| 2793 |
- t.Fatal(ree) |
| 2794 |
- } |
| 2795 |
- assertEqual(t, string(formatted_expected), string(formatted)) |
| 2796 |
- } |
| 2797 |
- }) |
| 2798 |
- |
| 2799 |
- // Show specifying both is an error |
| 2800 |
- badFile := ` |
| 2801 |
- package foo |
| 2802 |
- |
| 2803 |
- // +k8s:openapi-gen=true |
| 2804 |
- type Blah struct { |
| 2805 |
- // +optional |
| 2806 |
- // +required |
| 2807 |
- ConfusingField string |
| 2808 |
- }` |
| 2809 |
- packagestest.TestAll(t, func(t *testing.T, x packagestest.Exporter) { |
| 2810 |
- e := packagestest.Export(t, x, []packagestest.Module{{ |
| 2811 |
- Name: "example.com/base/foo", |
| 2812 |
- Files: map[string]interface{}{ |
| 2813 |
- "foo.go": badFile, |
| 2814 |
- }, |
| 2815 |
- }}) |
| 2816 |
- defer e.Cleanup() |
| 2817 |
- |
| 2818 |
- callErr, funcErr, _, _, _ := testOpenAPITypeWriter(t, e.Config) |
| 2819 |
- if callErr != nil { |
| 2820 |
- t.Errorf("Unexpected callErr: %v", callErr) |
| 2821 |
- } |
| 2822 |
- if funcErr == nil { |
| 2823 |
- t.Fatalf("Expected funcErr") |
| 2824 |
- } |
| 2825 |
- if !strings.Contains(funcErr.Error(), "cannot be both optional and required") { |
| 2826 |
- t.Errorf("Unexpected error: %v", funcErr) |
| 2827 |
- } |
| 2828 |
- }) |
| 2829 |
-} |
| 2830 |
- |
| 2831 |
-func TestMarkerCommentsCustomDefsV3(t *testing.T) { |
| 2832 |
- inputFile := ` |
| 2833 |
- package foo |
| 2834 |
- |
| 2835 |
- import openapi "k8s.io/kube-openapi/pkg/common" |
| 2836 |
- |
| 2837 |
- // +k8s:validation:maxProperties=10 |
| 2838 |
- type Blah struct { |
| 2839 |
- } |
| 2840 |
- |
| 2841 |
- func (_ Blah) OpenAPIV3Definition() openapi.OpenAPIDefinition { |
| 2842 |
- return openapi.OpenAPIDefinition{ |
| 2843 |
- Schema: spec.Schema{ |
| 2844 |
- SchemaProps: spec.SchemaProps{ |
| 2845 |
- Type: []string{"object"}, |
| 2846 |
- MaxProperties: ptr.To[int64](10), |
| 2847 |
- Format: "ipv4", |
| 2848 |
- }, |
| 2849 |
- }, |
| 2850 |
- } |
| 2851 |
- } |
| 2852 |
- |
| 2853 |
- func (_ Blah) OpenAPISchemaType() []string { return []string{"object"} } |
| 2854 |
- func (_ Blah) OpenAPISchemaFormat() string { return "ipv4" }` |
| 2855 |
- commonFile := `package common |
| 2856 |
- |
| 2857 |
- type OpenAPIDefinition struct {}` |
| 2858 |
- |
| 2859 |
- packagestest.TestAll(t, func(t *testing.T, x packagestest.Exporter) { |
| 2860 |
- e := packagestest.Export(t, x, []packagestest.Module{{ |
| 2861 |
- Name: "example.com/base/foo", |
| 2862 |
- Files: map[string]interface{}{ |
| 2863 |
- "foo.go": inputFile, |
| 2864 |
- }, |
| 2865 |
- }, { |
| 2866 |
- Name: "k8s.io/kube-openapi/pkg/common", |
| 2867 |
- Files: map[string]interface{}{ |
| 2868 |
- "common.go": commonFile, |
| 2869 |
- }, |
| 2870 |
- }}) |
| 2871 |
- defer e.Cleanup() |
| 2872 |
- |
| 2873 |
- callErr, funcErr, callBuffer, funcBuffer, _ := testOpenAPITypeWriter(t, e.Config) |
| 2874 |
- if callErr != nil { |
| 2875 |
- t.Fatal(callErr) |
| 2876 |
- } |
| 2877 |
- if funcErr != nil { |
| 2878 |
- t.Fatal(funcErr) |
| 2879 |
- } |
| 2880 |
- assertEqual(t, callBuffer.String(), |
| 2881 |
- `"example.com/base/foo.Blah": schema_examplecom_base_foo_Blah(ref),`) |
| 2882 |
- assertEqual(t, funcBuffer.String(), |
| 2883 |
- `func schema_examplecom_base_foo_Blah(ref common.ReferenceCallback) common.OpenAPIDefinition { |
| 2884 |
-return common.EmbedOpenAPIDefinitionIntoV2Extension(foo.Blah{}.OpenAPIV3Definition(), common.OpenAPIDefinition{ |
| 2885 |
-Schema: spec.Schema{ |
| 2886 |
-SchemaProps: spec.SchemaProps{ |
| 2887 |
-Type:foo.Blah{}.OpenAPISchemaType(), |
| 2888 |
-Format:foo.Blah{}.OpenAPISchemaFormat(), |
| 2889 |
-MaxProperties: ptr.To[int64](10), |
| 2890 |
-}, |
| 2891 |
-}, |
| 2892 |
-}) |
| 2893 |
-}`) |
| 2894 |
- }) |
| 2895 |
-} |
| 2896 |
- |
| 2897 |
-func TestMarkerCommentsV3OneOfTypes(t *testing.T) { |
| 2898 |
- inputFile := ` |
| 2899 |
- package foo |
| 2900 |
- |
| 2901 |
- // +k8s:validation:maxLength=10 |
| 2902 |
- type Blah struct { |
| 2903 |
- } |
| 2904 |
- |
| 2905 |
- func (_ Blah) OpenAPISchemaType() []string { return []string{"string"} } |
| 2906 |
- func (_ Blah) OpenAPIV3OneOfTypes() []string { return []string{"string", "array"} } |
| 2907 |
- func (_ Blah) OpenAPISchemaFormat() string { return "ipv4" }` |
| 2908 |
- packagestest.TestAll(t, func(t *testing.T, x packagestest.Exporter) { |
| 2909 |
- e := packagestest.Export(t, x, []packagestest.Module{{ |
| 2910 |
- Name: "example.com/base/foo", |
| 2911 |
- Files: map[string]interface{}{ |
| 2912 |
- "foo.go": inputFile, |
| 2913 |
- }, |
| 2914 |
- }}) |
| 2915 |
- defer e.Cleanup() |
| 2916 |
- |
| 2917 |
- callErr, funcErr, callBuffer, funcBuffer, _ := testOpenAPITypeWriter(t, e.Config) |
| 2918 |
- if callErr != nil { |
| 2919 |
- t.Fatal(callErr) |
| 2920 |
- } |
| 2921 |
- if funcErr != nil { |
| 2922 |
- t.Fatal(funcErr) |
| 2923 |
- } |
| 2924 |
- assertEqual(t, callBuffer.String(), |
| 2925 |
- `"example.com/base/foo.Blah": schema_examplecom_base_foo_Blah(ref),`) |
| 2926 |
- assertEqual(t, funcBuffer.String(), |
| 2927 |
- `func schema_examplecom_base_foo_Blah(ref common.ReferenceCallback) common.OpenAPIDefinition { |
| 2928 |
-return common.EmbedOpenAPIDefinitionIntoV2Extension(common.OpenAPIDefinition{ |
| 2929 |
-Schema: spec.Schema{ |
| 2930 |
-SchemaProps: spec.SchemaProps{ |
| 2931 |
-OneOf:common.GenerateOpenAPIV3OneOfSchema(foo.Blah{}.OpenAPIV3OneOfTypes()), |
| 2932 |
-Format:foo.Blah{}.OpenAPISchemaFormat(), |
| 2933 |
-MaxLength: ptr.To[int64](10), |
| 2934 |
-}, |
| 2935 |
-}, |
| 2936 |
-},common.OpenAPIDefinition{ |
| 2937 |
-Schema: spec.Schema{ |
| 2938 |
-SchemaProps: spec.SchemaProps{ |
| 2939 |
-Type:foo.Blah{}.OpenAPISchemaType(), |
| 2940 |
-Format:foo.Blah{}.OpenAPISchemaFormat(), |
| 2941 |
-MaxLength: ptr.To[int64](10), |
| 2942 |
-}, |
| 2943 |
-}, |
| 2944 |
-}) |
| 2945 |
-}`) |
| 2946 |
- }) |
| 2947 |
-} |
| 2948 |
- |
| 2949 |
-func TestNestedMarkers(t *testing.T) { |
| 2950 |
- inputFile := ` |
| 2951 |
- package foo |
| 2952 |
- |
| 2953 |
- // +k8s:openapi-gen=true |
| 2954 |
- // +k8s:validation:properties:field:items:maxLength=10 |
| 2955 |
- // +k8s:validation:properties:aliasMap:additionalProperties:pattern>^foo$ |
| 2956 |
- type Blah struct { |
| 2957 |
- // +k8s:validation:items:cel[0]:rule="self.length() % 2 == 0" |
| 2958 |
- Field MyAlias ` + "`json:\"field,omitempty\"`" + ` |
| 2959 |
- |
| 2960 |
- // +k8s:validation:additionalProperties:maxLength=10 |
| 2961 |
- AliasMap MyAliasMap ` + "`json:\"aliasMap,omitempty\"`" + ` |
| 2962 |
- } |
| 2963 |
- |
| 2964 |
- type MyAliasMap map[string]MyAlias |
| 2965 |
- type MyAlias []string` |
| 2966 |
- |
| 2967 |
- packagestest.TestAll(t, func(t *testing.T, x packagestest.Exporter) { |
| 2968 |
- e := packagestest.Export(t, x, []packagestest.Module{{ |
| 2969 |
- Name: "example.com/base/foo", |
| 2970 |
- Files: map[string]interface{}{ |
| 2971 |
- "foo.go": inputFile, |
| 2972 |
- }, |
| 2973 |
- }}) |
| 2974 |
- defer e.Cleanup() |
| 2975 |
- |
| 2976 |
- callErr, funcErr, _, funcBuffer, imports := testOpenAPITypeWriter(t, e.Config) |
| 2977 |
- if funcErr != nil { |
| 2978 |
- t.Fatalf("Unexpected funcErr: %v", funcErr) |
| 2979 |
- } |
| 2980 |
- if callErr != nil { |
| 2981 |
- t.Fatalf("Unexpected callErr: %v", callErr) |
| 2982 |
- } |
| 2983 |
- expImports := []string{ |
| 2984 |
- `foo "example.com/base/foo"`, |
| 2985 |
- `common "k8s.io/kube-openapi/pkg/common"`, |
| 2986 |
- `spec "k8s.io/kube-openapi/pkg/validation/spec"`, |
| 2987 |
- `ptr "k8s.io/utils/ptr"`, |
| 2988 |
- } |
| 2989 |
- if !cmp.Equal(imports, expImports) { |
| 2990 |
- t.Errorf("wrong imports:\n%s", cmp.Diff(expImports, imports)) |
| 2991 |
- } |
| 2992 |
- |
| 2993 |
- if formatted, err := format.Source(funcBuffer.Bytes()); err != nil { |
| 2994 |
- t.Fatalf("%v\n%v", err, funcBuffer.String()) |
| 2995 |
- } else { |
| 2996 |
- formatted_expected, ree := format.Source([]byte(`func schema_examplecom_base_foo_Blah(ref common.ReferenceCallback) common.OpenAPIDefinition { |
| 2997 |
- return common.OpenAPIDefinition{ |
| 2998 |
- Schema: spec.Schema{ |
| 2999 |
- SchemaProps: spec.SchemaProps{ |
| 3000 |
- Type: []string{"object"}, |
| 3001 |
- AllOf: []spec.Schema{ |
| 3002 |
- { |
| 3003 |
- SchemaProps: spec.SchemaProps{ |
| 3004 |
- Properties: map[string]spec.Schema{ |
| 3005 |
- "aliasMap": { |
| 3006 |
- SchemaProps: spec.SchemaProps{ |
| 3007 |
- AllOf: []spec.Schema{ |
| 3008 |
- { |
| 3009 |
- SchemaProps: spec.SchemaProps{ |
| 3010 |
- AdditionalProperties: &spec.SchemaOrBool{ |
| 3011 |
- Allows: true, |
| 3012 |
- Schema: &spec.Schema{ |
| 3013 |
- SchemaProps: spec.SchemaProps{ |
| 3014 |
- Pattern: "^foo$", |
| 3015 |
- }, |
| 3016 |
- }, |
| 3017 |
- }, |
| 3018 |
- }, |
| 3019 |
- }, |
| 3020 |
- }, |
| 3021 |
- }, |
| 3022 |
- }, |
| 3023 |
- "field": { |
| 3024 |
- SchemaProps: spec.SchemaProps{ |
| 3025 |
- AllOf: []spec.Schema{ |
| 3026 |
- { |
| 3027 |
- SchemaProps: spec.SchemaProps{ |
| 3028 |
- Items: &spec.SchemaOrArray{ |
| 3029 |
- Schema: &spec.Schema{ |
| 3030 |
- SchemaProps: spec.SchemaProps{ |
| 3031 |
- MaxLength: ptr.To[int64](10), |
| 3032 |
- }, |
| 3033 |
- }, |
| 3034 |
- }, |
| 3035 |
- }, |
| 3036 |
- }, |
| 3037 |
- }, |
| 3038 |
- }, |
| 3039 |
- }, |
| 3040 |
- }, |
| 3041 |
- }, |
| 3042 |
- }, |
| 3043 |
- }, |
| 3044 |
- Properties: map[string]spec.Schema{ |
| 3045 |
- "field": { |
| 3046 |
- SchemaProps: spec.SchemaProps{ |
| 3047 |
- AllOf: []spec.Schema{ |
| 3048 |
- { |
| 3049 |
- SchemaProps: spec.SchemaProps{ |
| 3050 |
- Items: &spec.SchemaOrArray{ |
| 3051 |
- Schema: &spec.Schema{ |
| 3052 |
- VendorExtensible: spec.VendorExtensible{ |
| 3053 |
- Extensions: spec.Extensions{ |
| 3054 |
- "x-kubernetes-validations": []interface{}{map[string]interface{}{"rule": "self.length() % 2 == 0"}}, |
| 3055 |
- }, |
| 3056 |
- }, |
| 3057 |
- }, |
| 3058 |
- }, |
| 3059 |
- }, |
| 3060 |
- }, |
| 3061 |
- }, |
| 3062 |
- Type: []string{"array"}, |
| 3063 |
- Items: &spec.SchemaOrArray{ |
| 3064 |
- Schema: &spec.Schema{ |
| 3065 |
- SchemaProps: spec.SchemaProps{ |
| 3066 |
- Default: "", |
| 3067 |
- Type: []string{"string"}, |
| 3068 |
- Format: "", |
| 3069 |
- }, |
| 3070 |
- }, |
| 3071 |
- }, |
| 3072 |
- }, |
| 3073 |
- }, |
| 3074 |
- "aliasMap": { |
| 3075 |
- SchemaProps: spec.SchemaProps{ |
| 3076 |
- AllOf: []spec.Schema{ |
| 3077 |
- { |
| 3078 |
- SchemaProps: spec.SchemaProps{ |
| 3079 |
- AdditionalProperties: &spec.SchemaOrBool{ |
| 3080 |
- Allows: true, |
| 3081 |
- Schema: &spec.Schema{ |
| 3082 |
- SchemaProps: spec.SchemaProps{ |
| 3083 |
- MaxLength: ptr.To[int64](10), |
| 3084 |
- }, |
| 3085 |
- }, |
| 3086 |
- }, |
| 3087 |
- }, |
| 3088 |
- }, |
| 3089 |
- }, |
| 3090 |
- Type: []string{"object"}, |
| 3091 |
- AdditionalProperties: &spec.SchemaOrBool{ |
| 3092 |
- Allows: true, |
| 3093 |
- Schema: &spec.Schema{ |
| 3094 |
- SchemaProps: spec.SchemaProps{ |
| 3095 |
- Type: []string{"array"}, |
| 3096 |
- Items: &spec.SchemaOrArray{ |
| 3097 |
- Schema: &spec.Schema{ |
| 3098 |
- SchemaProps: spec.SchemaProps{ |
| 3099 |
- Default: "", |
| 3100 |
- Type: []string{"string"}, |
| 3101 |
- Format: "", |
| 3102 |
- }, |
| 3103 |
- }, |
| 3104 |
- }, |
| 3105 |
- }, |
| 3106 |
- }, |
| 3107 |
- }, |
| 3108 |
- }, |
| 3109 |
- }, |
| 3110 |
- }, |
| 3111 |
- }, |
| 3112 |
- }, |
| 3113 |
- } |
| 3114 |
- }`)) |
| 3115 |
- if ree != nil { |
| 3116 |
- t.Fatal(ree) |
| 3117 |
- } |
| 3118 |
- assertEqual(t, string(formatted), string(formatted_expected)) |
| 3119 |
- } |
| 3120 |
- }) |
| 3121 |
- |
| 3122 |
-} |