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

Commit ddec5a8

Browse files
thxCodeaiwantaozi
authored andcommitted
fix: failed keeping required if object has required properties
Signed-off-by: thxCode <thxcode0824@gmail.com>
1 parent e32eff4 commit ddec5a8

File tree

5 files changed

+29
-8
lines changed

5 files changed

+29
-8
lines changed

pkg/resourcedefinitions/helper.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,11 @@ func refillVariableSchemaRef(
570570
return
571571
}
572572

573-
v.AdditionalProperties.Schema.Value.Default = def
573+
// Refill the default value to the additional properties,
574+
// but ignore this refilling when it is an array item.
575+
if !strings.HasSuffix(key, ".0") {
576+
v.Default = def
577+
}
574578

575579
// Turn property to optional if found different defaults.
576580
pName := pNames[len(pNames)-1]
@@ -694,14 +698,14 @@ func alignDefaults(key string, defs [][]byte) (any, bool) {
694698
var found bool
695699

696700
for _, d := range defs {
697-
jq := json.Get(d, prefix)
701+
jq := json.Get(d, key)
698702
if jq.Exists() {
699703
found = true
700704
break
701705
}
702706
}
703707

704-
// Return directly if parent is not found.
708+
// Return directly if key is not found.
705709
if !found {
706710
return nil, false
707711
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,15 @@
2323
"publicly_accessible": true
2424
},
2525
"operation_labels": {
26-
"key1": "value1"
26+
"key1": "value1",
27+
"key2": "value2"
2728
},
2829
"operation_pool_labels": [
2930
{
3031
"key1": "value1"
32+
},
33+
{
34+
"key2": "value2"
3135
}
3236
]
3337
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
"operation_pool_labels": [
3030
{
3131
"key1": "value1"
32+
},
33+
{
34+
"key2": "value2"
3235
}
3336
]
3437
}

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ components:
102102
colSpan: 12
103103
variables:
104104
type: object
105+
required:
106+
- infrastructure
105107
properties:
106108
context:
107109
type: object
@@ -205,6 +207,8 @@ components:
205207
infrastructure:
206208
type: object
207209
title: Infrastructure
210+
required:
211+
- kms_key_id
208212
properties:
209213
vpc_id:
210214
type: string
@@ -272,17 +276,19 @@ components:
272276
order: 4
273277
showIf: architecture=replication
274278
operation_labels:
279+
default:
280+
key1: "value1"
281+
key2: "value2"
275282
additionalProperties:
276-
type: object
283+
type: string
277284
title: Operation Labels
278285
type: object
279286
operation_pool_labels:
280287
default:
281288
- key1: "value1"
289+
- key2: "value2"
282290
items:
283291
additionalProperties:
284-
default:
285-
key1: "value1"
286292
type: string
287293
type: object
288294
title: Operation Pool Labels

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,10 @@ components:
212212
infrastructure:
213213
type: object
214214
title: Infrastructure
215+
# NB: we will tidy up this field.
215216
required:
216217
- vpc_id
218+
- kms_key_id
217219
properties:
218220
# NB: we mark this property optional as the given defaults are not the same.
219221
vpc_id:
@@ -284,8 +286,10 @@ components:
284286
order: 4
285287
showIf: architecture=replication
286288
operation_labels:
289+
default:
290+
name: default-node
287291
additionalProperties:
288-
type: object
292+
type: string
289293
title: Operation Labels
290294
type: object
291295
operation_pool_labels:

0 commit comments

Comments
 (0)