Repository: ontodev/robot (java)

Issue:
--add-prefixes does not work correctly
It seems to me that the `--add-prefixes` doesn't work as intended. In e.g. `template`, the prefixes are in fact added to the namespaces, but they are not used in the rest of the output. Looking at the output of `export-prefixes` confirms that something is amiss:

**Command:**
`java -jar ./bin/robot.jar --noprefixes export-prefixes --add-prefixes prefixes.json`

**Expected output:** (and content of prefixes.json)
```
{
  "@context" : {
    "natox" : "http://natox.be",
  }
}
```
**Actual output:**
`{ }`

**Workaround:**

Add a (dummy) prefix with `--add-prefix` too. Then the prefixes specified with `--add-prefixes` also show up.
```
$ java -jar ./bin/robot.jar --noprefixes export-prefixes --add-prefixes prefixes.json --add-prefix "foo: http://bar"
{
  "@context" : {
    "natox" : "http://natox.be",
    "foo" : "http://bar"
  }
}
```

**Potential fix?**

Is there a `setContext` missing in `IOHelper#addPrefixes`? When using the workaround given above, the `setContext` is called in `IOHelper#addPrefix`, fixing the issue.

```diff
diff --git a/robot-core/src/main/java/org/obolibrary/robot/IOHelper.java b/robot-core/src/main/java/org/obolibrary/robot/IOHelper.java
index b5fb450..a298b55 100644
--- a/robot-core/src/main/java/org/obolibrary/robot/IOHelper.java
+++ b/robot-core/src/main/java/org/obolibrary/robot/IOHelper.java
@@ -962,6 +962,8 @@ public class IOHelper {
     }
     Context context1 = parseContext(FileUtils.readFileToString(prefixFile));
     context.putAll(context1.getPrefixes(false));
+    context.remove("@base");
+    setContext((Map<String, Object>) context);
   }
 
   /**
```

Pull request description:
Fix add-prefixes and add test
Resolves #714

- [ ] `docs/` have been added/updated
- [x] tests have been added/updated
- [x] `mvn verify` says all tests pass
- [ ] `mvn site` says all JavaDocs correct
- [x] `CHANGELOG.md` has been updated


Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index f3cab7e7..69b9eb53 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -23,6 +23,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 - Reduced time spent loading datasets for [`query`] in [#666]
 - Fix writing JSON format to use `OutputStream` with ['convert'] in [#671]
 - Fix IRI resolution for `template` in [#689]
+- Fix issue with `--add-prefixes` option in [#715]
 
 
 ## [1.6.0] - 2020-03-04
@@ -187,6 +188,7 @@ First official release of ROBOT!
 [`report`]: http://robot.obolibrary.org/report
 [`template`]: http://robot.obolibrary.org/template
 
+[#715]: https://github.com/ontodev/robot/pull/715
 [#689]: https://github.com/ontodev/robot/pull/689
 [#685]: https://github.com/ontodev/robot/pull/685
 [#671]: https://github.com/ontodev/robot/pull/671
diff --git a/robot-core/src/main/java/org/obolibrary/robot/IOHelper.java b/robot-core/src/main/java/org/obolibrary/robot/IOHelper.java
index c6133f7d..b495b3b8 100644
--- a/robot-core/src/main/java/org/obolibrary/robot/IOHelper.java
+++ b/robot-core/src/main/java/org/obolibrary/robot/IOHelper.java
@@ -1095,7 +1095,19 @@ public class IOHelper {
       throw new IOException(String.format(fileDoesNotExistError, prefixPath));
     }
     Context context1 = parseContext(FileUtils.readFileToString(prefixFile));
+    addPrefixes(context1);
+  }
+
+  /**
+   * Given a Context, add the prefix mappings to the current JSON-LD context.
+   *
+   * @param context1 Context to add
+   * @throws IOException if the Context cannot be set
+   */
+  public void addPrefixes(Context context1) throws IOException {
     context.putAll(context1.getPrefixes(false));
+    context.remove("@base");
+    setContext((Map<String, Object>) context);
   }
 
   /**


Test patch:
diff --git a/robot-core/src/test/java/org/obolibrary/robot/IOHelperTest.java b/robot-core/src/test/java/org/obolibrary/robot/IOHelperTest.java
index cc9a9912..ce8cf6be 100644
--- a/robot-core/src/test/java/org/obolibrary/robot/IOHelperTest.java
+++ b/robot-core/src/test/java/org/obolibrary/robot/IOHelperTest.java
@@ -20,6 +20,27 @@ import org.semanticweb.owlapi.util.DefaultPrefixManager;
 
 /** Tests for IOHelper. */
 public class IOHelperTest extends CoreTest {
+
+  /**
+   * Test adding prefixes using the addPrefixes method
+   *
+   * @throws IOException on problem creating IOHelper or adding prefixes
+   */
+  @Test
+  public void testAddPrefixes() throws IOException {
+    // IOHelper without default prefixes
+    IOHelper ioh = new IOHelper(false);
+
+    // Single prefix to add from a context
+    String inputContext = "{\n  \"@context\" : {\n    \"foo\" : \"http://foo.bar\"\n  }\n}";
+    Context context = IOHelper.parseContext(inputContext);
+    ioh.addPrefixes(context);
+
+    // Get the context back from IOHelper
+    String outputContext = ioh.getContextString();
+    assertEquals(inputContext, outputContext);
+  }
+
   /**
    * Test loading JSON files.
    *