Skip to content

Commit

Permalink
changes
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-zepol committed Jun 17, 2024
1 parent e6afd05 commit ec19c28
Show file tree
Hide file tree
Showing 10 changed files with 2,555 additions and 248 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.cyclonedx.model.Component;
import org.cyclonedx.model.component.Component12Mixin;
import org.cyclonedx.model.component.Component16Mixin;
import org.cyclonedx.util.serializer.CustomEnumSerializerModifier;
import org.cyclonedx.util.serializer.CustomSerializerModifier;
import org.cyclonedx.util.serializer.EnvironmentVarsSerializer;
import org.cyclonedx.util.serializer.EvidenceSerializer;
Expand Down Expand Up @@ -43,6 +44,9 @@ public Version getSchemaVersion() {
}

protected void setupObjectMapper(boolean isXml) {
mapper.addMixIn(Component.class, Component12Mixin.class);
mapper.addMixIn(Component.class, Component16Mixin.class);

SimpleModule licenseModule = new SimpleModule();
licenseModule.addSerializer(new LicenseChoiceSerializer(isXml, version));
mapper.registerModule(licenseModule);
Expand Down Expand Up @@ -102,5 +106,8 @@ protected void setupObjectMapper(boolean isXml) {
mapper.addMixIn(Component.class, Component12Mixin.class);
mapper.addMixIn(Component.class, Component16Mixin.class);

SimpleModule module = new SimpleModule();
module.setSerializerModifier(new CustomEnumSerializerModifier(version));
//mapper.registerModule(module);
}
}
8 changes: 8 additions & 0 deletions src/main/java/org/cyclonedx/model/Mandatory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.cyclonedx.model;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)
public @interface Mandatory {
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ public abstract class Component12Mixin
{
@JsonProperty("author")
@VersionFilter(Version.VERSION_12)
abstract String getOldAuthor();
abstract String getAuthor();
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializationConfig;
import com.fasterxml.jackson.databind.ser.BeanPropertyWriter;
import com.fasterxml.jackson.databind.ser.BeanSerializerModifier;
import org.cyclonedx.Version;
import org.cyclonedx.model.VersionFilter;
;
import java.util.ArrayList;
import java.util.List;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.EnumSerializer;

import java.io.IOException;

public class CustomEnumSerializerModifier extends BeanSerializerModifier {

Expand All @@ -22,39 +24,29 @@ public CustomEnumSerializerModifier(Version currentVersion) {

@Override
public JsonSerializer<?> modifyEnumSerializer(SerializationConfig config, JavaType type, BeanDescription beanDesc, JsonSerializer<?> serializer) {
return new VersionFilteringEnumSerializer(currentVersion);
return new VersionFilteringEnumSerializer((EnumSerializer) serializer, currentVersion);
}

@Override
public List<BeanPropertyWriter> changeProperties(SerializationConfig config, BeanDescription beanDesc, List<BeanPropertyWriter> beanProperties) {
List<BeanPropertyWriter> newProperties = new ArrayList<>();
for (BeanPropertyWriter writer : beanProperties) {
if (shouldInclude(writer)) {
newProperties.add(writer);
}
}
return newProperties;
}
public static class VersionFilteringEnumSerializer extends JsonSerializer<Enum<?>> {

private final EnumSerializer defaultSerializer;
private final Version currentVersion;

private boolean shouldInclude(BeanPropertyWriter writer) {
// Check for VersionFilter annotation and current version here
VersionFilter versionFilter = writer.getAnnotation(VersionFilter.class);
if (versionFilter != null && versionFilter.value().compareTo(currentVersion) > 0) {
return false;
public VersionFilteringEnumSerializer(EnumSerializer defaultSerializer, Version currentVersion) {
this.defaultSerializer = defaultSerializer;
this.currentVersion = currentVersion;
}

// Check if the property is an Enum with VersionFilter annotation
if (Enum.class.isAssignableFrom(writer.getType().getRawClass())) {
@Override
public void serialize(Enum<?> value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
try {
Enum<?> enumValue = (Enum<?>) writer.get(null);
VersionFilter enumVersionFilter = enumValue.getClass().getField(enumValue.name()).getAnnotation(VersionFilter.class);
if (enumVersionFilter != null && enumVersionFilter.value().compareTo(currentVersion) > 0) {
return false;
VersionFilter versionFilter = value.getClass().getField(value.name()).getAnnotation(VersionFilter.class);
if (versionFilter == null || versionFilter.value().compareTo(currentVersion) <= 0) {
defaultSerializer.serialize(value, gen, serializers);
}
} catch (Exception e) {
// Handle exception
}catch (NoSuchFieldException e) {
defaultSerializer.serialize(value, gen, serializers);
}
}
return true;
}
}
12 changes: 6 additions & 6 deletions src/test/java/org/cyclonedx/BomJsonGeneratorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,11 @@ private static Stream<Arguments> versionAndBomProvider() {
Arguments.of(Version.VERSION_12, "/bom-1.5.json"),

// Backward compatibility tests from BOM 1.6
Arguments.of(Version.VERSION_16, "/1.6/valid-bom-1.6.json"),
Arguments.of(Version.VERSION_15, "/1.6/valid-bom-1.6.json"),
Arguments.of(Version.VERSION_14, "/1.6/valid-bom-1.6.json"),
Arguments.of(Version.VERSION_13, "/1.6/valid-bom-1.6.json"),
Arguments.of(Version.VERSION_12, "/1.6/valid-bom-1.6.json")
Arguments.of(Version.VERSION_16, "/bom-1.6.json"),
Arguments.of(Version.VERSION_15, "/bom-1.6.json"),
Arguments.of(Version.VERSION_14, "/bom-1.6.json"),
Arguments.of(Version.VERSION_13, "/bom-1.6.json"),
Arguments.of(Version.VERSION_12, "/bom-1.6.json")
);
}

Expand All @@ -225,7 +225,7 @@ private static Stream<Arguments> versionAndBom() {
Arguments.of(Version.VERSION_13, "/bom-1.3.json"),
Arguments.of(Version.VERSION_14, "/bom-1.4.json"),
Arguments.of(Version.VERSION_15, "/bom-1.5.json"),
Arguments.of(Version.VERSION_16, "/1.6/valid-bom-1.6.json")
Arguments.of(Version.VERSION_16, "/bom-1.6.json")
);
}

Expand Down
14 changes: 7 additions & 7 deletions src/test/java/org/cyclonedx/BomXmlGeneratorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -386,12 +386,12 @@ private static Stream<Arguments> versionAndBomProvider() {
Arguments.of(Version.VERSION_11, "/bom-1.5.xml"),

// Backward compatibility tests from BOM 1.5
Arguments.of(Version.VERSION_16, "/1.6/valid-bom-1.6.xml"),
Arguments.of(Version.VERSION_15, "/1.6/valid-bom-1.6.xml"),
Arguments.of(Version.VERSION_14, "/1.6/valid-bom-1.6.xml"),
Arguments.of(Version.VERSION_13, "/1.6/valid-bom-1.6.xml"),
Arguments.of(Version.VERSION_12, "/1.6/valid-bom-1.6.xml"),
Arguments.of(Version.VERSION_11, "/1.6/valid-bom-1.6.xml")
Arguments.of(Version.VERSION_16, "/bom-1.6.xml"),
Arguments.of(Version.VERSION_15, "/bom-1.6.xml"),
Arguments.of(Version.VERSION_14, "/bom-1.6.xml"),
Arguments.of(Version.VERSION_13, "/bom-1.6.xml"),
Arguments.of(Version.VERSION_12, "/bom-1.6.xml"),
Arguments.of(Version.VERSION_11, "/bom-1.6.xml")
);
}

Expand Down Expand Up @@ -654,7 +654,7 @@ private static Stream<Arguments> versionAndBom() {
Arguments.of(Version.VERSION_13, "/bom-1.3.xml"),
Arguments.of(Version.VERSION_14, "/bom-1.4.xml"),
Arguments.of(Version.VERSION_15, "/bom-1.5.xml"),
Arguments.of(Version.VERSION_16, "/1.6/valid-bom-1.6.xml")
Arguments.of(Version.VERSION_16, "/bom-1.6.xml")
);
}

Expand Down
4 changes: 2 additions & 2 deletions src/test/java/org/cyclonedx/parse/XmlParseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public Collection<DynamicTest> dynamicTestsWithCollection() {
final List<File> files = getAllResources();
final List<DynamicTest> dynamicTests = new ArrayList<>();
for (final File file : files) {
if (file.getName().endsWith(".xml")) {
if (file.getName().startsWith("valid")) {
if (file.getName().endsWith("1.6.xml")) {
if (file.getName().startsWith("valid-machine")) {
//dynamicTests.add(DynamicTest.dynamicTest(file.getName(), () -> assertNotNull(parseBom(file))));
dynamicTests.add(DynamicTest.dynamicTest(file.getName(), () -> {
final Bom bom = parseBom(file);
Expand Down
15 changes: 7 additions & 8 deletions src/test/resources/bom-1.5.xml

Large diffs are not rendered by default.

Loading

0 comments on commit ec19c28

Please sign in to comment.