From c63f3b297b0965c4b0eae49595fdd65c6d12cf46 Mon Sep 17 00:00:00 2001 From: Kathrin Geilmann Date: Mon, 26 Oct 2020 22:58:34 +0100 Subject: [PATCH] Fix #1228: Added explicit checks for Null on JsonObject This fix works around the limitation of minimal-json, that null as default is not handled (see https://github.com/ralfstx/minimal-json/issues/103#issue-343117716) --- .../java/io/cucumber/createmeta/CreateMeta.java | 16 +++++++++++----- .../io/cucumber/createmeta/CreateMetaTest.java | 17 +++++++++++++++++ create-meta/testdata/GoCD.txt | 2 ++ create-meta/testdata/GoCD.txt.json | 7 +++++++ 4 files changed, 37 insertions(+), 5 deletions(-) create mode 100644 create-meta/testdata/GoCD.txt create mode 100644 create-meta/testdata/GoCD.txt.json diff --git a/create-meta/java/src/main/java/io/cucumber/createmeta/CreateMeta.java b/create-meta/java/src/main/java/io/cucumber/createmeta/CreateMeta.java index 7e9ef0b56a..3cf0b9e042 100644 --- a/create-meta/java/src/main/java/io/cucumber/createmeta/CreateMeta.java +++ b/create-meta/java/src/main/java/io/cucumber/createmeta/CreateMeta.java @@ -2,6 +2,7 @@ import com.eclipsesource.json.Json; import com.eclipsesource.json.JsonObject; +import com.eclipsesource.json.JsonValue; import io.cucumber.messages.Messages; import io.cucumber.messages.ProtocolVersion; @@ -81,13 +82,13 @@ static Messages.Meta.CI detectCI(Map env) { } private static Messages.Meta.CI createCi(String name, JsonObject ciSystem, Map env) { - String url = evaluate(ciSystem.getString("url", null), env); + String url = evaluate(getString(ciSystem, "url"), env); if (url == null) return null; JsonObject git = ciSystem.get("git").asObject(); - String remote = removeUserInfoFromUrl(evaluate(git.getString("remote", null), env)); - String revision = evaluate(git.getString("revision", null), env); - String branch = evaluate(git.getString("branch", null), env); - String tag = evaluate(git.getString("tag", null), env); + String remote = removeUserInfoFromUrl(evaluate(getString(git, "remote"), env)); + String revision = evaluate(getString(git, "revision"), env); + String branch = evaluate(getString(git, "branch"), env); + String tag = evaluate(getString(git, "tag"), env); Messages.Meta.CI.Builder ciBuilder = Messages.Meta.CI.newBuilder() .setName(name) @@ -144,4 +145,9 @@ private static String group1(String value, Pattern pattern) { } return matcher.find() ? matcher.group(1) : null; } + + private static String getString(JsonObject json, String name) { + JsonValue val = json.get(name); + return val.isNull() ? null : val.asString(); + } } diff --git a/create-meta/java/src/test/java/io/cucumber/createmeta/CreateMetaTest.java b/create-meta/java/src/test/java/io/cucumber/createmeta/CreateMetaTest.java index bf58549d7f..67c199d3b7 100644 --- a/create-meta/java/src/test/java/io/cucumber/createmeta/CreateMetaTest.java +++ b/create-meta/java/src/test/java/io/cucumber/createmeta/CreateMetaTest.java @@ -56,4 +56,21 @@ void it_detects_github_actions() { meta.getCi()); } + @Test + void can_handle_CIs_with_null_in_ciDict() { + // choose gocd as example, which has null everywhere except for url and revision + HashMap env = new HashMap() {{ + put("GO_SERVER_URL", "https:///buildurl"); + put("GO_REVISION", "the-revision"); + }}; + Messages.Meta meta = CreateMeta.createMeta("cucumber-jvm", "3.2.1", env); + assertEquals(Messages.Meta.CI.newBuilder() + .setName("GoCD") + .setUrl("https:///buildurl/???") + .setGit(Messages.Meta.CI.Git.newBuilder() + .setRevision("the-revision") + ) + .build(), + meta.getCi()); + } } diff --git a/create-meta/testdata/GoCD.txt b/create-meta/testdata/GoCD.txt new file mode 100644 index 0000000000..f4e2aaf87c --- /dev/null +++ b/create-meta/testdata/GoCD.txt @@ -0,0 +1,2 @@ +GO_SERVER_URL=https://cihost.com/path/to/the/build +GO_REVISION=decafbad \ No newline at end of file diff --git a/create-meta/testdata/GoCD.txt.json b/create-meta/testdata/GoCD.txt.json new file mode 100644 index 0000000000..fe99978738 --- /dev/null +++ b/create-meta/testdata/GoCD.txt.json @@ -0,0 +1,7 @@ +{ + "git": { + "revision": "decafbad" + }, + "name": "GoCD", + "url": "https://cihost.com/path/to/the/build/???" +}