Skip to content
This repository was archived by the owner on Sep 16, 2024. It is now read-only.

Commit b926a09

Browse files
thxCodegitlawr
authored andcommitted
fix: failed make optional on ancestor node
Signed-off-by: thxCode <thxcode0824@gmail.com>
1 parent ddec5a8 commit b926a09

File tree

10 files changed

+258
-87
lines changed

10 files changed

+258
-87
lines changed

pkg/resourcedefinitions/helper.go

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,11 @@ func refillVariableSchemaRef(
538538
return
539539
}
540540

541-
v.Default = def
541+
// Refill the default value to the root,
542+
// but ignore this refilling when it is an array item.
543+
if !strings.HasSuffix(key, ".0") {
544+
v.Default = def
545+
}
542546
}
543547

544548
for k := range v.Properties {
@@ -575,18 +579,18 @@ func refillVariableSchemaRef(
575579
if !strings.HasSuffix(key, ".0") {
576580
v.Default = def
577581
}
582+
}
578583

579-
// Turn property to optional if found different defaults.
580-
pName := pNames[len(pNames)-1]
581-
pSchema := pSchemas[len(pSchemas)-1]
582-
dropRequired(pSchema, pName)
584+
// Turn property to optional if found different defaults.
585+
pName := pNames[len(pNames)-1]
586+
pSchema := pSchemas[len(pSchemas)-1]
587+
dropRequired(pSchema, v, pName)
583588

584-
// Effect to the parent node also if the node has no required property.
585-
if len(pSchema.Required) == 0 && len(pSchemas) > 1 {
586-
ppName := pNames[len(pNames)-2]
587-
ppSchema := pSchemas[len(pSchemas)-2]
588-
dropRequired(ppSchema, ppName)
589-
}
589+
// Effect to the parent node also if the node has no required property.
590+
if len(pSchemas) > 1 {
591+
ppName := pNames[len(pNames)-2]
592+
ppSchema := pSchemas[len(pSchemas)-2]
593+
dropRequired(ppSchema, pSchema, ppName)
590594
}
591595

592596
return
@@ -626,43 +630,50 @@ func refillVariableSchemaRef(
626630
// Turn property to optional if found different defaults.
627631
pName := pNames[len(pNames)-1]
628632
pSchema := pSchemas[len(pSchemas)-1]
629-
dropRequired(pSchema, pName)
633+
dropRequired(pSchema, v, pName)
630634

631635
// Effect to the parent node also if the node has no required property.
632-
if len(pSchema.Required) == 0 && len(pSchemas) > 1 {
636+
if len(pSchemas) > 1 {
633637
ppName := pNames[len(pNames)-2]
634638
ppSchema := pSchemas[len(pSchemas)-2]
635-
dropRequired(ppSchema, ppName)
639+
dropRequired(ppSchema, pSchema, ppName)
636640
}
637641
}
638642
}
639643

640-
// dropRequired drops the given property name from the required of the schema.
641-
func dropRequired(s *openapi3.Schema, n string) {
642-
switch s.Type {
644+
// dropRequired drops the given property name from the parent schema required list,
645+
// if the property schema's required list is empty.
646+
func dropRequired(parentSchema, propSchema *openapi3.Schema, propName string) {
647+
// Return directly, if required list is not empty.
648+
if len(propSchema.Required) != 0 {
649+
return
650+
}
651+
652+
// Otherwise, drop the property from the required list.
653+
switch parentSchema.Type {
643654
default:
644655
return
645656
case openapi3.TypeObject:
646657
case openapi3.TypeArray:
647-
s = s.Items.Value
658+
parentSchema = parentSchema.Items.Value
648659
}
649660

650661
var i int
651-
for ; i < len(s.Required); i++ {
652-
if s.Required[i] == n {
662+
for ; i < len(parentSchema.Required); i++ {
663+
if parentSchema.Required[i] == propName {
653664
break
654665
}
655666
}
656667

657668
switch i {
658-
case len(s.Required):
669+
case len(parentSchema.Required):
659670
return
660671
case 0:
661-
s.Required = s.Required[1:]
662-
case len(s.Required) - 1:
663-
s.Required = s.Required[:i]
672+
parentSchema.Required = parentSchema.Required[1:]
673+
case len(parentSchema.Required) - 1:
674+
parentSchema.Required = parentSchema.Required[:i]
664675
default:
665-
s.Required = append(s.Required[:i], s.Required[i+1:]...)
676+
parentSchema.Required = append(parentSchema.Required[:i], parentSchema.Required[i+1:]...)
666677
}
667678
}
668679

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"map": {
3+
"key1": "value1",
4+
"key2": "value2"
5+
},
6+
"list_map": [
7+
{
8+
"key1": "value1"
9+
}
10+
],
11+
"object_object_complex": {
12+
"object": {
13+
"object": {
14+
"name": "myoperation",
15+
"value": "myvalue"
16+
}
17+
}
18+
},
19+
"list_object_list_complex": [
20+
{
21+
"array": [
22+
{
23+
"name": "myoperation",
24+
"value": "myvalue"
25+
},
26+
{
27+
"name": "myoperation2",
28+
"value": "myvalue2"
29+
}
30+
],
31+
"map": {
32+
"key1": "value1",
33+
"key2": "value2"
34+
}
35+
}
36+
]
37+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"map": {
3+
"key2": "value2",
4+
"key1": "value1"
5+
},
6+
"list_map": [
7+
{
8+
"key1": "value1"
9+
}
10+
],
11+
"object_object_complex": {
12+
"object": {
13+
"object": {
14+
"name": "myoperation2",
15+
"value": "myvalue"
16+
}
17+
}
18+
},
19+
"list_object_list_complex": [
20+
{
21+
"array": [
22+
{
23+
"name": "myoperation",
24+
"value": "myvalue"
25+
}
26+
],
27+
"map": {
28+
"key2": "value2",
29+
"key1": "value1"
30+
}
31+
}
32+
]
33+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
info:
2+
title: OpenAPI schema for resource definition type "dummy"
3+
version: v0.0.0
4+
paths:
5+
openapi: 3.0.3
6+
components:
7+
schemas:
8+
variables:
9+
type: object
10+
required:
11+
- object_object_complex
12+
properties:
13+
map:
14+
type: object
15+
additionalProperties:
16+
type: string
17+
list_map:
18+
type: array
19+
default:
20+
- key1: "value1"
21+
items:
22+
additionalProperties:
23+
type: string
24+
type: object
25+
object_object_complex:
26+
type: object
27+
required:
28+
- map
29+
properties:
30+
object:
31+
type: object
32+
properties:
33+
object:
34+
type: object
35+
properties:
36+
name:
37+
type: string
38+
value:
39+
type: string
40+
default: "myvalue"
41+
map:
42+
type: object
43+
additionalProperties:
44+
type: string
45+
list_object_list_complex:
46+
type: array
47+
items:
48+
type: object
49+
properties:
50+
array:
51+
type: array
52+
items:
53+
type: object
54+
properties:
55+
name:
56+
type: string
57+
default: "myoperation"
58+
value:
59+
type: string
60+
default: "myvalue"
61+
map:
62+
type: object
63+
additionalProperties:
64+
type: string
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
info:
2+
title: OpenAPI schema for resource definition type "dummy"
3+
version: v0.0.0
4+
paths:
5+
openapi: 3.0.3
6+
components:
7+
schemas:
8+
variables:
9+
type: object
10+
required:
11+
- map # expected: remove this.
12+
- list_map # expected: remove this.
13+
- object_object_complex
14+
- list_object_list_complex # expected: remove this.
15+
properties:
16+
map:
17+
type: object
18+
default: # expected: remove this, as the given defaults are not the same.
19+
key1: "value1"
20+
key2: "value2"
21+
additionalProperties:
22+
type: string
23+
list_map:
24+
type: array
25+
default: # expected: replace this, as the given defaults are the same.
26+
- key1: "value1"
27+
- key2: "value2"
28+
items:
29+
additionalProperties:
30+
type: string
31+
type: object
32+
object_object_complex:
33+
type: object
34+
required:
35+
- object # expected: remove this, as there are defaults.
36+
- map
37+
properties:
38+
object:
39+
type: object
40+
# expected: no default, as the given defaults are not the same.
41+
properties:
42+
object:
43+
type: object
44+
required: # expected: remove this, as there are defaults.
45+
- name
46+
- value
47+
properties:
48+
name:
49+
type: string
50+
value:
51+
type: string
52+
# expected: default, as the given defaults are the same.
53+
map:
54+
type: object
55+
additionalProperties:
56+
type: string
57+
list_object_list_complex:
58+
type: array
59+
items:
60+
type: object
61+
required: # expected: remove this, as there are defaults.
62+
- array
63+
- map
64+
properties:
65+
array:
66+
type: array
67+
items:
68+
type: object
69+
required: # expected: remove this, as there are defaults.
70+
- name
71+
- value
72+
# expected: no default, as the given defaults are not the same.
73+
properties:
74+
name:
75+
type: string
76+
# expected: default, as the given defaults are the same.
77+
value:
78+
type: string
79+
# expected: default, as the given defaults are the same.
80+
map:
81+
type: object
82+
additionalProperties:
83+
type: string
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"variables": {}
3+
}

pkg/resourcedefinitions/testdata/refill_variable_schema_ref/optional/default.1.json

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,5 @@
2121
"infrastructure": {
2222
"vpc_id": "abc",
2323
"publicly_accessible": true
24-
},
25-
"operation_labels": {
26-
"key1": "value1",
27-
"key2": "value2"
28-
},
29-
"operation_pool_labels": [
30-
{
31-
"key1": "value1"
32-
},
33-
{
34-
"key2": "value2"
35-
}
36-
]
24+
}
3725
}

pkg/resourcedefinitions/testdata/refill_variable_schema_ref/optional/default.2.json

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,5 @@
2121
"infrastructure": {
2222
"vpc_id": "edf",
2323
"publicly_accessible": true
24-
},
25-
"operation_labels": {
26-
"key1": "value1",
27-
"key2": "value2"
28-
},
29-
"operation_pool_labels": [
30-
{
31-
"key1": "value1"
32-
},
33-
{
34-
"key2": "value2"
35-
}
36-
]
24+
}
3725
}

pkg/resourcedefinitions/testdata/refill_variable_schema_ref/optional/expected.yaml

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -275,24 +275,6 @@ components:
275275
group: Basic
276276
order: 4
277277
showIf: architecture=replication
278-
operation_labels:
279-
default:
280-
key1: "value1"
281-
key2: "value2"
282-
additionalProperties:
283-
type: string
284-
title: Operation Labels
285-
type: object
286-
operation_pool_labels:
287-
default:
288-
- key1: "value1"
289-
- key2: "value2"
290-
items:
291-
additionalProperties:
292-
type: string
293-
type: object
294-
title: Operation Pool Labels
295-
type: array
296278
x-walrus-ui:
297279
groupOrder:
298280
- Basic

0 commit comments

Comments
 (0)