Skip to content

Commit 07afc37

Browse files
committed
fix
1 parent 28beca0 commit 07afc37

File tree

9 files changed

+52
-187
lines changed

9 files changed

+52
-187
lines changed

models/webhook/webhook.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -150,14 +150,7 @@ func init() {
150150
// AfterLoad updates the webhook object upon setting a column
151151
func (w *Webhook) AfterLoad() {
152152
w.HookEvent = &webhook_module.HookEvent{}
153-
154-
events := w.Events
155-
if events == "" {
156-
// jsonv2 is unable to unmarshal an empty string
157-
return
158-
}
159-
160-
if err := json.Unmarshal([]byte(events), w.HookEvent); err != nil {
153+
if err := json.Unmarshal([]byte(w.Events), w.HookEvent); err != nil {
161154
log.Error("Unmarshal[%d]: %v", w.ID, err)
162155
}
163156
}

modules/assetfs/embed.go

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -102,29 +102,12 @@ func NewEmbeddedFS(data []byte) fs.ReadDirFS {
102102
efs := &embeddedFS{data: data, files: make(map[string]*embeddedFileInfo)}
103103
efs.meta = sync.OnceValue(func() *EmbeddedMeta {
104104
var meta EmbeddedMeta
105-
106-
// look for the separator newline between binary data and JSON metadata
107-
// jsonv2 may end with an extra newline
108105
p := bytes.LastIndexByte(data, '\n')
109106
if p < 0 {
110107
return &meta
111108
}
112-
113-
// if the data ends with a newline, look for the previous newline
114-
// to find the real separator
115-
if p == len(data)-1 {
116-
p = bytes.LastIndexByte(data[:p], '\n')
117-
if p < 0 {
118-
return &meta
119-
}
120-
}
121-
122-
jsonData := data[p+1:]
123-
if err := json.Unmarshal(jsonData, &meta); err != nil {
124-
panic("embedded file is not valid: " + err.Error())
125-
}
126-
if meta.Root == nil {
127-
panic("embedded file metadata has nil root")
109+
if err := json.Unmarshal(data[p+1:], &meta); err != nil {
110+
panic("embedded file is not valid")
128111
}
129112
return &meta
130113
})
@@ -167,15 +150,9 @@ func (e *embeddedFS) getFileInfo(fullName string) (*embeddedFileInfo, error) {
167150

168151
fields := strings.Split(fullName, "/")
169152
fi = e.meta().Root
170-
if fi == nil {
171-
return nil, fs.ErrNotExist
172-
}
173153
if fullName != "." {
174154
found := true
175155
for _, field := range fields {
176-
if fi.Children == nil {
177-
return nil, fs.ErrNotExist
178-
}
179156
for _, child := range fi.Children {
180157
if found = child.BaseName == field; found {
181158
fi = child

modules/json/json.go

Lines changed: 2 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,12 @@ type Interface interface {
3232
}
3333

3434
var (
35-
// DefaultJSONHandler default json handler - uses JSON v2 if available, otherwise JSONiter
36-
DefaultJSONHandler = getDefaultHandler()
35+
DefaultJSONHandler = getDefaultJSONHandler()
3736

3837
_ Interface = StdJSON{}
3938
_ Interface = JSONiter{}
40-
_ Interface = JSONv2{}
4139
)
4240

43-
// getDefaultHandler returns the expected JSON implementation
44-
func getDefaultHandler() Interface {
45-
if isJSONv2Available() {
46-
return JSONv2{}
47-
}
48-
return JSONiter{jsoniter.ConfigCompatibleWithStandardLibrary}
49-
}
50-
5141
// StdJSON implements Interface via encoding/json
5242
type StdJSON struct{}
5343

@@ -106,47 +96,6 @@ func (j JSONiter) Indent(dst *bytes.Buffer, src []byte, prefix, indent string) e
10696
return json.Indent(dst, src, prefix, indent)
10797
}
10898

109-
// JSONv2 implements Interface via encoding/json/v2
110-
// Requires GOEXPERIMENT=jsonv2 to be set at build time
111-
type JSONv2 struct{}
112-
113-
// Marshal implements Interface using JSON v2 - fallback if v2 is not available
114-
func (JSONv2) Marshal(v any) ([]byte, error) {
115-
if !isJSONv2Available() {
116-
return json.Marshal(v)
117-
}
118-
return marshalV2(v)
119-
}
120-
121-
// Unmarshal implements Interface using JSON v2 - fallback if v2 is not available
122-
func (JSONv2) Unmarshal(data []byte, v any) error {
123-
if !isJSONv2Available() {
124-
return json.Unmarshal(data, v)
125-
}
126-
return unmarshalV2(data, v)
127-
}
128-
129-
// NewEncoder implements Interface using JSON v2 - fallback if v2 is not available
130-
func (JSONv2) NewEncoder(writer io.Writer) Encoder {
131-
if !isJSONv2Available() {
132-
return json.NewEncoder(writer)
133-
}
134-
return newEncoderV2(writer)
135-
}
136-
137-
// NewDecoder implements Interface using JSON v2 - fallback if v2 is not available
138-
func (JSONv2) NewDecoder(reader io.Reader) Decoder {
139-
if !isJSONv2Available() {
140-
return json.NewDecoder(reader)
141-
}
142-
return newDecoderV2(reader)
143-
}
144-
145-
// Indent implements Interface using standard library (JSON v2 doesn't have Indent yet)
146-
func (JSONv2) Indent(dst *bytes.Buffer, src []byte, prefix, indent string) error {
147-
return json.Indent(dst, src, prefix, indent)
148-
}
149-
15099
// Marshal converts object as bytes
151100
func Marshal(v any) ([]byte, error) {
152101
return DefaultJSONHandler.Marshal(v)
@@ -174,7 +123,7 @@ func Indent(dst *bytes.Buffer, src []byte, prefix, indent string) error {
174123

175124
// MarshalIndent copied from encoding/json
176125
func MarshalIndent(v any, prefix, indent string) ([]byte, error) {
177-
b, err := DefaultJSONHandler.Marshal(v)
126+
b, err := Marshal(v)
178127
if err != nil {
179128
return nil, err
180129
}
@@ -200,12 +149,6 @@ func UnmarshalHandleDoubleEncode(bs []byte, v any) error {
200149
// To be consistent, we should treat all empty inputs as success
201150
return nil
202151
}
203-
204-
trimmed := bytes.TrimSpace(bs)
205-
if len(trimmed) == 0 {
206-
return nil
207-
}
208-
209152
err := DefaultJSONHandler.Unmarshal(bs, v)
210153
if err != nil {
211154
ok := true

modules/json/jsonlegacy.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//go:build !goexperiment.jsonv2
2+
3+
package json
4+
5+
import jsoniter "github.com/json-iterator/go"
6+
7+
func getDefaultJSONHandler() Interface {
8+
return JSONiter{jsoniter.ConfigCompatibleWithStandardLibrary}
9+
}

modules/json/jsonv2.go

Lines changed: 34 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -11,95 +11,71 @@ import (
1111
"io"
1212
)
1313

14-
// isJSONv2Available returns true when JSON v2 is available (compiled with GOEXPERIMENT=jsonv2)
15-
func isJSONv2Available() bool {
16-
return true
14+
// JSONv2 implements Interface via encoding/json/v2
15+
// Requires GOEXPERIMENT=jsonv2 to be set at build time
16+
type JSONv2 struct{}
17+
18+
var _ Interface = JSONv2{}
19+
20+
func getDefaultJSONHandler() Interface {
21+
return JSONv2{}
1722
}
1823

19-
// marshalV2Internal uses JSON v2 marshal with v1 compatibility options (no trailing newline)
20-
func marshalV2Internal(v any) ([]byte, error) {
21-
opts := jsonv2.JoinOptions(
24+
func jsonv2DefaultMarshalOptions() jsonv2.Options {
25+
return jsonv2.JoinOptions(
2226
jsonv2.MatchCaseInsensitiveNames(true),
2327
jsonv2.FormatNilSliceAsNull(true),
2428
jsonv2.FormatNilMapAsNull(true),
2529
jsonv2.Deterministic(true),
2630
)
27-
return jsonv2.Marshal(v, opts)
2831
}
2932

30-
// marshalV2 uses JSON v2 marshal with v1 compatibility options (with trailing newline for compatibility with standard library)
31-
func marshalV2(v any) ([]byte, error) {
32-
result, err := marshalV2Internal(v)
33-
if err != nil {
34-
return nil, err
35-
}
36-
37-
return append(result, '\n'), nil
33+
func jsonv2DefaultUnmarshalOptions() jsonv2.Options {
34+
return jsonv2.JoinOptions(
35+
jsonv2.MatchCaseInsensitiveNames(true),
36+
)
3837
}
3938

40-
// unmarshalV2 uses JSON v2 unmarshal with v1 compatibility options
41-
func unmarshalV2(data []byte, v any) error {
42-
if len(data) == 0 {
43-
return nil
44-
}
39+
func (JSONv2) Marshal(v any) ([]byte, error) {
40+
return jsonv2.Marshal(v, jsonv2DefaultMarshalOptions())
41+
}
4542

43+
func (JSONv2) Unmarshal(data []byte, v any) error {
44+
// legacy behavior: treat empty or whitespace-only input as no input, it should be safe
4645
data = bytes.TrimSpace(data)
4746
if len(data) == 0 {
4847
return nil
4948
}
49+
return jsonv2.Unmarshal(data, v, jsonv2DefaultUnmarshalOptions())
50+
}
5051

51-
opts := jsonv2.JoinOptions(
52-
jsonv2.MatchCaseInsensitiveNames(true),
53-
)
54-
return jsonv2.Unmarshal(data, v, opts)
52+
func (JSONv2) NewEncoder(writer io.Writer) Encoder {
53+
return &encoderV2{writer: writer, opts: jsonv2DefaultMarshalOptions()}
54+
}
55+
56+
func (JSONv2) NewDecoder(reader io.Reader) Decoder {
57+
return &decoderV2{reader: reader, opts: jsonv2DefaultMarshalOptions()}
58+
}
59+
60+
// Indent implements Interface using standard library (JSON v2 doesn't have Indent yet)
61+
func (JSONv2) Indent(dst *bytes.Buffer, src []byte, prefix, indent string) error {
62+
return json.Indent(dst, src, prefix, indent)
5563
}
5664

57-
// encoderV2 wraps JSON v2 streaming encoder
5865
type encoderV2 struct {
5966
writer io.Writer
6067
opts jsonv2.Options
6168
}
6269

6370
func (e *encoderV2) Encode(v any) error {
64-
err := jsonv2.MarshalWrite(e.writer, v, e.opts)
65-
if err != nil {
66-
return err
67-
}
68-
69-
_, err = e.writer.Write([]byte{'\n'})
70-
return err
71+
return jsonv2.MarshalWrite(e.writer, v, e.opts)
7172
}
7273

73-
// newEncoderV2 creates a new JSON v2 streaming encoder
74-
func newEncoderV2(writer io.Writer) Encoder {
75-
opts := jsonv2.JoinOptions(
76-
jsonv2.MatchCaseInsensitiveNames(true),
77-
jsonv2.FormatNilSliceAsNull(true),
78-
jsonv2.FormatNilMapAsNull(true),
79-
jsonv2.Deterministic(true),
80-
)
81-
return &encoderV2{writer: writer, opts: opts}
82-
}
83-
84-
// decoderV2 wraps JSON v2 streaming decoder
8574
type decoderV2 struct {
8675
reader io.Reader
8776
opts jsonv2.Options
8877
}
8978

9079
func (d *decoderV2) Decode(v any) error {
91-
err := jsonv2.UnmarshalRead(d.reader, v, d.opts)
92-
// Handle EOF more gracefully to match standard library behavior
93-
if err != nil && err.Error() == "unexpected EOF" {
94-
return io.EOF
95-
}
96-
return err
97-
}
98-
99-
// newDecoderV2 creates a new JSON v2 streaming decoder
100-
func newDecoderV2(reader io.Reader) Decoder {
101-
opts := jsonv2.JoinOptions(
102-
jsonv2.MatchCaseInsensitiveNames(true),
103-
)
104-
return &decoderV2{reader: reader, opts: opts}
80+
return jsonv2.UnmarshalRead(d.reader, v, d.opts)
10581
}

modules/json/jsonv2_fallback.go

Lines changed: 0 additions & 33 deletions
This file was deleted.

services/webhook/deliver_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,13 +142,13 @@ func TestWebhookDeliverHookTask(t *testing.T) {
142142
assert.NoError(t, err)
143143
assert.Equal(t, `{"data": 42}`, string(body))
144144

145-
case "/webhook/4ddf3b1533e54f082ae6eadfc1b5530be36c8893":
145+
case "/webhook/6db5dc1e282529a8c162c7fe93dd2667494eeb51":
146146
// Version 2
147147
assert.Equal(t, "push", r.Header.Get("X-GitHub-Event"))
148148
assert.Equal(t, "application/json", r.Header.Get("Content-Type"))
149149
body, err := io.ReadAll(r.Body)
150150
assert.NoError(t, err)
151-
assert.Len(t, body, 2047)
151+
assert.Len(t, body, 2147)
152152

153153
default:
154154
w.WriteHeader(http.StatusNotFound)

services/webhook/matrix_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ func TestMatrixJSONPayload(t *testing.T) {
216216
require.NoError(t, err)
217217

218218
assert.Equal(t, "PUT", req.Method)
219-
assert.Equal(t, "/_matrix/client/r0/rooms/ROOM_ID/send/m.room.message/4ddf3b1533e54f082ae6eadfc1b5530be36c8893", req.URL.Path)
219+
assert.Equal(t, "/_matrix/client/r0/rooms/ROOM_ID/send/m.room.message/6db5dc1e282529a8c162c7fe93dd2667494eeb51", req.URL.Path)
220220
assert.Equal(t, "sha256=", req.Header.Get("X-Hub-Signature-256"))
221221
assert.Equal(t, "application/json", req.Header.Get("Content-Type"))
222222
var body MatrixPayload

tests/integration/api_packages_vagrant_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,6 @@ func TestPackageVagrant(t *testing.T) {
165165
provider := version.Providers[0]
166166
assert.Equal(t, packageProvider, provider.Name)
167167
assert.Equal(t, "sha512", provider.ChecksumType)
168-
assert.Equal(t, "c9967d88db2888a74778b5c62dbc2508921c8b54aca0e2ba34ab3e95e655cdb182bb2989b28e7ab4cab696f2ac7193d7ba9f57dea5191aad0c6a1082991c1ab8", provider.Checksum)
168+
assert.Equal(t, "259bebd6160acad695016d22a45812e26f187aaf78e71a4c23ee3201528346293f991af3468a8c6c5d2a21d7d9e1bdc1bf79b87110b2fddfcc5a0d45963c7c30", provider.Checksum)
169169
})
170170
}

0 commit comments

Comments
 (0)