Skip to content

Commit

Permalink
[ANCHOR-346] Fix trailing comma in assets JSON file causes a "null" a…
Browse files Browse the repository at this point in the history
…ssets (#1369)

### Description

- Fix trailing comma in assets JSON file causes a "null" assets

Although the trailing comma in JSON does not cause gson parsing failure,
it creates a `null` entry in the result `Assets` object. The PR removes
the `null` entry.

### Context

- Bug fixe.

### Testing
- `./gradlew test`

### Documentation
N/A
### Known limitations

N/A
  • Loading branch information
lijamie98 authored May 28, 2024
1 parent eba522b commit 652c680
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public static DefaultAssetService fromYaml(String assetsYaml) throws InvalidConf
public static DefaultAssetService fromJson(String assetsJson) throws InvalidConfigException {
DefaultAssetService assetService = new DefaultAssetService();
assetService.assets = gson.fromJson(assetsJson, Assets.class);
assetService.assets.assets.removeIf(Objects::isNull);
AssetServiceValidator.validate(assetService);
return assetService;
}
Expand All @@ -88,7 +89,6 @@ public List<AssetInfo> listAllAssets() {
@Override
public AssetInfo getAsset(String code) {
for (AssetInfo asset : assets.getAssets()) {
// FIXME: ANCHOR-346
if (asset != null && asset.getCode().equals(code)) {
return asset;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ internal class DefaultAssetServiceTest {
private val gson: Gson = GsonUtils.getInstance()

@BeforeEach fun setup() {}

@ParameterizedTest
@ValueSource(strings = ["test_assets.json", "test_assets.yaml"])
fun `test assets loading and listing`(filename: String) {
Expand Down Expand Up @@ -88,6 +89,12 @@ internal class DefaultAssetServiceTest {
}
}

@Test
fun `test trailing comma in JSON does not result in null element`() {
val assetsService = DefaultAssetService.fromJson(trailingCommaInAssets)
assert(assetsService.assets.assets.all { it != null })
}

// This is supposed to match the result from loading test_assets.json file.
private val expectedAssetsJson =
"""
Expand Down Expand Up @@ -387,3 +394,17 @@ internal class DefaultAssetServiceTest {
"""
.trimIndent()
}

val trailingCommaInAssets =
"""
{
"assets": [
{
"schema": "stellar",
"code": "native",
"significant_decimals": 7
},
]
}
"""
.trimIndent()

0 comments on commit 652c680

Please sign in to comment.