Skip to content

Commit

Permalink
bugfix: emit CEL rule strings using raw strings since they may contai…
Browse files Browse the repository at this point in the history
…n quotes
  • Loading branch information
alexzielenski committed Jan 17, 2024
1 parent 90b8d5f commit d43d9d4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
6 changes: 3 additions & 3 deletions pkg/generators/openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -640,14 +640,14 @@ func (g openAPITypeWriter) emitExtensions(extensions []extension, unions []union
for _, rule := range celRules {
g.Do("map[string]interface{}{\n", nil)

g.Do("\"rule\": \"$.$\",\n", rule.Rule)
g.Do("\"rule\": `$.$`,\n", rule.Rule)

if len(rule.Message) > 0 {
g.Do("\"message\": \"$.$\",\n", rule.Message)
g.Do("\"message\": `$.$`,\n", rule.Message)
}

if len(rule.MessageExpression) > 0 {
g.Do("\"messageExpression\": \"$.$\",\n", rule.MessageExpression)
g.Do("\"messageExpression\": `$.$`,\n", rule.MessageExpression)
}

if rule.OptionalOldSelf != nil && *rule.OptionalOldSelf {
Expand Down
19 changes: 10 additions & 9 deletions pkg/generators/openapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2113,9 +2113,10 @@ func TestMultilineCELMarkerComments(t *testing.T) {
type Blah struct {
// +k8s:validation:cel[0]:rule="self.length() > 0"
// +k8s:validation:cel[0]:message="string message"
// +k8s:validation:cel[1]:rule>
// +k8s:validation:cel[1]:rule> self.length() % 2 == 0
// +k8s:validation:cel[1]:messageExpression="self + ' hello'"
// +k8s:validation:cel[1]:rule> !oldSelf.hasValue() || self.length() % 2 == 0
// +k8s:validation:cel[1]:rule> ? self.field == "even"
// +k8s:validation:cel[1]:rule> : self.field == "odd"
// +k8s:validation:cel[1]:messageExpression="field must be whether the length of the string is even or odd"
// +k8s:validation:cel[1]:optionalOldSelf
// +optional
Field string
Expand All @@ -2140,12 +2141,12 @@ func TestMultilineCELMarkerComments(t *testing.T) {
Extensions: spec.Extensions{
"x-kubernetes-validations": []interface{}{
map[string]interface{}{
"rule": "self.length() > 0",
"message": "string message",
"rule": ` + "`self.length() > 0`" + `,
"message": ` + "`string message`" + `,
},
map[string]interface{}{
"rule": "self.length() % 2 == 0",
"messageExpression": "self + ' hello'",
"rule": ` + "` !oldSelf.hasValue() || self.length() % 2 == 0\n ? self.field == \"even\"\n : self.field == \"odd\"`" + `,
"messageExpression": ` + "`field must be whether the length of the string is even or odd`" + `,
"optionalOldSelf": ptr.To[bool](true),
},
},
Expand All @@ -2163,8 +2164,8 @@ func TestMultilineCELMarkerComments(t *testing.T) {
Extensions: spec.Extensions{
"x-kubernetes-validations": []interface{}{
map[string]interface{}{
"rule": "self == oldSelf",
"message": "message1",
"rule": ` + "`self == oldSelf`" + `,
"message": ` + "`message1`" + `,
},
},
},
Expand Down

0 comments on commit d43d9d4

Please sign in to comment.