Skip to content

Commit

Permalink
fix(api): numeric/bignumeric conversion issue #1757 (#1768)
Browse files Browse the repository at this point in the history
  • Loading branch information
sandhusanthakumar authored Aug 30, 2022
1 parent 40e0ba0 commit 5cc96a0
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ private static void fillField(
protoMsg.setField(
fieldDescriptor,
BigDecimalByteStringEncoder.encodeToNumericByteString(
new BigDecimal(((Number) val).doubleValue())));
new BigDecimal(String.valueOf(val))));
return;
}
} else if (fieldSchema.getType() == TableFieldSchema.Type.BIGNUMERIC) {
Expand All @@ -333,7 +333,7 @@ private static void fillField(
protoMsg.setField(
fieldDescriptor,
BigDecimalByteStringEncoder.encodeToBigNumericByteString(
new BigDecimal(((Number) val).doubleValue())));
new BigDecimal(String.valueOf(val))));
return;
}
}
Expand Down Expand Up @@ -547,7 +547,7 @@ private static void fillRepeatedField(
protoMsg.addRepeatedField(
fieldDescriptor,
BigDecimalByteStringEncoder.encodeToNumericByteString(
new BigDecimal(((Number) val).doubleValue())));
new BigDecimal(String.valueOf(val))));
added = true;
}
} else if (fieldSchema != null
Expand All @@ -568,7 +568,7 @@ private static void fillRepeatedField(
protoMsg.addRepeatedField(
fieldDescriptor,
BigDecimalByteStringEncoder.encodeToBigNumericByteString(
new BigDecimal(((Number) val).doubleValue())));
new BigDecimal(String.valueOf(val))));
added = true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.google.protobuf.Message;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Map;
import java.util.logging.Logger;
import org.json.JSONArray;
Expand Down Expand Up @@ -1311,4 +1312,58 @@ public void testNullRepeatedField() throws Exception {
JsonToProtoMessage.convertJsonToProtoMessage(RepeatedInt32.getDescriptor(), ts, json);
assertTrue(protoMsg.getAllFields().isEmpty());
}

@Test
public void testDoubleAndFloatToNumericConversion() {
TableSchema ts =
TableSchema.newBuilder()
.addFields(
0,
TableFieldSchema.newBuilder()
.setName("numeric")
.setType(TableFieldSchema.Type.NUMERIC)
.build())
.build();
TestNumeric expectedProto =
TestNumeric.newBuilder()
.setNumeric(
BigDecimalByteStringEncoder.encodeToNumericByteString(new BigDecimal("24.678")))
.build();
JSONObject json = new JSONObject();
json.put("numeric", new Double(24.678));
DynamicMessage protoMsg =
JsonToProtoMessage.convertJsonToProtoMessage(TestNumeric.getDescriptor(), ts, json);
assertEquals(expectedProto, protoMsg);
json.put("numeric", new Float(24.678));
protoMsg = JsonToProtoMessage.convertJsonToProtoMessage(TestNumeric.getDescriptor(), ts, json);
assertEquals(expectedProto, protoMsg);
}

@Test
public void testDoubleAndFloatToRepeatedBigNumericConversion() {
TableSchema ts =
TableSchema.newBuilder()
.addFields(
0,
TableFieldSchema.newBuilder()
.setName("bignumeric")
.setType(TableFieldSchema.Type.BIGNUMERIC)
.setMode(TableFieldSchema.Mode.REPEATED)
.build())
.build();
TestBignumeric expectedProto =
TestBignumeric.newBuilder()
.addBignumeric(
BigDecimalByteStringEncoder.encodeToBigNumericByteString(new BigDecimal("24.678")))
.build();
JSONObject json = new JSONObject();
json.put("bignumeric", Collections.singletonList(new Double(24.678)));
DynamicMessage protoMsg =
JsonToProtoMessage.convertJsonToProtoMessage(TestBignumeric.getDescriptor(), ts, json);
assertEquals(expectedProto, protoMsg);
json.put("bignumeric", Collections.singletonList(new Float(24.678)));
protoMsg =
JsonToProtoMessage.convertJsonToProtoMessage(TestBignumeric.getDescriptor(), ts, json);
assertEquals(expectedProto, protoMsg);
}
}

0 comments on commit 5cc96a0

Please sign in to comment.