Skip to content

Commit 4fcedbd

Browse files
authored
fix: protojson non-determinism in ID generation (#4967)
My tests in #4966 were failing because `protojson` decided it wanted to add two spaces between the keys and values in its output. Run the `protojson` output through the `json.Indent` to format it consistently. That said, does the formatting of the output actually matter here? Should we instead rewrite the tests to do a semantic comparison instead of a string comparison?
1 parent dbadc4c commit 4fcedbd

1 file changed

Lines changed: 8 additions & 7 deletions

File tree

vulnfeeds/cmd/ids/main.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
package main
33

44
import (
5+
"bytes"
56
"crypto/rand"
67
"encoding/hex"
8+
"encoding/json"
79
"flag"
810
"fmt"
911
"io"
@@ -221,20 +223,19 @@ func readVulnWithFormat(r io.Reader, format fileFormat) (*osvschema.Vulnerabilit
221223
}
222224

223225
func writeVulnWithFormat(v *osvschema.Vulnerability, w io.Writer, format fileFormat) error {
224-
marshaler := protojson.MarshalOptions{
225-
Multiline: true,
226-
Indent: " ",
227-
}
228-
229-
jsonBytes, err := marshaler.Marshal(v)
226+
jsonBytes, err := protojson.Marshal(v)
230227
if err != nil {
231228
return err
232229
}
233230

234231
var data []byte
235232
switch format {
236233
case fileFormatJSON:
237-
data = jsonBytes
234+
var buf bytes.Buffer
235+
if err := json.Indent(&buf, jsonBytes, "", " "); err != nil {
236+
return err
237+
}
238+
data = buf.Bytes()
238239
case fileFormatYAML:
239240
data, err = yaml.JSONToYAML(jsonBytes)
240241
if err != nil {

0 commit comments

Comments
 (0)