Skip to content

Conversation

@kevinoid
Copy link
Contributor

PR checklist

  • Read the contribution guildelines.
  • Ran the shell/batch script under ./bin/ to update Petstore sample so that CIs can verify the change. (For instance, only need to run ./bin/{LANG}-petstore.sh and ./bin/security/{LANG}-petstore.sh if updating the {LANG} (e.g. php, ruby, python, etc) code generator or {LANG} client's mustache templates)
  • Filed the PR against the correct branch: master for non-breaking changes and 2.3.0 branch for breaking (non-backward compatible) changes.

Description of the PR

The DateTimeFormatter returned by ISODateTimeFormat.dateTime() only parses dates with millisecond values, and throws IllegalArgumentException when milliseconds are not present. The
date-time construct from RFC 3339 Section 5.6 referenced by the Swagger/OpenAPI spec allows fractional second values to be omitted. This results in valid date-time values being rejected by the generated code.

This PR fixes the problem by using ISODateTimeFormat.dateTimeParser() for parsing, which correctly handles date-time values without fractional seconds. Note that .dateTime() must
still be used for printing, which is not supported by .dateTimeParser().

@cbornet
Copy link
Contributor

cbornet commented Dec 30, 2016

Yes but dateOptionalTimeParser should be used instead.
Related to #4234

@kevinoid
Copy link
Contributor Author

@cbornet Why dateOptionalTimeParser instead of dateTimeParser? The date-time format references the date-time construct from RFC 3339, which requires a time part. The date format, which does not have a time part, appears to be parsed by ISODateTimeFormat.date() in LocalDateTypeAdapter which I haven't changed.

@kevinoid
Copy link
Contributor Author

@cbornet I took a closer look at the grammars accepted by dateOptionalTimeParser and dateTimeParser. I had misread the documentation. It looks like the only difference is dateOptionalTimeParser does not accept a time without a date. I'll update the PR to use dateOptionalTimeParser shortly.

@kevinoid
Copy link
Contributor Author

@cbornet I noticed that dateTimeParser is currently used by the akka-scala generator. Should I change that to dateOptionalTimeParser as well?

The DateTimeFormatter returned by ISODateTimeFormat.dateTime() only
parses dates with millisecond values, and throws
IllegalArgumentException when milliseconds are not present.  The
date-time construct from RFC 3339 Section 5.6 referenced by the
Swagger/OpenAPI spec allows fractional second values to be omitted.
This results in valid date-time values being rejected by the generated
code.

This commit fixes the problem by using .dateOptionalTimeParser() for
parsing, which correctly handles date-time values without fractional
seconds.  A previous version of this commit used .dateTimeParser(),
which accepted a time without a date and was considered too liberal.
Note that .dateTime() must still be used for printing, which is not
supported by .dateTimeParser().

Signed-off-by: Kevin Locke <kevin@kevinlocke.name>
As in the previous commit, which fixed Java generators,
ISOISODateTimeFormat.dateOptionalTimeParser() should be used for
date-time parsing and ISOISODateTimeFormat.dateTime() for printing.
Apply the same change to akka-scala.

Signed-off-by: Kevin Locke <kevin@kevinlocke.name>
@kevinoid kevinoid force-pushed the fix-gson-joda-datetime branch from e26b85b to 5765cd5 Compare December 30, 2016 17:04
@kevinoid
Copy link
Contributor Author

PR updated and CI passed. I included the analogous akka-scala change as a separate commit that can easily be dropped if it should be handled separately.

@cbornet
Copy link
Contributor

cbornet commented Dec 31, 2016

👍

@wing328 wing328 added this to the v2.2.2 milestone Jan 6, 2017
@wing328 wing328 merged commit 409e1a5 into swagger-api:master Jan 7, 2017
@wing328
Copy link
Contributor

wing328 commented Jan 7, 2017

@kevinoid thanks for the contribution, especially to the akka-scala generator.

@kevinoid
Copy link
Contributor Author

kevinoid commented Jan 7, 2017

@wing328 Thanks for reviewing and merging it!

@wing328 wing328 changed the title Fix Gson parsing of Joda DateTime without millis [Java][okhttp] Fix Gson parsing of Joda DateTime without millis Feb 20, 2017
@wing328 wing328 changed the title [Java][okhttp] Fix Gson parsing of Joda DateTime without millis [Scala][Java][okhttp] Fix Gson parsing of Joda DateTime without millis Feb 20, 2017
davidgri pushed a commit to davidgri/swagger-codegen that referenced this pull request May 11, 2017
* Fix Gson parsing of Joda DateTime without millis

The DateTimeFormatter returned by ISODateTimeFormat.dateTime() only
parses dates with millisecond values, and throws
IllegalArgumentException when milliseconds are not present.  The
date-time construct from RFC 3339 Section 5.6 referenced by the
Swagger/OpenAPI spec allows fractional second values to be omitted.
This results in valid date-time values being rejected by the generated
code.

This commit fixes the problem by using .dateOptionalTimeParser() for
parsing, which correctly handles date-time values without fractional
seconds.  A previous version of this commit used .dateTimeParser(),
which accepted a time without a date and was considered too liberal.
Note that .dateTime() must still be used for printing, which is not
supported by .dateTimeParser().

Signed-off-by: Kevin Locke <kevin@kevinlocke.name>

* Fix akka-scala date-time parser with Joda

As in the previous commit, which fixed Java generators,
ISOISODateTimeFormat.dateOptionalTimeParser() should be used for
date-time parsing and ISOISODateTimeFormat.dateTime() for printing.
Apply the same change to akka-scala.

Signed-off-by: Kevin Locke <kevin@kevinlocke.name>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants