Skip to content

Commit 065b18c

Browse files
author
Nicolás Míguez García
committed
[OLINGO-1501] Fix error when OData client receives EdmDateTime or EdmDateTimeOffset values with precision greater than milliseconds
1 parent c31a2ca commit 065b18c

File tree

6 files changed

+34
-49
lines changed

6 files changed

+34
-49
lines changed

odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/edm/EdmDateTime.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public static EdmDateTime getInstance() {
4747

4848
@Override
4949
public Class<?> getDefaultType() {
50-
return Calendar.class;
50+
return Timestamp.class;
5151
}
5252

5353
@Override
@@ -73,6 +73,8 @@ protected <T> T internalValueOfString(final String value, final EdmLiteralKind l
7373
dateTimeValue.clear();
7474
dateTimeValue.setTimeInMillis(millis);
7575
return returnType.cast(dateTimeValue);
76+
} else if (returnType.isAssignableFrom(Timestamp.class)) {
77+
return returnType.cast(new Timestamp(millis));
7678
} else {
7779
throw new EdmSimpleTypeException(EdmSimpleTypeException.VALUE_TYPE_NOT_SUPPORTED.addContent(returnType));
7880
}
@@ -170,11 +172,7 @@ protected <T> String internalValueToString(final T value, final EdmLiteralKind l
170172
}
171173

172174
if (literalKind == EdmLiteralKind.JSON) {
173-
if (value instanceof Timestamp && ((Timestamp) value).getNanos() % (1000 * 1000) != 0) {
174-
throw new EdmSimpleTypeException(EdmSimpleTypeException.VALUE_ILLEGAL_CONTENT.addContent(value));
175-
} else {
176-
return "/Date(" + timeInMillis + ")/";
177-
}
175+
return "/Date(" + timeInMillis + ")/";
178176
}
179177

180178
Calendar dateTimeValue = Calendar.getInstance(TimeZone.getTimeZone("GMT"));

odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/edm/EdmDateTimeOffset.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public static EdmDateTimeOffset getInstance() {
5252

5353
@Override
5454
public Class<?> getDefaultType() {
55-
return Calendar.class;
55+
return Timestamp.class;
5656
}
5757

5858
@Override

odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/edm/EdmSimpleTypeTest.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -436,8 +436,8 @@ public void defaultType() throws Exception {
436436
assertEquals(byte[].class, EdmSimpleTypeKind.Binary.getEdmSimpleTypeInstance().getDefaultType());
437437
assertEquals(Boolean.class, EdmSimpleTypeKind.Boolean.getEdmSimpleTypeInstance().getDefaultType());
438438
assertEquals(Short.class, EdmSimpleTypeKind.Byte.getEdmSimpleTypeInstance().getDefaultType());
439-
assertEquals(Calendar.class, EdmSimpleTypeKind.DateTime.getEdmSimpleTypeInstance().getDefaultType());
440-
assertEquals(Calendar.class, EdmSimpleTypeKind.DateTimeOffset.getEdmSimpleTypeInstance().getDefaultType());
439+
assertEquals(Timestamp.class, EdmSimpleTypeKind.DateTime.getEdmSimpleTypeInstance().getDefaultType());
440+
assertEquals(Timestamp.class, EdmSimpleTypeKind.DateTimeOffset.getEdmSimpleTypeInstance().getDefaultType());
441441
assertEquals(BigDecimal.class, EdmSimpleTypeKind.Decimal.getEdmSimpleTypeInstance().getDefaultType());
442442
assertEquals(Double.class, EdmSimpleTypeKind.Double.getEdmSimpleTypeInstance().getDefaultType());
443443
assertEquals(UUID.class, EdmSimpleTypeKind.Guid.getEdmSimpleTypeInstance().getDefaultType());
@@ -596,8 +596,7 @@ public void valueToStringDateTime() throws Exception {
596596
getPrecisionScaleFacets(9, null)));
597597
expectErrorInValueToString(instance, timestamp, EdmLiteralKind.DEFAULT, getPrecisionScaleFacets(8, null),
598598
EdmSimpleTypeException.VALUE_FACETS_NOT_MATCHED);
599-
expectErrorInValueToString(instance, timestamp, EdmLiteralKind.JSON, null,
600-
EdmSimpleTypeException.VALUE_ILLEGAL_CONTENT);
599+
assertEquals("/Date(0)/", instance.valueToString(timestamp, EdmLiteralKind.JSON, null));
601600

602601
dateTime.add(Calendar.MILLISECOND, -100);
603602
expectErrorInValueToString(instance, dateTime, EdmLiteralKind.DEFAULT, getPrecisionScaleFacets(0, null),
@@ -1190,8 +1189,6 @@ public void valueOfStringDateTime() throws Exception {
11901189
getPrecisionScaleFacets(0, null), EdmSimpleTypeException.LITERAL_FACETS_NOT_MATCHED);
11911190
expectErrorInValueOfString(instance, "2012-02-29T23:32:02.98700", EdmLiteralKind.DEFAULT,
11921191
getPrecisionScaleFacets(2, null), EdmSimpleTypeException.LITERAL_FACETS_NOT_MATCHED);
1193-
expectErrorInValueOfString(instance, "2012-02-29T23:32:02.9876", EdmLiteralKind.DEFAULT, null,
1194-
EdmSimpleTypeException.LITERAL_ILLEGAL_CONTENT);
11951192
expectErrorInValueOfString(instance, "2012-02-29T23:32:02.", EdmLiteralKind.DEFAULT, null,
11961193
EdmSimpleTypeException.LITERAL_ILLEGAL_CONTENT);
11971194
expectErrorInValueOfString(instance, "2012-02-29T23:32:02.0000000000", EdmLiteralKind.DEFAULT, null,
@@ -1293,8 +1290,6 @@ public void valueOfStringDateTimeOffset() throws Exception {
12931290

12941291
expectErrorInValueOfString(instance, "2012-02-29T23:32:02.9Z", EdmLiteralKind.DEFAULT,
12951292
getPrecisionScaleFacets(0, null), EdmSimpleTypeException.LITERAL_FACETS_NOT_MATCHED);
1296-
expectErrorInValueOfString(instance, "2012-02-29T23:32:02.9876Z", EdmLiteralKind.DEFAULT, null,
1297-
EdmSimpleTypeException.LITERAL_ILLEGAL_CONTENT);
12981293
expectErrorInValueOfString(instance, "datetime'2012-02-29T23:32:02'", EdmLiteralKind.URI, null,
12991294
EdmSimpleTypeException.LITERAL_ILLEGAL_CONTENT);
13001295
expectErrorInValueOfString(instance, "2012-02-29T23:32:02X", EdmLiteralKind.DEFAULT, null,

odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/ep/consumer/JsonPropertyConsumerTest.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.io.InputStreamReader;
3131
import java.io.UnsupportedEncodingException;
3232
import java.math.BigDecimal;
33+
import java.sql.Timestamp;
3334
import java.util.Arrays;
3435
import java.util.Calendar;
3536
import java.util.Collections;
@@ -126,15 +127,15 @@ public void allStringSimplePropertyKinds() throws Exception {
126127
JsonReader reader = prepareReader(simplePropertyJson);
127128
when(edmProperty.getType()).thenReturn(EdmSimpleTypeKind.DateTime.getEdmSimpleTypeInstance());
128129
Map<String, Object> resultMap = execute(edmProperty, reader);
129-
Calendar entryDate = (Calendar) resultMap.get("Name");
130-
assertEquals(Long.valueOf(915148800000l), Long.valueOf(entryDate.getTimeInMillis()));
130+
Timestamp entryDate = (Timestamp) resultMap.get("Name");
131+
assertEquals(Long.valueOf(915148800000l), Long.valueOf(entryDate.getTime()));
131132
// DateTimeOffset
132133
simplePropertyJson = "{\"d\":{\"Name\":\"\\/Date(915148800000)\\/\"}}";
133134
reader = prepareReader(simplePropertyJson);
134135
when(edmProperty.getType()).thenReturn(EdmSimpleTypeKind.DateTime.getEdmSimpleTypeInstance());
135136
resultMap = execute(edmProperty, reader);
136-
entryDate = (Calendar) resultMap.get("Name");
137-
assertEquals(Long.valueOf(915148800000l), Long.valueOf(entryDate.getTimeInMillis()));
137+
entryDate = (Timestamp) resultMap.get("Name");
138+
assertEquals(Long.valueOf(915148800000l), Long.valueOf(entryDate.getTime()));
138139
// Decimal
139140
simplePropertyJson = "{\"d\":{\"Name\":\"123456789\"}}";
140141
reader = prepareReader(simplePropertyJson);

odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/ep/consumer/XmlEntityConsumerTest.java

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,11 @@
2525
import static org.junit.Assert.assertTrue;
2626

2727
import java.io.InputStream;
28-
import java.util.Calendar;
28+
import java.sql.Timestamp;
2929
import java.util.Date;
3030
import java.util.HashMap;
3131
import java.util.List;
3232
import java.util.Map;
33-
import java.util.TimeZone;
3433
import java.util.Map.Entry;
3534
import java.util.logging.Level;
3635
import java.util.logging.Logger;
@@ -1855,9 +1854,8 @@ public void testReadFeed() throws Exception {
18551854
assertEquals("69124", city.get("PostalCode"));
18561855
assertEquals("Heidelberg", city.get("CityName"));
18571856
assertEquals(Integer.valueOf(52), properties.get("Age"));
1858-
Calendar entryDate = (Calendar) properties.get("EntryDate");
1859-
assertEquals(915148800000L, entryDate.getTimeInMillis());
1860-
assertEquals(TimeZone.getTimeZone("GMT"), entryDate.getTimeZone());
1857+
Timestamp entryDate = (Timestamp) properties.get("EntryDate");
1858+
assertEquals(915148800000L, entryDate.getTime());
18611859
assertEquals("Employees('1')/$value", properties.get("ImageUrl"));
18621860
}
18631861

@@ -1901,9 +1899,8 @@ public void testReadFeedWithInlineCountAndNextLink() throws Exception {
19011899
assertEquals("69124", city.get("PostalCode"));
19021900
assertEquals("Heidelberg", city.get("CityName"));
19031901
assertEquals(Integer.valueOf(52), properties.get("Age"));
1904-
Calendar entryDate = (Calendar) properties.get("EntryDate");
1905-
assertEquals(915148800000L, entryDate.getTimeInMillis());
1906-
assertEquals(TimeZone.getTimeZone("GMT"), entryDate.getTimeZone());
1902+
Timestamp entryDate = (Timestamp) properties.get("EntryDate");
1903+
assertEquals(915148800000L, entryDate.getTime());
19071904
assertEquals("Employees('1')/$value", properties.get("ImageUrl"));
19081905
}
19091906

@@ -1936,9 +1933,8 @@ public void testReadEntry() throws Exception {
19361933
assertEquals("69124", city.get("PostalCode"));
19371934
assertEquals("Heidelberg", city.get("CityName"));
19381935
assertEquals(Integer.valueOf(52), properties.get("Age"));
1939-
Calendar entryDate = (Calendar) properties.get("EntryDate");
1940-
assertEquals(915148800000L, entryDate.getTimeInMillis());
1941-
assertEquals(TimeZone.getTimeZone("GMT"), entryDate.getTimeZone());
1936+
Timestamp entryDate = (Timestamp) properties.get("EntryDate");
1937+
assertEquals(915148800000L, entryDate.getTime());
19421938
assertEquals("/SAP/PUBLIC/BC/NWDEMO_MODEL/IMAGES/Employee_1.png", properties.get("ImageUrl"));
19431939
}
19441940

@@ -1972,9 +1968,8 @@ public void testReadEntryWithLargeProperty() throws Exception {
19721968
assertEquals("69124", city.get("PostalCode"));
19731969
assertEquals("Heidelberg", city.get("CityName"));
19741970
assertEquals(Integer.valueOf(52), properties.get("Age"));
1975-
Calendar entryDate = (Calendar) properties.get("EntryDate");
1976-
assertEquals(915148800000L, entryDate.getTimeInMillis());
1977-
assertEquals(TimeZone.getTimeZone("GMT"), entryDate.getTimeZone());
1971+
Timestamp entryDate = (Timestamp) properties.get("EntryDate");
1972+
assertEquals(915148800000L, entryDate.getTime());
19781973
assertEquals("/SAP/PUBLIC/BC/NWDEMO_MODEL/IMAGES/Employee_1.png", properties.get("ImageUrl"));
19791974
}
19801975

@@ -2011,9 +2006,8 @@ public void testReadEntryMissingKeyProperty() throws Exception {
20112006
assertEquals("69124", city.get("PostalCode"));
20122007
assertEquals("Heidelberg", city.get("CityName"));
20132008
assertEquals(Integer.valueOf(52), properties.get("Age"));
2014-
Calendar entryDate = (Calendar) properties.get("EntryDate");
2015-
assertEquals(915148800000L, entryDate.getTimeInMillis());
2016-
assertEquals(TimeZone.getTimeZone("GMT"), entryDate.getTimeZone());
2009+
Timestamp entryDate = (Timestamp) properties.get("EntryDate");
2010+
assertEquals(915148800000L, entryDate.getTime());
20172011
assertEquals("/SAP/PUBLIC/BC/NWDEMO_MODEL/IMAGES/Employee_1.png", properties.get("ImageUrl"));
20182012
}
20192013

@@ -2105,9 +2099,8 @@ public void testReadEntryWithMerge() throws Exception {
21052099
assertEquals(2, city.size());
21062100
assertEquals("69124", city.get("PostalCode"));
21072101
assertEquals("Heidelberg", city.get("CityName"));
2108-
Calendar entryDate = (Calendar) properties.get("EntryDate");
2109-
assertEquals(915148800000L, entryDate.getTimeInMillis());
2110-
assertEquals(TimeZone.getTimeZone("GMT"), entryDate.getTimeZone());
2102+
Timestamp entryDate = (Timestamp) properties.get("EntryDate");
2103+
assertEquals(915148800000L, entryDate.getTime());
21112104
assertEquals("/SAP/PUBLIC/BC/NWDEMO_MODEL/IMAGES/Employee_1.png", properties.get("ImageUrl"));
21122105
}
21132106

@@ -2178,9 +2171,8 @@ public void testReadEntryRequest() throws Exception {
21782171
assertEquals("69124", city.get("PostalCode"));
21792172
assertEquals("Heidelberg", city.get("CityName"));
21802173
assertEquals(Integer.valueOf(52), properties.get("Age"));
2181-
Calendar entryDate = (Calendar) properties.get("EntryDate");
2182-
assertEquals(915148800000L, entryDate.getTimeInMillis());
2183-
assertEquals(TimeZone.getTimeZone("GMT"), entryDate.getTimeZone());
2174+
Timestamp entryDate = (Timestamp) properties.get("EntryDate");
2175+
assertEquals(915148800000L, entryDate.getTime());
21842176
assertEquals("/SAP/PUBLIC/BC/NWDEMO_MODEL/IMAGES/Employee_1.png", properties.get("ImageUrl"));
21852177
}
21862178

@@ -2211,9 +2203,8 @@ public void testReadEntryRequestNullMapping() throws Exception {
22112203
assertEquals("69124", city.get("PostalCode"));
22122204
assertEquals("Heidelberg", city.get("CityName"));
22132205
assertEquals(Integer.valueOf(52), properties.get("Age"));
2214-
Calendar entryDate = (Calendar) properties.get("EntryDate");
2215-
assertEquals(915148800000L, entryDate.getTimeInMillis());
2216-
assertEquals(TimeZone.getTimeZone("GMT"), entryDate.getTimeZone());
2206+
Timestamp entryDate = (Timestamp) properties.get("EntryDate");
2207+
assertEquals(915148800000L, entryDate.getTime());
22172208
assertEquals("/SAP/PUBLIC/BC/NWDEMO_MODEL/IMAGES/Employee_1.png", properties.get("ImageUrl"));
22182209
}
22192210

@@ -2243,9 +2234,8 @@ public void testReadEntryRequestEmptyMapping() throws Exception {
22432234
assertEquals("69124", city.get("PostalCode"));
22442235
assertEquals("Heidelberg", city.get("CityName"));
22452236
assertEquals(Integer.valueOf(52), properties.get("Age"));
2246-
Calendar entryDate = (Calendar) properties.get("EntryDate");
2247-
assertEquals(915148800000L, entryDate.getTimeInMillis());
2248-
assertEquals(TimeZone.getTimeZone("GMT"), entryDate.getTimeZone());
2237+
Timestamp entryDate = (Timestamp) properties.get("EntryDate");
2238+
assertEquals(915148800000L, entryDate.getTime());
22492239
assertEquals("/SAP/PUBLIC/BC/NWDEMO_MODEL/IMAGES/Employee_1.png", properties.get("ImageUrl"));
22502240
}
22512241

odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/ep/consumer/XmlPropertyConsumerTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import static org.mockito.Mockito.when;
2828

2929
import java.math.BigDecimal;
30+
import java.sql.Timestamp;
3031
import java.util.Arrays;
3132
import java.util.Calendar;
3233
import java.util.Collections;
@@ -172,8 +173,8 @@ public void readStringPropertyNullFalse() throws Exception {
172173

173174
final Map<String, Object> resultMap = new XmlPropertyConsumer().readProperty(reader, property, null);
174175

175-
assertEquals(86400000L, ((Calendar) resultMap.get("EntryDate")).getTimeInMillis());
176176
}
177+
assertEquals(86400000L, ((Timestamp) resultMap.get("EntryDate")).getTime());
177178
178179
@Test(expected = EntityProviderException.class)
179180
public void invalidSimplePropertyName() throws Exception {

0 commit comments

Comments
 (0)