Skip to content

Commit 1d2336b

Browse files
feat(client): support optional json html escaping
1 parent 219f209 commit 1d2336b

File tree

5 files changed

+16
-8
lines changed

5 files changed

+16
-8
lines changed

internal/encoding/json/encode.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,11 @@ func Marshal(v any) ([]byte, error) {
177177
e := newEncodeState()
178178
defer encodeStatePool.Put(e)
179179

180-
err := e.marshal(v, encOpts{escapeHTML: true})
180+
// SHIM(begin): don't escape HTML by default
181+
err := e.marshal(v, encOpts{escapeHTML: shims.EscapeHTMLByDefault})
182+
// ORIGINAL:
183+
// err := e.marshal(v, encOpts{escapeHTML: true})
184+
// SHIM(end)
181185
if err != nil {
182186
return nil, err
183187
}

internal/encoding/json/shims/shims.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
"slices"
1212
)
1313

14+
const EscapeHTMLByDefault = true
15+
1416
type OverflowableType struct{ reflect.Type }
1517

1618
func (t OverflowableType) OverflowInt(x int64) bool {

internal/requestconfig/requestconfig.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,13 @@ func NewRequestConfig(ctx context.Context, method string, u string, body any, ds
137137
// Fallback to json serialization if none of the serialization functions that we expect
138138
// to see is present.
139139
if body != nil && !hasSerializationFunc {
140-
content, err := json.Marshal(body)
141-
if err != nil {
140+
buf := new(bytes.Buffer)
141+
enc := json.NewEncoder(buf)
142+
enc.SetEscapeHTML(true)
143+
if err := enc.Encode(body); err != nil {
142144
return nil, err
143145
}
144-
reader = bytes.NewBuffer(content)
146+
reader = buf
145147
}
146148

147149
req, err := http.NewRequestWithContext(ctx, method, u, nil)

packages/param/option.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func (o Opt[T]) MarshalJSON() ([]byte, error) {
6262
if !o.Valid() {
6363
return []byte("null"), nil
6464
}
65-
return json.Marshal(o.Value)
65+
return shimjson.Marshal(o.Value)
6666
}
6767

6868
func (o *Opt[T]) UnmarshalJSON(data []byte) error {
@@ -96,7 +96,7 @@ func (o Opt[T]) MarshalJSONWithTimeLayout(format string) []byte {
9696
return nil
9797
}
9898

99-
b, err := json.Marshal(t.Format(shimjson.TimeLayout(format)))
99+
b, err := shimjson.Marshal(t.Format(shimjson.TimeLayout(format)))
100100
if err != nil {
101101
return nil
102102
}

shared/constant/constants.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
package constant
44

55
import (
6-
"encoding/json"
6+
shimjson "github.com/openai/openai-go/internal/encoding/json"
77
)
88

99
type Constant[T any] interface {
@@ -730,5 +730,5 @@ func marshalString[T ~string, PT constant[T]](v T) ([]byte, error) {
730730
if v == zero {
731731
v = PT(&v).Default()
732732
}
733-
return json.Marshal(string(v))
733+
return shimjson.Marshal(string(v))
734734
}

0 commit comments

Comments
 (0)