Skip to content

Commit

Permalink
core/adt: correct copying of conjuncts in stripNonDefaults
Browse files Browse the repository at this point in the history
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 <matthew@cue.works>
Change-Id: I7dd5c23cad4c9b4d7453c7c8d6de9cc9e07a7f92
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1209119
Reviewed-by: Marcel van Lohuizen <mpvl@gmail.com>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
  • Loading branch information
cuematthew committed Feb 21, 2025
1 parent 9413ce6 commit 15a702e
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
4 changes: 4 additions & 0 deletions encoding/openapi/openapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ func TestParseDefinitions(t *testing.T) {
in: "omitvalue.cue",
out: "omitvalue.json",
config: defaultConfig,
}, {
in: "issue3718.cue",
out: "issue3718.json",
config: defaultConfig,
}}
for _, tc := range testCases {
t.Run(tc.out+tc.variant, func(t *testing.T) {
Expand Down
9 changes: 9 additions & 0 deletions encoding/openapi/testdata/issue3718.cue
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import (
"encoding/json"
)

let defaultPolicy = json.Marshal({ deny: "all" })

#Spec: {
policy?: string | *defaultPolicy
}
21 changes: 21 additions & 0 deletions encoding/openapi/testdata/issue3718.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"openapi": "3.0.0",
"info": {
"title": "Generated by cue.",
"version": "no version"
},
"paths": {},
"components": {
"schemas": {
"Spec": {
"type": "object",
"properties": {
"policy": {
"type": "string",
"default": "{\"deny\":\"all\"}"
}
}
}
}
}
}
10 changes: 7 additions & 3 deletions internal/core/adt/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@

package adt

import (
"slices"
)

// Default returns the default value or itself if there is no default.
func Default(v Value) Value {
switch x := v.(type) {
Expand Down Expand Up @@ -131,9 +135,9 @@ func stripNonDefaults(elem Elem) (r Elem, stripped bool) {
// NOTE: this code requires allocations unconditional. This should be
// mitigated once we optimize conjunct groupings.
isNew := false
var a ConjunctGroup = make([]Conjunct, len(*x))
for i, c := range *x {
a[i].x, ok = stripNonDefaults(c.Expr())
a := slices.Clone(*x)
for i, c := range a {
a[i].x, ok = stripNonDefaults(c.Elem())
if ok {
isNew = true
}
Expand Down

0 comments on commit 15a702e

Please sign in to comment.