Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

/**
Expand Down Expand Up @@ -183,7 +184,12 @@ public ModuleMetadata deserialize(JsonElement json, Type typeOfT, JsonDeserializ
metadata.setDescription(context.<I18nMap>deserialize(entry.getValue(), I18nMap.class));
break;
case ModuleMetadata.DEPENDENCIES:
metadata.getDependencies().addAll((List<DependencyInfo>) context.deserialize(entry.getValue(), DEPENDENCY_LIST_TYPE));
// Trailing comma -> Gson emits a phantom null element; strip it (#133).
List<DependencyInfo> dependencyInfos = context.deserialize(entry.getValue(), DEPENDENCY_LIST_TYPE);
if (dependencyInfos != null) {
dependencyInfos.removeIf(Objects::isNull);
metadata.getDependencies().addAll(dependencyInfos);
}
break;
case ModuleMetadata.REQUIRED_PERMISSIONS:
metadata.getRequiredPermissions().addAll((Set<String>) context.deserialize(entry.getValue(), STRING_SET_TYPE));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,22 @@ public void testReadWrite() {

Assert.assertEquals(meta, parsedMeta);
}

@Test
public void testTrailingCommaInDependenciesDoesNotProduceNullElement() {
// Gson's lenient reader accepts this invalid JSON (trailing comma) and would
// otherwise leave a null element in the dependencies list. See #133.
String json = "{"
+ "\"id\": \"ModuleNameId\","
+ "\"dependencies\": ["
+ "{\"id\": \"myDependency\", \"minVersion\": \"1.0.0\", \"maxVersion\": \"2.0.0\"},"
+ "]"
+ "}";

ModuleMetadataJsonAdapter adapter = new ModuleMetadataJsonAdapter();
ModuleMetadata parsedMeta = adapter.read(new StringReader(json));

Assert.assertEquals(1, parsedMeta.getDependencies().size());
Assert.assertFalse(parsedMeta.getDependencies().contains(null));
}
}