Skip to content

Commit 254d7a7

Browse files
fix: Fix prefixDocumentId() to use correct prefix (#272)
Signed-off-by: Souta Kawahara <souta.kawahara@almalinux.org>
1 parent e6786a8 commit 254d7a7

File tree

2 files changed

+172
-1
lines changed

2 files changed

+172
-1
lines changed

spdx/v2/common/identifier.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func (d *DocumentID) UnmarshalJSON(data []byte) error {
3737
// prefixDocumentId adds the DocumentRef- prefix to an document ID if it does not have one
3838
func prefixDocumentId(id DocumentID) string {
3939
val := string(id)
40-
if !strings.HasPrefix(val, spdxRefPrefix) {
40+
if !strings.HasPrefix(val, documentRefPrefix) {
4141
return documentRefPrefix + val
4242
}
4343
return val

spdx/v2/common/identifier_test.go

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,3 +323,174 @@ func Test_ElementIDStructDecoding(t *testing.T) {
323323
})
324324
}
325325
}
326+
327+
func Test_DocumentIDEncoding(t *testing.T) {
328+
tests := []struct {
329+
name string
330+
value DocumentID
331+
expected string
332+
err bool
333+
}{
334+
{
335+
name: "appends documentref",
336+
value: DocumentID("some-id"),
337+
expected: "DocumentRef-some-id",
338+
},
339+
{
340+
name: "appends documentref",
341+
value: DocumentID("DocumentRef-some-id"),
342+
expected: "DocumentRef-some-id",
343+
},
344+
}
345+
346+
for _, test := range tests {
347+
t.Run(test.name, func(t *testing.T) {
348+
result, err := marshal.JSON(test.value)
349+
switch {
350+
case !test.err && err != nil:
351+
t.Fatalf("unexpected error: %v", err)
352+
case test.err && err == nil:
353+
t.Fatalf("expected error but got none")
354+
case test.err:
355+
return
356+
}
357+
s := string(result)
358+
if !strings.HasPrefix(s, `"`) || !strings.HasSuffix(s, `"`) {
359+
t.Fatalf("string was not returned: %s", s)
360+
}
361+
s = strings.Trim(s, `"`)
362+
if test.expected != s {
363+
t.Fatalf("%s != %s", test.expected, s)
364+
}
365+
})
366+
}
367+
}
368+
369+
func Test_DocumentIDDecoding(t *testing.T) {
370+
tests := []struct {
371+
name string
372+
value string
373+
expected DocumentID
374+
err bool
375+
}{
376+
{
377+
name: "valid id",
378+
value: "DocumentRef-some-id",
379+
expected: DocumentID("some-id"),
380+
},
381+
{
382+
name: "without prefix",
383+
value: "some-id-without-documentref",
384+
expected: DocumentID("some-id-without-documentref"),
385+
},
386+
}
387+
388+
for _, test := range tests {
389+
t.Run(test.name, func(t *testing.T) {
390+
var out DocumentID
391+
s := fmt.Sprintf(`"%s"`, test.value)
392+
err := json.Unmarshal([]byte(s), &out)
393+
switch {
394+
case !test.err && err != nil:
395+
t.Fatalf("unexpected error: %v", err)
396+
case test.err && err == nil:
397+
t.Fatalf("expected error but got none")
398+
case test.err:
399+
return
400+
}
401+
if !reflect.DeepEqual(test.expected, out) {
402+
t.Fatalf("unexpected value: %v != %v", test.expected, out)
403+
}
404+
})
405+
}
406+
}
407+
408+
func Test_DocumentIDStructEncoding(t *testing.T) {
409+
type typ struct {
410+
Id DocumentID `json:"id"`
411+
}
412+
tests := []struct {
413+
name string
414+
value typ
415+
expected string
416+
err bool
417+
}{
418+
{
419+
name: "appends spdxref",
420+
value: typ{
421+
Id: DocumentID("some-id"),
422+
},
423+
expected: `{"id":"DocumentRef-some-id"}`,
424+
},
425+
{
426+
name: "appends spdxref",
427+
value: typ{
428+
Id: DocumentID("DocumentRef-some-id"),
429+
},
430+
expected: `{"id":"DocumentRef-some-id"}`,
431+
},
432+
}
433+
434+
for _, test := range tests {
435+
t.Run(test.name, func(t *testing.T) {
436+
result, err := marshal.JSON(test.value)
437+
switch {
438+
case !test.err && err != nil:
439+
t.Fatalf("unexpected error: %v", err)
440+
case test.err && err == nil:
441+
t.Fatalf("expected error but got none")
442+
case test.err:
443+
return
444+
}
445+
s := string(result)
446+
if test.expected != s {
447+
t.Fatalf("%s != %s", test.expected, s)
448+
}
449+
})
450+
}
451+
}
452+
453+
func Test_DocumentIDStructDecoding(t *testing.T) {
454+
type typ struct {
455+
Id DocumentID `json:"id"`
456+
}
457+
tests := []struct {
458+
name string
459+
value string
460+
expected typ
461+
err bool
462+
}{
463+
{
464+
name: "valid id",
465+
expected: typ{
466+
Id: DocumentID("some-id"),
467+
},
468+
value: `{"id":"DocumentRef-some-id"}`,
469+
},
470+
{
471+
name: "without prefix",
472+
expected: typ{
473+
Id: DocumentID("some-id"),
474+
},
475+
value: `{"id":"some-id"}`,
476+
},
477+
}
478+
479+
for _, test := range tests {
480+
t.Run(test.name, func(t *testing.T) {
481+
out := typ{}
482+
err := json.Unmarshal([]byte(test.value), &out)
483+
switch {
484+
case !test.err && err != nil:
485+
t.Fatalf("unexpected error: %v", err)
486+
case test.err && err == nil:
487+
t.Fatalf("expected error but got none")
488+
case test.err:
489+
return
490+
}
491+
if !reflect.DeepEqual(test.expected, out) {
492+
t.Fatalf("unexpected value: %v != %v", test.expected, out)
493+
}
494+
})
495+
}
496+
}

0 commit comments

Comments
 (0)