Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use double for metadata numbers #811

Merged
merged 6 commits into from
Jun 5, 2020
Merged
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
13 changes: 7 additions & 6 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# Changelog

## Next Release
- Add support for setting Tracking Codes ([#766](https://github.com/box/box-java-sdk/pull/766))
- Fix issue for `getIsExternallyOwned()` for Files and Folders ([#808](https://github.com/box/box-java-sdk/pull/808))
- Add support for the classification field for Files and Folders ([#809](https://github.com/box/box-java-sdk/pull/809))
- Add ability to set the filename when uploading a new version of a file ([#810](https://github.com/box/box-java-sdk/pull/810))
- Add iterator support for group collaborations ([#813](https://github.com/box/box-java-sdk/pull/813))
## Next Release [MINOR]
- [MINOR] Add support for setting Tracking Codes ([#766](https://github.com/box/box-java-sdk/pull/766))
- [PATCH] Fix issue for `getIsExternallyOwned()` for Files and Folders ([#808](https://github.com/box/box-java-sdk/pull/808))
- [MINOR] Add support for the classification field for Files and Folders ([#809](https://github.com/box/box-java-sdk/pull/809))
- [MINOR] Add ability to set the filename when uploading a new version of a file ([#810](https://github.com/box/box-java-sdk/pull/810))
- [MINOR] Deprecate the use of float for Metadata values, in preference of the underlying value (double) ([#811](https://github.com/box/box-java-sdk/pull/811))
- [MINOR] Add iterator support for group collaborations ([#813](https://github.com/box/box-java-sdk/pull/813))

## 2.47.0 [2020-04-23]
- Add support for the uploader display name field for Files and File Versions ([#791](https://github.com/box/box-java-sdk/pull/791))
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/box/sdk/BoxFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -1117,7 +1117,7 @@ public Metadata setMetadata(String templateName, String scope, Metadata metadata
for (JsonValue value : metadata.getOperations()) {
if (value.asObject().get("value").isNumber()) {
metadataToUpdate.add(value.asObject().get("path").asString(),
value.asObject().get("value").asFloat());
value.asObject().get("value").asDouble());
} else if (value.asObject().get("value").isString()) {
metadataToUpdate.add(value.asObject().get("path").asString(),
value.asObject().get("value").asString());
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/box/sdk/BoxFolder.java
Original file line number Diff line number Diff line change
Expand Up @@ -876,7 +876,7 @@ public Metadata setMetadata(String templateName, String scope, Metadata metadata
for (JsonValue value : metadata.getOperations()) {
if (value.asObject().get("value").isNumber()) {
metadataToUpdate.add(value.asObject().get("path").asString(),
value.asObject().get("value").asFloat());
value.asObject().get("value").asDouble());
} else if (value.asObject().get("value").isString()) {
metadataToUpdate.add(value.asObject().get("path").asString(),
value.asObject().get("value").asString());
Expand Down
58 changes: 56 additions & 2 deletions src/main/java/com/box/sdk/Metadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,28 @@ public Metadata add(String path, String value) {
* @param path the path that designates the key. Must be prefixed with a "/".
* @param value the value.
* @return this metadata object.
* @deprecated add(String, double) is preferred as it avoids errors when converting a
* float to the underlying data type used by Metadata (double)
*/
@Deprecated
public Metadata add(String path, float value) {
this.values.add(this.pathToProperty(path), value);
this.addOp("add", path, value);
return this;
}

/**
* Adds a new metadata value.
* @param path the path that designates the key. Must be prefixed with a "/".
* @param value the value.
* @return this metadata object.
*/
public Metadata add(String path, double value) {
this.values.add(this.pathToProperty(path), value);
this.addOp("add", path, value);
return this;
}

/**
* Adds a new metadata value of array type.
* @param path the path to the field.
Expand Down Expand Up @@ -225,6 +240,18 @@ public Metadata replace(String path, float value) {
return this;
}

/**
* Replaces an existing metadata value.
* @param path the path that designates the key. Must be prefixed with a "/".
* @param value the value.
* @return this metadata object.
*/
public Metadata replace(String path, double value) {
this.values.set(this.pathToProperty(path), value);
this.addOp("replace", path, value);
return this;
}

/**
* Replaces an existing metadata value of array type.
* @param path the path that designates the key. Must be prefixed with a "/".
Expand Down Expand Up @@ -316,15 +343,26 @@ public String getString(String path) {
}

/**
* Get a value from a float metadata field.
* Get a value from a double metadata field.
* @param path the key path in the metadata object. Must be prefixed with a "/".
* @return the metadata value as a floating point number.
* @return the metadata value as a double floating point number.
* @deprecated getDouble() is preferred as it more clearly describes the return type (double)
*/
@Deprecated
public double getFloat(String path) {
// @NOTE(mwiller) 2018-02-05: JS number are all 64-bit floating point, so double is the correct type to use here
return this.getValue(path).asDouble();
}

/**
* Get a value from a double metadata field.
* @param path the key path in the metadata object. Must be prefixed with a "/".
* @return the metadata value as a floating point number.
*/
public double getDouble(String path) {
return this.getValue(path).asDouble();
}

/**
* Get a value from a date metadata field.
* @param path the key path in the metadata object. Must be prefixed with a "/".
Expand Down Expand Up @@ -452,6 +490,22 @@ private void addOp(String op, String path, float value) {
.add("value", value));
}

/**
* Adds a patch operation.
* @param op the operation type. Must be add, replace, remove, or test.
* @param path the path that designates the key. Must be prefixed with a "/".
* @param value the value to be set.
*/
private void addOp(String op, String path, double value) {
if (this.operations == null) {
this.operations = new JsonArray();
}

this.operations.add(new JsonObject()
.add("op", op)
.add("path", path)
.add("value", value));
}
/**
* Adds a new patch operation for array values.
* @param op the operation type. Must be add, replace, remove, or test.
Expand Down
8 changes: 5 additions & 3 deletions src/test/Fixtures/BoxFile/UpdateMetadataOnFile200.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
{
"test":"text",
"test2":2,
"test3":["first", "second", "third"],
"test1":"text",
"test2":["first", "second", "third"],
"test3":2,
"test4":1.23456794E9,
"test5":2.33333333333333344E17,
"$type":"account-e68798f0-347f-4882-a1ae-f65b032780ac",
"$parent":"file_12345",
"$id":"0db91053-c355-4fc7-b69c-9d511974bbc4",
Expand Down
8 changes: 5 additions & 3 deletions src/test/Fixtures/BoxFolder/UpdateMetadataOnFolder200.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
{
"test":"text",
"test2":2,
"test3":["first", "second", "third"],
"test1":"text",
"test2":["first", "second", "third"],
"test3":2,
"test4":1.23456794E9,
"test5":2.33333333333333344E17,
"$type":"account-e68798f0-347f-4882-a1ae-f65b032780ac",
"$parent":"folder_12345",
"$id":"0db91053-c355-4fc7-b69c-9d511974bbc4",
Expand Down
50 changes: 36 additions & 14 deletions src/test/java/com/box/sdk/BoxFileTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1857,38 +1857,54 @@ public void testSetMetadataReturnsCorrectly() throws IOException {
String putResult = "";
final String fileID = "12345";
final String metadataURL = "/files/" + fileID + "/metadata/enterprise/testtemplate";
ArrayList<String> values = new ArrayList<String>();
values.add("first");
values.add("second");
values.add("third");
ArrayList<String> secondValueArray = new ArrayList<String>();
secondValueArray.add("first");
secondValueArray.add("second");
secondValueArray.add("third");

postResult = TestConfig.getFixture("/BoxException/BoxResponseException409");
putResult = TestConfig.getFixture("/BoxFile/UpdateMetadataOnFile200");

JsonArray array = new JsonArray()
final String firstValue = "text";
JsonArray secondValueJson = new JsonArray()
.add("first")
.add("second")
.add("third");
final int thirdValue = 2;
final float fourthValue = 1234567890f;
final double fifthValue = 233333333333333340.0;

JsonObject firstAttribute = new JsonObject()
.add("op", "add")
.add("path", "/test")
.add("value", "text");
.add("path", "/test1")
.add("value", firstValue);

JsonObject secondAttribute = new JsonObject()
.add("op", "add")
.add("path", "/test2")
.add("value", 2);
.add("value", secondValueJson);

JsonObject thirdAttribute = new JsonObject()
.add("op", "add")
.add("path", "/test3")
.add("value", array);
.add("value", thirdValue);

JsonObject fourthAttribute = new JsonObject()
.add("op", "add")
.add("path", "/test4")
.add("value", fourthValue);

JsonObject fifthAttribute = new JsonObject()
.add("op", "add")
.add("path", "/test5")
.add("value", fifthValue);

JsonArray jsonArray = new JsonArray()
.add(firstAttribute)
.add(secondAttribute)
.add(thirdAttribute);
.add(thirdAttribute)
.add(fourthAttribute)
.add(fifthAttribute);

WIRE_MOCK_CLASS_RULE.stubFor(WireMock.post(WireMock.urlPathEqualTo(metadataURL))
.willReturn(WireMock.aResponse()
Expand All @@ -1907,16 +1923,22 @@ public void testSetMetadataReturnsCorrectly() throws IOException {
BoxFile file = new BoxFile(this.api, "12345");

Metadata metadata = new Metadata()
.add("/test", "text")
.add("/test2", 2)
.add("/test3", values);
.add("/test1", firstValue)
.add("/test2", secondValueArray)
.add("/test3", thirdValue)
.add("/test4", fourthValue)
.add("/test5", fifthValue);

Metadata metadataValues = file.setMetadata("testtemplate", "enterprise", metadata);

Assert.assertEquals("file_12345", metadataValues.getParentID());
Assert.assertEquals("testtemplate", metadataValues.getTemplateName());
Assert.assertEquals("enterprise_11111", metadataValues.getScope());
Assert.assertEquals("text", metadataValues.getString("/test"));
Assert.assertEquals(firstValue, metadataValues.getString("/test1"));
Assert.assertEquals(secondValueJson, metadataValues.getValue("/test2"));
Assert.assertEquals((double) thirdValue, metadataValues.getFloat("/test3"), 0);
Assert.assertEquals((double) fourthValue, metadataValues.getFloat("/test4"), 4);
Assert.assertEquals((double) fifthValue, metadataValues.getFloat("/test5"), 0);
}

@Test
Expand Down
48 changes: 35 additions & 13 deletions src/test/java/com/box/sdk/BoxFolderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1847,38 +1847,54 @@ public void testSetMetadataReturnsCorrectly() throws IOException {
String putResult = "";
final String folderID = "12345";
final String metadataURL = "/folders/" + folderID + "/metadata/enterprise/testtemplate";
ArrayList<String> values = new ArrayList<String>();
values.add("first");
values.add("second");
values.add("third");
ArrayList<String> secondValueArray = new ArrayList<String>();
secondValueArray.add("first");
secondValueArray.add("second");
secondValueArray.add("third");

postResult = TestConfig.getFixture("/BoxException/BoxResponseException409");
putResult = TestConfig.getFixture("/BoxFolder/UpdateMetadataOnFolder200");

JsonArray array = new JsonArray()
final String firstValue = "text";
JsonArray secondValueJson = new JsonArray()
.add("first")
.add("second")
.add("third");
final int thirdValue = 2;
final float fourthValue = 1234567890f;
final double fifthValue = 233333333333333340.0;

JsonObject firstAttribute = new JsonObject()
.add("op", "add")
.add("path", "/test")
.add("path", "/test1")
.add("value", "text");

JsonObject secondAttribute = new JsonObject()
.add("op", "add")
.add("path", "/test2")
.add("value", 2);
.add("value", secondValueJson);

JsonObject thirdAttribute = new JsonObject()
.add("op", "add")
.add("path", "/test3")
.add("value", array);
.add("value", thirdValue);

JsonObject fourthAttribute = new JsonObject()
.add("op", "add")
.add("path", "/test4")
.add("value", fourthValue);

JsonObject fifthAttribute = new JsonObject()
.add("op", "add")
.add("path", "/test5")
.add("value", fifthValue);

JsonArray jsonArray = new JsonArray()
.add(firstAttribute)
.add(secondAttribute)
.add(thirdAttribute);
.add(thirdAttribute)
.add(fourthAttribute)
.add(fifthAttribute);

WIRE_MOCK_CLASS_RULE.stubFor(WireMock.post(WireMock.urlPathEqualTo(metadataURL))
.willReturn(WireMock.aResponse()
Expand All @@ -1897,16 +1913,22 @@ public void testSetMetadataReturnsCorrectly() throws IOException {
BoxFolder folder = new BoxFolder(this.api, "12345");

Metadata metadata = new Metadata()
.add("/test", "text")
.add("/test2", 2)
.add("/test3", values);
.add("/test1", firstValue)
.add("/test2", secondValueArray)
.add("/test3", thirdValue)
.add("/test4", fourthValue)
.add("/test5", fifthValue);

Metadata metadataValues = folder.setMetadata("testtemplate", "enterprise", metadata);

Assert.assertEquals("folder_12345", metadataValues.getParentID());
Assert.assertEquals("testtemplate", metadataValues.getTemplateName());
Assert.assertEquals("enterprise_11111", metadataValues.getScope());
Assert.assertEquals("text", metadataValues.getString("/test"));
Assert.assertEquals(firstValue, metadataValues.getString("/test1"));
Assert.assertEquals(secondValueJson, metadataValues.getValue("/test2"));
Assert.assertEquals((double) thirdValue, metadataValues.getFloat("/test3"), 0);
Assert.assertEquals((double) fourthValue, metadataValues.getFloat("/test4"), 4);
Assert.assertEquals((double) fifthValue, metadataValues.getFloat("/test5"), 0);
}

@Test(expected = BoxDeserializationException.class)
Expand Down
Loading