Skip to content

Commit 15a702e

Browse files
committed
core/adt: correct copying of conjuncts in stripNonDefaults
If Conjunct.x is a ConjunctGroup, then Conjunct.Expr() may return an expression which is a distant descendent. And then we have no way of obtaining the correct Env for the expr which we need in stripNonDefaults(). StripNonDefaults should duplicate the tree of conjuncts and conjunct groups faithfully, so it is simpler to clone the current ConjunctGroup and then update the x's with stripped versions of each conjunct's Elem (which does not behave in the same way as the Conjunct.Expr()). Fixes: #3718 Signed-off-by: Matthew Sackman <[email protected]> Change-Id: I7dd5c23cad4c9b4d7453c7c8d6de9cc9e07a7f92 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1209119 Reviewed-by: Marcel van Lohuizen <[email protected]> TryBot-Result: CUEcueckoo <[email protected]>
1 parent 9413ce6 commit 15a702e

File tree

4 files changed

+41
-3
lines changed

4 files changed

+41
-3
lines changed

encoding/openapi/openapi_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,10 @@ func TestParseDefinitions(t *testing.T) {
176176
in: "omitvalue.cue",
177177
out: "omitvalue.json",
178178
config: defaultConfig,
179+
}, {
180+
in: "issue3718.cue",
181+
out: "issue3718.json",
182+
config: defaultConfig,
179183
}}
180184
for _, tc := range testCases {
181185
t.Run(tc.out+tc.variant, func(t *testing.T) {
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import (
2+
"encoding/json"
3+
)
4+
5+
let defaultPolicy = json.Marshal({ deny: "all" })
6+
7+
#Spec: {
8+
policy?: string | *defaultPolicy
9+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"openapi": "3.0.0",
3+
"info": {
4+
"title": "Generated by cue.",
5+
"version": "no version"
6+
},
7+
"paths": {},
8+
"components": {
9+
"schemas": {
10+
"Spec": {
11+
"type": "object",
12+
"properties": {
13+
"policy": {
14+
"type": "string",
15+
"default": "{\"deny\":\"all\"}"
16+
}
17+
}
18+
}
19+
}
20+
}
21+
}

internal/core/adt/default.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414

1515
package adt
1616

17+
import (
18+
"slices"
19+
)
20+
1721
// Default returns the default value or itself if there is no default.
1822
func Default(v Value) Value {
1923
switch x := v.(type) {
@@ -131,9 +135,9 @@ func stripNonDefaults(elem Elem) (r Elem, stripped bool) {
131135
// NOTE: this code requires allocations unconditional. This should be
132136
// mitigated once we optimize conjunct groupings.
133137
isNew := false
134-
var a ConjunctGroup = make([]Conjunct, len(*x))
135-
for i, c := range *x {
136-
a[i].x, ok = stripNonDefaults(c.Expr())
138+
a := slices.Clone(*x)
139+
for i, c := range a {
140+
a[i].x, ok = stripNonDefaults(c.Elem())
137141
if ok {
138142
isNew = true
139143
}

0 commit comments

Comments
 (0)