Skip to content

Commit 3c9b3ed

Browse files
committed
test: Assert date-time format accepts all ISO-8601 fractional precisions
The OpenAPI 'date-time' string format delegates to OffsetDateTime.parse, which already accepts everything from second precision through nanosecond precision (and either Z or numeric offset). Pin that behaviour so we can keep emitting Java's natural toString() form on responses without fear of clients sending higher-precision timestamps back to us.
1 parent bce9b71 commit 3c9b3ed

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

src/test/java/com/retailsvc/http/validate/StringIntegerNumberTest.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,40 @@ void stringFormatIpv6() {
158158
.isEqualTo("format");
159159
}
160160

161+
@Test
162+
void stringFormatDateTimeAcceptsAnyIsoPrecision() {
163+
StringSchema s =
164+
new StringSchema(Set.of(TypeName.STRING), null, null, null, "date-time", null, Map.of());
165+
assertThatCode(() -> v.validate("2026-01-01T20:00Z", s, "/v")).doesNotThrowAnyException();
166+
assertThatCode(() -> v.validate("2026-01-01T20:00:00Z", s, "/v")).doesNotThrowAnyException();
167+
assertThatCode(() -> v.validate("2026-01-01T20:00:00.1Z", s, "/v")).doesNotThrowAnyException();
168+
assertThatCode(() -> v.validate("2026-01-01T20:00:00.123Z", s, "/v"))
169+
.doesNotThrowAnyException();
170+
assertThatCode(() -> v.validate("2026-01-01T20:00:00.000000Z", s, "/v"))
171+
.doesNotThrowAnyException();
172+
assertThatCode(() -> v.validate("2026-01-01T20:00:00.123456789Z", s, "/v"))
173+
.doesNotThrowAnyException();
174+
assertThatCode(() -> v.validate("2026-01-01T20:00:00+02:00", s, "/v"))
175+
.doesNotThrowAnyException();
176+
assertThatCode(() -> v.validate("2026-01-01T20:00:00.250-05:30", s, "/v"))
177+
.doesNotThrowAnyException();
178+
}
179+
180+
@Test
181+
void stringFormatDateTimeRejectsBadValues() {
182+
StringSchema s =
183+
new StringSchema(Set.of(TypeName.STRING), null, null, null, "date-time", null, Map.of());
184+
assertThatThrownBy(() -> v.validate("2026-01-01T20:00:00", s, "/v"))
185+
.extracting(t -> ((ValidationException) t).error().keyword())
186+
.isEqualTo("format");
187+
assertThatThrownBy(() -> v.validate("2026-01-01 20:00:00Z", s, "/v"))
188+
.extracting(t -> ((ValidationException) t).error().keyword())
189+
.isEqualTo("format");
190+
assertThatThrownBy(() -> v.validate("not-a-date", s, "/v"))
191+
.extracting(t -> ((ValidationException) t).error().keyword())
192+
.isEqualTo("format");
193+
}
194+
161195
@Test
162196
void integerWithMinMax() {
163197
IntegerSchema s =

0 commit comments

Comments
 (0)