Skip to content

Exception is thrown when using Reads.DefaultInstantReads with an invalid Instant form #985

Description

@jhkldev

Play JSON Version (2.5.x / etc)

3.0.2

API (Scala / Java / Neither / Both)

Scala

Operating System (Ubuntu 15.10 / MacOS 10.10 / Windows 10)

macOS Ventura 13.6.4

JDK (Oracle 1.8.0_72, OpenJDK 1.8.x, Azul Zing)

openjdk 11.0.19 2023-04-18
OpenJDK Runtime Environment Temurin-11.0.19+7 (build 11.0.19+7)
OpenJDK 64-Bit Server VM Temurin-11.0.19+7 (build 11.0.19+7, mixed mode)

Expected Behavior

WhenReads.DefaultInstantReads reads an invalid Instant form, a JsError instance should be returned.

Reads.DefaultInstantReads reads JsString("2024-07-30T23:44:12.969")

val res0: play.api.libs.json.JsResult[java.time.Instant] = JsError(List((,List(JsonValidationError(List(error.expected.date.isoformat),ArraySeq((ParseCaseSensitive(false)(Value(Year,4,10,EXCEEDS_PAD)'-'Value(MonthOfYear,2)'-'Value(DayOfMonth,2))'T'(Value(HourOfDay,2)':'Value(MinuteOfHour,2)[':'Value(SecondOfMinute,2)[Fraction(NanoOfSecond,0,9,DecimalPoint)]]))[Offset(+HH:MM:ss,'Z')['['ParseCaseSensitive(true)ZoneRegionId()']']]))))))

Actual Behavior

A DateTimeException is thrown when Reads.DefaultInstantReads reads an invalid Instant form.

Reads.DefaultInstantReads reads JsString("2024-07-30T23:44:12.969")

java.time.DateTimeException: Unable to obtain Instant from TemporalAccessor: {},ISO resolved to 2024-07-30T23:44:12.969 of type java.time.format.Parsed
  at java.base/java.time.Instant.from(Instant.java:378)

Currently only a DateTimeParseException is caught when parsing an input string.

I believe the case statement should be changed to match against DateTimeException, which would catch both the Instant.from and formatter.parse exceptions. This was how the case statement was implemented in the 2.8.2 release.
https://github.com/playframework/play-json/blob/main/play-json/jvm/src/main/scala/play/api/libs/json/EnvReads.scala#L234

The same change could be made for LocalTimeFormatterParser.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions