Skip to content

Commit 1fe6d72

Browse files
authored
feat: validate OffsetDateTime to satisfy RFC 3339 (#140)
1 parent 9c8af0c commit 1fe6d72

File tree

4 files changed

+133
-0
lines changed

4 files changed

+133
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
### Features
44
1. [#139](https://github.com/influxdata/influxdb-client-java/pull/139): Marked Apis as @ThreadSafe
5+
1. [#140](https://github.com/influxdata/influxdb-client-java/pull/140): Validate OffsetDateTime to satisfy RFC 3339
56

67
### Bug Fixes
78
1. [#136](https://github.com/influxdata/influxdb-client-java/pull/136): Data Point: measurement name is requiring in constructor

client/src/generated/java/com/influxdb/client/JSON.java

+12
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import com.google.gson.Gson;
1717
import com.google.gson.GsonBuilder;
18+
import com.google.gson.JsonIOException;
1819
import com.google.gson.JsonParseException;
1920
import com.google.gson.JsonSerializer;
2021
import com.google.gson.TypeAdapter;
@@ -34,7 +35,9 @@
3435
import java.text.ParseException;
3536
import java.text.ParsePosition;
3637
import java.time.LocalDate;
38+
import java.time.LocalDateTime;
3739
import java.time.OffsetDateTime;
40+
import java.time.ZoneOffset;
3841
import java.time.format.DateTimeFormatter;
3942
import java.util.Date;
4043
import java.util.Locale;
@@ -163,6 +166,9 @@ public JSON setGson(Gson gson) {
163166
*/
164167
public static class OffsetDateTimeTypeAdapter extends TypeAdapter<OffsetDateTime> {
165168

169+
private static final OffsetDateTime ZERO = LocalDateTime.of(0, 1, 1, 0, 0)
170+
.atOffset(ZoneOffset.UTC);
171+
166172
private DateTimeFormatter formatter;
167173

168174
public OffsetDateTimeTypeAdapter() {
@@ -182,6 +188,12 @@ public void write(JsonWriter out, OffsetDateTime date) throws IOException {
182188
if (date == null) {
183189
out.nullValue();
184190
} else {
191+
if (date.getYear() > 9999 || date.isBefore(ZERO)) {
192+
// https://tools.ietf.org/html/rfc3339
193+
throw new JsonIOException("OffsetDateTime is out of range. All dates and times are assumed to be "
194+
+ "in the \"current era\", somewhere between 0000AD and 9999AD.");
195+
}
196+
185197
out.value(formatter.format(date));
186198
}
187199
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
* The MIT License
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
* THE SOFTWARE.
21+
*/
22+
package com.influxdb.client;
23+
24+
import java.io.IOException;
25+
import java.io.StringWriter;
26+
import java.time.LocalDateTime;
27+
import java.time.OffsetDateTime;
28+
import java.time.ZoneOffset;
29+
30+
import com.google.gson.JsonIOException;
31+
import com.google.gson.stream.JsonWriter;
32+
import org.assertj.core.api.Assertions;
33+
import org.junit.jupiter.api.BeforeEach;
34+
import org.junit.jupiter.api.Test;
35+
36+
/**
37+
* @author Jakub Bednar (25/06/2020 13:47)
38+
*/
39+
public class OffsetDateTimeTypeAdapterTest {
40+
41+
private JSON.OffsetDateTimeTypeAdapter adapter;
42+
private StringWriter writer;
43+
44+
@BeforeEach
45+
void beforeEach() {
46+
adapter = new JSON.OffsetDateTimeTypeAdapter();
47+
writer = new StringWriter();
48+
}
49+
50+
@Test
51+
public void max() throws IOException {
52+
OffsetDateTime time = LocalDateTime.of(9999, 12, 31, 23, 59)
53+
.atOffset(ZoneOffset.UTC);
54+
adapter.write(new JsonWriter(writer), time);
55+
56+
Assertions.assertThat(writer.toString()).isEqualTo("\"9999-12-31T23:59:00Z\"");
57+
}
58+
59+
@Test
60+
public void max_over() {
61+
62+
OffsetDateTime time = LocalDateTime.of(10_000, 12, 31, 23, 59)
63+
.atOffset(ZoneOffset.UTC);
64+
65+
Assertions.assertThatThrownBy(() -> adapter.write(new JsonWriter(writer), time))
66+
.isInstanceOf(JsonIOException.class)
67+
.hasMessage("OffsetDateTime is out of range. All dates and times are assumed to be in the "
68+
+ "\"current era\", somewhere between 0000AD and 9999AD.");
69+
}
70+
71+
@Test
72+
public void max_offset() {
73+
74+
Assertions.assertThatThrownBy(() -> adapter.write(new JsonWriter(writer), OffsetDateTime.MAX))
75+
.isInstanceOf(JsonIOException.class)
76+
.hasMessage("OffsetDateTime is out of range. All dates and times are assumed to be in the "
77+
+ "\"current era\", somewhere between 0000AD and 9999AD.");
78+
}
79+
80+
@Test
81+
public void min() throws IOException {
82+
OffsetDateTime time = LocalDateTime.of(0, 1, 1, 0, 0)
83+
.atOffset(ZoneOffset.UTC);
84+
adapter.write(new JsonWriter(writer), time);
85+
86+
Assertions.assertThat(writer.toString()).isEqualTo("\"0000-01-01T00:00:00Z\"");
87+
}
88+
89+
@Test
90+
public void min_over() {
91+
OffsetDateTime time = LocalDateTime.of(-1, 1, 1, 0, 0)
92+
.atOffset(ZoneOffset.UTC);
93+
Assertions.assertThatThrownBy(() -> adapter.write(new JsonWriter(writer), time))
94+
.isInstanceOf(JsonIOException.class)
95+
.hasMessage("OffsetDateTime is out of range. All dates and times are assumed to be in the "
96+
+ "\"current era\", somewhere between 0000AD and 9999AD.");
97+
98+
}
99+
100+
@Test
101+
public void min_offset() {
102+
Assertions.assertThatThrownBy(() -> adapter.write(new JsonWriter(writer), OffsetDateTime.MIN))
103+
.isInstanceOf(JsonIOException.class)
104+
.hasMessage("OffsetDateTime is out of range. All dates and times are assumed to be in the "
105+
+ "\"current era\", somewhere between 0000AD and 9999AD.");
106+
107+
}
108+
}

openapi-generator/src/main/resources/Java/libraries/retrofit2/JSON.mustache

+12
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package {{invokerPackage}};
44

55
import com.google.gson.Gson;
66
import com.google.gson.GsonBuilder;
7+
import com.google.gson.JsonIOException;
78
import com.google.gson.JsonParseException;
89
import com.google.gson.JsonSerializer;
910
import com.google.gson.TypeAdapter;
@@ -38,7 +39,9 @@ import java.text.ParseException;
3839
import java.text.ParsePosition;
3940
{{#java8}}
4041
import java.time.LocalDate;
42+
import java.time.LocalDateTime;
4143
import java.time.OffsetDateTime;
44+
import java.time.ZoneOffset;
4245
import java.time.format.DateTimeFormatter;
4346
{{/java8}}
4447
import java.util.Date;
@@ -236,6 +239,9 @@ public class JSON {
236239
*/
237240
public static class OffsetDateTimeTypeAdapter extends TypeAdapter<OffsetDateTime> {
238241
242+
private static final OffsetDateTime ZERO = LocalDateTime.of(0, 1, 1, 0, 0)
243+
.atOffset(ZoneOffset.UTC);
244+
239245
private DateTimeFormatter formatter;
240246
241247
public OffsetDateTimeTypeAdapter() {
@@ -255,6 +261,12 @@ public class JSON {
255261
if (date == null) {
256262
out.nullValue();
257263
} else {
264+
if (date.getYear() > 9999 || date.isBefore(ZERO)) {
265+
// https://tools.ietf.org/html/rfc3339
266+
throw new JsonIOException("OffsetDateTime is out of range. All dates and times are assumed to be "
267+
+ "in the \"current era\", somewhere between 0000AD and 9999AD.");
268+
}
269+
258270
out.value(formatter.format(date));
259271
}
260272
}

0 commit comments

Comments
 (0)