Skip to content

Commit

Permalink
[OLINGO-1568] Add handling of special case precision/scale
Browse files Browse the repository at this point in the history
  • Loading branch information
mibo committed Sep 24, 2023
1 parent 5d1fae1 commit 6a800be
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -206,12 +206,16 @@ public String toString() {
return value.toString();
} else {
try {
// TODO: set facets
Integer precision = Constants.DEFAULT_PRECISION;
Integer scale = Constants.DEFAULT_SCALE;
if (typeKind.equals(EdmPrimitiveTypeKind.Decimal) && value instanceof BigDecimal) {
precision = ((BigDecimal) value).precision();
scale = ((BigDecimal) value).scale();
if (precision == 0) {
precision = null;
} else if (scale > precision) {
precision = scale;
}
}
return type.valueToString(value, null, null, precision, scale, null);
} catch (EdmPrimitiveTypeException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,16 +296,20 @@ protected void primitiveValue(final JsonGenerator jgen, final EdmTypeInfo typeIn
jgen.writeString(value.toString()); // This might not be valid OData.
}
} else {
// TODO: add facets
Integer precesion = Constants.DEFAULT_PRECISION;
Integer precision = Constants.DEFAULT_PRECISION;
Integer scale = Constants.DEFAULT_SCALE;
if(kind == EdmPrimitiveTypeKind.Decimal && value instanceof BigDecimal){
BigDecimal bigDecimal = (BigDecimal)value;
precesion = bigDecimal.precision();
precision = bigDecimal.precision();
scale = bigDecimal.scale();
if (precision == 0) {
precision = null;
} else if (scale > precision) {
precision = scale;
}
}
final String serialized = EdmPrimitiveTypeFactory.getInstance(kind)
.valueToString(value, null, null, precesion, scale, null);
.valueToString(value, null, null, precision, scale, null);

if (isIEEE754Compatible && (kind == EdmPrimitiveTypeKind.Int64 || kind == EdmPrimitiveTypeKind.Decimal)
|| !NUMBER_TYPES.contains(kind)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import static org.junit.Assert.assertEquals;

import java.math.BigDecimal;
import java.util.Calendar;

import org.apache.olingo.client.api.domain.ClientValue;
Expand Down Expand Up @@ -64,4 +65,16 @@ public void Date() throws EdmPrimitiveTypeException {

assertEquals("2013-01-10", value.asPrimitive().toString());
}

@Test
public void testBigDecimalToStringConversion() throws EdmPrimitiveTypeException {
final ClientValue leadingZerosDecimalValue = client.getObjectFactory().newPrimitiveValueBuilder()
.setType(EdmPrimitiveTypeKind.Decimal).setValue(new BigDecimal("0.01")).build();
final ClientValue arbitraryPrecisionDecimalValue = client.getObjectFactory().newPrimitiveValueBuilder()
.setType(EdmPrimitiveTypeKind.Decimal).setValue(new BigDecimal(0.01)).build();

assertEquals("0.01", leadingZerosDecimalValue.asPrimitive().toString());
assertEquals("0.01000000000000000020816681711721685132943093776702880859375",
arbitraryPrecisionDecimalValue.asPrimitive().toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static org.junit.Assert.assertThat;

import java.io.StringWriter;
import java.math.BigDecimal;

import org.apache.olingo.client.api.ODataClient;
import org.apache.olingo.client.api.domain.ClientEntity;
Expand Down Expand Up @@ -66,6 +67,33 @@ public void testClientEntityJSONWithNull() throws ODataSerializerException {

JsonSerializer jsonSerializer = new JsonSerializer(false, ContentType.JSON_FULL_METADATA);

StringWriter writer = new StringWriter();
jsonSerializer.write(writer, odataClient.getBinder().getEntity(clientEntity));
assertThat(writer.toString(), is(expectedJson));
}
@Test
public void testClientEntityJSONWithBigDecimal() throws ODataSerializerException {
String expectedJson = "{\"@odata.type\":\"#test.testClientEntity\","
+ "\"[email protected]\":\"Decimal\","
+ "\"testLeadingZerosDecimal\":0.01,"
+ "\"[email protected]\":\"Decimal\","
+ "\"testArbitraryPrecisionDecimal\":0.01000000000000000020816681711721685132943093776702880859375}";

ODataClient odataClient = ODataClientFactory.getClient();
ClientObjectFactory objFactory = odataClient.getObjectFactory();
ClientEntity clientEntity = objFactory.newEntity(new FullQualifiedName("test", "testClientEntity"));

clientEntity.getProperties().add(
objFactory.newPrimitiveProperty(
"testLeadingZerosDecimal",
objFactory.newPrimitiveValueBuilder().buildDecimal(new BigDecimal("0.01"))));
clientEntity.getProperties().add(
objFactory.newPrimitiveProperty(
"testArbitraryPrecisionDecimal",
objFactory.newPrimitiveValueBuilder().buildDecimal(new BigDecimal(0.01))));

JsonSerializer jsonSerializer = new JsonSerializer(false, ContentType.JSON_FULL_METADATA);

StringWriter writer = new StringWriter();
jsonSerializer.write(writer, odataClient.getBinder().getEntity(clientEntity));
assertThat(writer.toString(), is(expectedJson));
Expand Down

0 comments on commit 6a800be

Please sign in to comment.