[OLINGO-1568] Add handling of special case precision/scale

This commit is contained in:
mibo 2023-09-24 07:14:21 +02:00 committed by Michael Bolz
parent 5d1fae138a
commit 6a800beee3
4 changed files with 54 additions and 5 deletions

View File

@ -206,12 +206,16 @@ public class ClientPrimitiveValueImpl extends AbstractClientValue implements Cli
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) {

View File

@ -296,16 +296,20 @@ public class JsonSerializer implements ODataSerializer {
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)) {

View File

@ -20,6 +20,7 @@ package org.apache.olingo.client.core;
import static org.junit.Assert.assertEquals;
import java.math.BigDecimal;
import java.util.Calendar;
import org.apache.olingo.client.api.domain.ClientValue;
@ -64,4 +65,16 @@ public class PrimitiveValueTest extends AbstractTest {
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());
}
}

View File

@ -19,6 +19,7 @@ import static org.hamcrest.CoreMatchers.is;
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;
@ -66,6 +67,33 @@ public class JsonSerializerTest {
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\","
+ "\"testLeadingZerosDecimal@odata.type\":\"Decimal\","
+ "\"testLeadingZerosDecimal\":0.01,"
+ "\"testArbitraryPrecisionDecimal@odata.type\":\"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));