Skip to content

feat: support Java 8 date/time formats per OpenAPI Formats Registry (#5172)#5184

Open
kuntal1461 wants to merge 1 commit into
swagger-api:masterfrom
kuntal1461:feature/5172-java8-date-formats
Open

feat: support Java 8 date/time formats per OpenAPI Formats Registry (#5172)#5184
kuntal1461 wants to merge 1 commit into
swagger-api:masterfrom
kuntal1461:feature/5172-java8-date-formats

Conversation

@kuntal1461
Copy link
Copy Markdown
Contributor

Description

Fixes #5172

Java 8 introduced java.time.* types that have become the standard for date/time handling, largely replacing java.util.Date and Joda-Time. The OpenAPI Formats Registry now defines standard format strings that map to these types. This PR brings swagger-core into alignment with that registry.

What was wrong

Java Type Before this PR Should be
java.time.OffsetTime Expanded as complex object (broken) "time"
java.time.Duration Expanded as complex object (broken) "duration"
java.time.LocalDateTime "date-time" (wrong format name) "date-time-local"
java.time.LocalTime Complex object, or "partial-time" via opt-in "time-local"

OffsetTime and Duration had no mapping at all — they fell through to Jackson's object expansion, producing unusable schemas with internal JDK fields. LocalDateTime was mapped to "date-time" which is the RFC 3339 offset date-time format — incorrect for a type that carries no timezone.

Why not just fix everything as the default?

LocalDateTime → "date-time" is the current default and has been since the early versions of this library. Many existing users depend on this mapping in generated specs, client SDKs, and validation logic. Silently changing it would break them on upgrade with no warning.

OffsetTime and Duration are fixed as the new default because their previous output (a complex object schema) was never usable — no user could have a working integration built on that output.

What this PR does

Fixed by default (no user action required):

  • java.time.OffsetTimestring / "time"
  • java.time.Durationstring / "duration"

Opt-in via PrimitiveType.enableJava8Formats():

  • java.time.LocalDateTimestring / "date-time-local"
  • java.time.LocalTimestring / "time-local"
// Add once at application startup to get fully correct OpenAPI Formats Registry mappings
PrimitiveType.enableJava8Formats();

This follows the same opt-in pattern already established by enablePartialTime() in this codebase.

enablePartialTime() is deprecated in favour of enableJava8Formats(), which supersedes it with the correct registry format name ("time-local" instead of "partial-time"). The old method is kept as-is so existing callers are not broken.

Migration path

v2.2.51 (this PR) → call enableJava8Formats() to opt in
v3.x (next major) → enableJava8Formats() behaviour becomes the default

New schema model classes (swagger-models)

Class Format
DateTimeLocalSchema "date-time-local"
TimeSchema "time"
TimeLocalSchema "time-local"
DurationSchema "duration"

Each follows the same structure as the existing DateTimeSchema and DateSchema.

Type of Change

  • ✨ New feature
  • 🐛 Bug fix (OffsetTime and Duration were broken by default)
  • 📝 Documentation (enablePartialTime() deprecation Javadoc)

Checklist

  • I have added/updated tests as needed (Java8DateFormatsTest covers both default and opt-in behaviour)
  • The PR title is descriptive
  • The code builds and passes tests locally (689 tests, 0 failures)
  • I have linked related issues ([Feature]: Support for Java 8 date formats #5172)

…gistry (swagger-api#5172)

- Add DateTimeLocalSchema, TimeSchema, TimeLocalSchema, DurationSchema model classes
- Map java.time.OffsetTime  → format "time"     by default (was incorrect complex object)
- Map java.time.Duration    → format "duration"  by default (was incorrect complex object)
- Add PrimitiveType.enableJava8Formats() opt-in to map:
    java.time.LocalDateTime → "date-time-local"
    java.time.LocalTime     → "time-local"
- Deprecate enablePartialTime() in favour of enableJava8Formats()
- Add Java8DateFormatsTest and TestObjectJava8Dates covering default and opt-in behaviour

LocalDateTime and LocalTime are opt-in only to preserve backward compatibility.
The default will change in the next major version.

Authored-By: Kuntal Maity <kuntal.1461@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature]: Support for Java 8 date formats

1 participant