Skip to content

Commit 9eb274a

Browse files
authored
Merge pull request #773 from nscuro/fix-authors-serialization
2 parents 2dac786 + 645cc22 commit 9eb274a

3 files changed

Lines changed: 107 additions & 0 deletions

File tree

pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,13 @@
193193
<artifactId>junit-jupiter-params</artifactId>
194194
<scope>test</scope>
195195
</dependency>
196+
197+
<dependency>
198+
<groupId>net.javacrumbs.json-unit</groupId>
199+
<artifactId>json-unit-assertj</artifactId>
200+
<version>5.1.0</version>
201+
<scope>test</scope>
202+
</dependency>
196203
</dependencies>
197204

198205
<build>

src/main/java/org/cyclonedx/util/serializer/AuthorsSerializer.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,10 @@ public void serialize(List<OrganizationalContact> authors, JsonGenerator gen, Se
7474
gen.writeEndArray();
7575
}
7676
}
77+
78+
@Override
79+
public boolean isEmpty(SerializerProvider provider, List<OrganizationalContact> value) {
80+
return value == null || value.isEmpty();
81+
}
82+
7783
}

src/test/java/org/cyclonedx/Issue638ComprehensiveTest.java

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@
3131
import org.junit.jupiter.api.Test;
3232

3333
import java.util.Arrays;
34+
import java.util.Collections;
3435
import java.util.List;
3536

37+
import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson;
3638
import static org.assertj.core.api.Assertions.assertThat;
3739

3840
/**
@@ -202,6 +204,54 @@ public void testV15_XML_SerializeAuthorString() throws Exception {
202204
// VERSION 1.6 TESTS - Both 'author' (deprecated) and 'authors' exist
203205
// ============================================================================
204206

207+
@Test
208+
@DisplayName("v1.6 XML: Serialize component with null authors list")
209+
public void testV16_XML_SerializeNullAuthorsList() throws Exception {
210+
Component component = createComponentWithAuthorsList(
211+
null
212+
);
213+
Bom bom = createBom(component);
214+
215+
BomXmlGenerator generator = BomGeneratorFactory.createXml(Version.VERSION_16, bom);
216+
String xml = generator.toXmlString();
217+
218+
assertThat(xml).isEqualTo(/* language=XML */ """
219+
<?xml version="1.0" encoding="UTF-8"?>
220+
<bom serialNumber="urn:uuid:3e671687-395b-41f5-a30f-a58921a69b79" version="1" xmlns="http://cyclonedx.org/schema/bom/1.6">
221+
<components>
222+
<component type="library">
223+
<name>test-lib</name>
224+
<version>1.0.0</version>
225+
</component>
226+
</components>
227+
</bom>
228+
""");
229+
}
230+
231+
@Test
232+
@DisplayName("v1.6 XML: Serialize component with empty authors list")
233+
public void testV16_XML_SerializeEmptyAuthorsList() throws Exception {
234+
Component component = createComponentWithAuthorsList(
235+
Collections.emptyList()
236+
);
237+
Bom bom = createBom(component);
238+
239+
BomXmlGenerator generator = BomGeneratorFactory.createXml(Version.VERSION_16, bom);
240+
String xml = generator.toXmlString();
241+
242+
assertThat(xml).isEqualTo(/* language=XML */ """
243+
<?xml version="1.0" encoding="UTF-8"?>
244+
<bom serialNumber="urn:uuid:3e671687-395b-41f5-a30f-a58921a69b79" version="1" xmlns="http://cyclonedx.org/schema/bom/1.6">
245+
<components>
246+
<component type="library">
247+
<name>test-lib</name>
248+
<version>1.0.0</version>
249+
</component>
250+
</components>
251+
</bom>
252+
""");
253+
}
254+
205255
@Test
206256
@DisplayName("v1.6 XML: Serialize component with ONLY author string (deprecated field)")
207257
public void testV16_XML_SerializeOnlyAuthorString() throws Exception {
@@ -421,6 +471,50 @@ public void testV16_JSON_SerializeOnlyAuthorString() throws Exception {
421471
assertThat(json).doesNotContain("\"authors\"");
422472
}
423473

474+
@Test
475+
@DisplayName("v1.6 JSON: Serialize component with null authors list")
476+
public void testV16_JSON_SerializeNullAuthorsList() throws Exception {
477+
Component component = createComponentWithAuthorsList(
478+
null
479+
);
480+
Bom bom = createBom(component);
481+
482+
BomJsonGenerator generator = BomGeneratorFactory.createJson(Version.VERSION_16, bom);
483+
String json = generator.toJsonString();
484+
485+
assertThatJson(json)
486+
.inPath("$.components[0]")
487+
.isEqualTo(/* language=JSON */ """
488+
{
489+
"type": "library",
490+
"name": "test-lib",
491+
"version": "1.0.0"
492+
}
493+
""");
494+
}
495+
496+
@Test
497+
@DisplayName("v1.6 JSON: Serialize component with empty authors list")
498+
public void testV16_JSON_SerializeEmptyAuthorsList() throws Exception {
499+
Component component = createComponentWithAuthorsList(
500+
Collections.emptyList()
501+
);
502+
Bom bom = createBom(component);
503+
504+
BomJsonGenerator generator = BomGeneratorFactory.createJson(Version.VERSION_16, bom);
505+
String json = generator.toJsonString();
506+
507+
assertThatJson(json)
508+
.inPath("$.components[0]")
509+
.isEqualTo(/* language=JSON */ """
510+
{
511+
"type": "library",
512+
"name": "test-lib",
513+
"version": "1.0.0"
514+
}
515+
""");
516+
}
517+
424518
@Test
425519
@DisplayName("v1.6 JSON: Serialize component with ONLY authors list")
426520
public void testV16_JSON_SerializeOnlyAuthorsList() throws Exception {

0 commit comments

Comments
 (0)