Skip to content

Commit ec61790

Browse files
fix: quote TOON array elements containing semicolons (#217)
1 parent f78466a commit ec61790

2 files changed

Lines changed: 35 additions & 2 deletions

File tree

oq/format.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -350,12 +350,24 @@ func toonValue(v expr.Value) string {
350350
case expr.KindBool:
351351
return strconv.FormatBool(v.Bool)
352352
case expr.KindArray:
353-
return toonEscape(strings.Join(v.Arr, ";"))
353+
return toonArrayValue(v.Arr)
354354
default:
355355
return "null"
356356
}
357357
}
358358

359+
func toonArrayValue(values []string) string {
360+
parts := make([]string, len(values))
361+
for i, value := range values {
362+
if strings.Contains(value, ";") {
363+
parts[i] = toonQuote(value)
364+
continue
365+
}
366+
parts[i] = toonEscape(value)
367+
}
368+
return strings.Join(parts, ";")
369+
}
370+
359371
// toonEscape quotes a string if it needs escaping for TOON format.
360372
// A string must be quoted if it: is empty, contains comma/colon/quote/backslash/
361373
// brackets/braces/control chars, has leading/trailing whitespace, or matches
@@ -387,7 +399,10 @@ func toonEscape(s string) string {
387399
if !needsQuote {
388400
return s
389401
}
390-
// Quote with escaping
402+
return toonQuote(s)
403+
}
404+
405+
func toonQuote(s string) string {
391406
var sb strings.Builder
392407
sb.WriteByte('"')
393408
for _, ch := range s {

oq/format_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package oq
2+
3+
import (
4+
"testing"
5+
6+
"github.com/speakeasy-api/openapi/oq/expr"
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestToonValue_ArrayEscapesSemicolonElements(t *testing.T) {
11+
t.Parallel()
12+
13+
value := expr.ArrayVal([]string{"v1;deprecated", "v2;current"})
14+
15+
encoded := toonValue(value)
16+
17+
assert.Equal(t, `"v1;deprecated";"v2;current"`, encoded, "array elements containing the delimiter should be quoted individually")
18+
}

0 commit comments

Comments
 (0)