From 79bd10f430a8edf26c0a38c556f5a1d370efc079 Mon Sep 17 00:00:00 2001 From: ramya vasanth Date: Thu, 19 Nov 2020 14:11:38 +0530 Subject: [PATCH] [OLINGO-1480]Client error when reading Decimal literal --- .../core/domain/ClientPrimitiveValueImpl.java | 8 +++++++- .../client/core/serialization/JsonDeserializer.java | 13 +++++++++---- .../client/core/serialization/JsonSerializer.java | 10 +++++++++- .../commons/core/edm/primitivetype/EdmDecimal.java | 5 +++-- 4 files changed, 28 insertions(+), 8 deletions(-) diff --git a/lib/client-core/src/main/java/org/apache/olingo/client/core/domain/ClientPrimitiveValueImpl.java b/lib/client-core/src/main/java/org/apache/olingo/client/core/domain/ClientPrimitiveValueImpl.java index 4824632bd..d42126863 100644 --- a/lib/client-core/src/main/java/org/apache/olingo/client/core/domain/ClientPrimitiveValueImpl.java +++ b/lib/client-core/src/main/java/org/apache/olingo/client/core/domain/ClientPrimitiveValueImpl.java @@ -207,7 +207,13 @@ public class ClientPrimitiveValueImpl extends AbstractClientValue implements Cli } else { try { // TODO: set facets - return type.valueToString(value, null, null, Constants.DEFAULT_PRECISION, Constants.DEFAULT_SCALE, null); + 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(); + } + return type.valueToString(value, null, null, precision, scale, null); } catch (EdmPrimitiveTypeException e) { throw new IllegalArgumentException(e); } diff --git a/lib/client-core/src/main/java/org/apache/olingo/client/core/serialization/JsonDeserializer.java b/lib/client-core/src/main/java/org/apache/olingo/client/core/serialization/JsonDeserializer.java index efd2963f6..ddde0afa5 100644 --- a/lib/client-core/src/main/java/org/apache/olingo/client/core/serialization/JsonDeserializer.java +++ b/lib/client-core/src/main/java/org/apache/olingo/client/core/serialization/JsonDeserializer.java @@ -289,10 +289,15 @@ public class JsonDeserializer implements ODataDeserializer { : typeInfo == null ? node.asText() : typeInfo.getPrimitiveTypeKind().isGeospatial() ? getGeoDeserializer().deserialize(node, typeInfo) - : ((EdmPrimitiveType) typeInfo.getType()) - .valueOfString(node.asText(), true, null, - Constants.DEFAULT_PRECISION, Constants.DEFAULT_SCALE, true, - ((EdmPrimitiveType) typeInfo.getType()).getDefaultType()); + : node.isBigDecimal() + ?((EdmPrimitiveType) typeInfo.getType()) + .valueOfString(node.asText(), true, null, + node.decimalValue().precision(), node.decimalValue().scale(), true, + ((EdmPrimitiveType) typeInfo.getType()).getDefaultType()) + :((EdmPrimitiveType) typeInfo.getType()) + .valueOfString(node.asText(), true, null, + Constants.DEFAULT_PRECISION, Constants.DEFAULT_SCALE, true, + ((EdmPrimitiveType) typeInfo.getType()).getDefaultType()); } private Object fromComplex(final ObjectNode node, final ObjectCodec codec) diff --git a/lib/client-core/src/main/java/org/apache/olingo/client/core/serialization/JsonSerializer.java b/lib/client-core/src/main/java/org/apache/olingo/client/core/serialization/JsonSerializer.java index bd983a597..f13e87284 100644 --- a/lib/client-core/src/main/java/org/apache/olingo/client/core/serialization/JsonSerializer.java +++ b/lib/client-core/src/main/java/org/apache/olingo/client/core/serialization/JsonSerializer.java @@ -20,6 +20,7 @@ package org.apache.olingo.client.core.serialization; import java.io.IOException; import java.io.Writer; +import java.math.BigDecimal; import java.net.URI; import java.util.ArrayList; import java.util.Arrays; @@ -296,8 +297,15 @@ public class JsonSerializer implements ODataSerializer { } } else { // TODO: add facets + Integer precesion = Constants.DEFAULT_PRECISION; + Integer scale = Constants.DEFAULT_SCALE; + if(kind == EdmPrimitiveTypeKind.Decimal && value instanceof BigDecimal){ + BigDecimal bigDecimal = (BigDecimal)value; + precesion = bigDecimal.precision(); + scale = bigDecimal.scale(); + } final String serialized = EdmPrimitiveTypeFactory.getInstance(kind) - .valueToString(value, null, null, Constants.DEFAULT_PRECISION, Constants.DEFAULT_SCALE, null); + .valueToString(value, null, null, precesion, scale, null); if (isIEEE754Compatible && (kind == EdmPrimitiveTypeKind.Int64 || kind == EdmPrimitiveTypeKind.Decimal) || !NUMBER_TYPES.contains(kind)) { diff --git a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/primitivetype/EdmDecimal.java b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/primitivetype/EdmDecimal.java index e177d7d7f..86033b8de 100644 --- a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/primitivetype/EdmDecimal.java +++ b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/primitivetype/EdmDecimal.java @@ -85,9 +85,10 @@ public final class EdmDecimal extends SingletonPrimitiveType { final int decimals = matcher.group(2) == null ? 0 : matcher.group(2).length(); return (precision == null || (significantIntegerDigits >= 0 && significantIntegerDigits <= precision - ((scale == null) ? 0 : scale))) && - (decimals >= 0 && decimals <= ((scale == null) ? 0 : scale)); + (( decimals == 0 && ((scale == null) ? 0 : scale) < 0) || + (decimals >= 0 && decimals <= ((scale == null) ? 0 : scale))); } - + @Override public boolean validateDecimals(final String value, final Boolean isNullable, final Integer maxLength, final Integer precision,