-
Notifications
You must be signed in to change notification settings - Fork 2.3k
feat: support Java 8 date/time formats per OpenAPI Formats Registry (#5172) #5184
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
daniel-kmiecik
merged 7 commits into
swagger-api:master
from
kuntal1461:feature/5172-java8-date-formats
Aug 13, 2026
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ef05660
feat: add support for Java 8 date/time formats per OpenAPI Formats Re…
kuntal1461 59d7f7e
Merge branch 'master' into feature/5172-java8-date-formats
daniel-kmiecik 0f48087
fix: support Java 8 date/time formats (#5184)
daniel-kmiecik 71ea628
fix: support Java 8 date/time formats (#5184)
daniel-kmiecik 0081fdf
test: preserve state for partial-time format
daniel-kmiecik 1ffc92a
test: add tests
daniel-kmiecik e285232
chore: update version in comment
daniel-kmiecik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
modules/swagger-core/src/test/java/io/swagger/v3/core/resolving/Java8DateFormatsTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| package io.swagger.v3.core.resolving; | ||
|
|
||
| import io.swagger.v3.core.converter.AnnotatedType; | ||
| import io.swagger.v3.core.converter.ModelConverterContextImpl; | ||
| import io.swagger.v3.core.jackson.ModelResolver; | ||
| import io.swagger.v3.core.matchers.SerializationMatchers; | ||
| import io.swagger.v3.core.resolving.resources.TestObjectJava8Dates; | ||
| import io.swagger.v3.core.resolving.resources.TestObject2992; | ||
| import io.swagger.v3.core.util.PrimitiveType; | ||
| import org.testng.annotations.Test; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * Verifies Java 8 date/time type → OpenAPI format mappings (issue #5172). | ||
| * | ||
| * Default behaviour (backward-compatible): | ||
| * OffsetTime → "time" (fixed; was incorrectly a complex object) | ||
| * Duration → "duration" (fixed; was incorrectly a complex object) | ||
| * LocalDateTime → "date-time" (unchanged for compatibility) | ||
| * LocalTime → complex object (unchanged; call enableJava8Formats() to opt in) | ||
| * | ||
| * Opt-in via PrimitiveType.enableJava8Formats(): | ||
| * LocalDateTime → "date-time-local" | ||
| * LocalTime → "time-local" | ||
| */ | ||
| public class Java8DateFormatsTest extends SwaggerTestBase { | ||
|
|
||
| @Test | ||
| public void testDefaultFormats() throws Exception { | ||
| final ModelResolver modelResolver = new ModelResolver(mapper()); | ||
| final ModelConverterContextImpl context = new ModelConverterContextImpl(modelResolver); | ||
|
|
||
| context.resolve(new AnnotatedType(TestObjectJava8Dates.class)); | ||
|
|
||
| SerializationMatchers.assertEqualsToYaml(context.getDefinedModels(), "TestObjectJava8Dates:\n" + | ||
| " type: object\n" + | ||
| " properties:\n" + | ||
| " localDateTime:\n" + | ||
| " type: string\n" + | ||
| " format: date-time\n" + | ||
| " offsetDateTime:\n" + | ||
| " type: string\n" + | ||
| " format: date-time\n" + | ||
| " zonedDateTime:\n" + | ||
| " type: string\n" + | ||
| " format: date-time\n" + | ||
| " instant:\n" + | ||
| " type: string\n" + | ||
| " format: date-time\n" + | ||
| " localDate:\n" + | ||
| " type: string\n" + | ||
| " format: date\n" + | ||
| " offsetTime:\n" + | ||
| " type: string\n" + | ||
| " format: time\n" + | ||
| " duration:\n" + | ||
| " type: string\n" + | ||
| " format: duration"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testEnableJava8Formats() throws Exception { | ||
| // Save current state so other tests are not affected by the static customClasses map | ||
| final Map<String, PrimitiveType> custom = PrimitiveType.customClasses(); | ||
| final PrimitiveType prevLocalDateTime = custom.get("java.time.LocalDateTime"); | ||
| final PrimitiveType prevLocalTime = custom.get("java.time.LocalTime"); | ||
|
|
||
| PrimitiveType.enableJava8Formats(); | ||
| try { | ||
| final ModelResolver modelResolver = new ModelResolver(mapper()); | ||
| final ModelConverterContextImpl context = new ModelConverterContextImpl(modelResolver); | ||
|
|
||
| context.resolve(new AnnotatedType(TestObject2992.class)); | ||
|
|
||
| // LocalDateTime → "date-time-local", LocalTime → "time-local" after opt-in | ||
| SerializationMatchers.assertEqualsToYaml(context.getDefinedModels(), "TestObject2992:\n" + | ||
| " type: object\n" + | ||
| " properties:\n" + | ||
| " name:\n" + | ||
| " type: string\n" + | ||
| " a:\n" + | ||
| " type: string\n" + | ||
| " format: time-local\n" + | ||
| " b:\n" + | ||
| " type: string\n" + | ||
| " format: time-local\n" + | ||
| " c:\n" + | ||
| " type: string\n" + | ||
| " format: time-local\n" + | ||
| " d:\n" + | ||
| " type: string\n" + | ||
| " format: date-time-local\n" + | ||
| " e:\n" + | ||
| " type: string\n" + | ||
| " format: date-time-local\n" + | ||
| " f:\n" + | ||
| " type: string\n" + | ||
| " format: date-time-local"); | ||
| } finally { | ||
| // Restore previous state so subsequent tests are not affected | ||
| if (prevLocalDateTime == null) custom.remove("java.time.LocalDateTime"); | ||
| else custom.put("java.time.LocalDateTime", prevLocalDateTime); | ||
| if (prevLocalTime == null) custom.remove("java.time.LocalTime"); | ||
| else custom.put("java.time.LocalTime", prevLocalTime); | ||
| } | ||
| } | ||
| } |
44 changes: 44 additions & 0 deletions
44
...agger-core/src/test/java/io/swagger/v3/core/resolving/resources/TestObjectJava8Dates.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| package io.swagger.v3.core.resolving.resources; | ||
|
|
||
| import java.time.Duration; | ||
| import java.time.Instant; | ||
| import java.time.LocalDate; | ||
| import java.time.LocalDateTime; | ||
| import java.time.OffsetDateTime; | ||
| import java.time.OffsetTime; | ||
| import java.time.ZonedDateTime; | ||
|
|
||
| // LocalTime is intentionally excluded: its default behaviour depends on whether | ||
| // enablePartialTime() has been called by a preceding test (shared static state). | ||
| // LocalTime opt-in behaviour is covered in Java8DateFormatsTest#testEnableJava8Formats. | ||
| public class TestObjectJava8Dates { | ||
|
|
||
| private LocalDateTime localDateTime; | ||
| private OffsetDateTime offsetDateTime; | ||
| private ZonedDateTime zonedDateTime; | ||
| private Instant instant; | ||
| private LocalDate localDate; | ||
| private OffsetTime offsetTime; | ||
| private Duration duration; | ||
|
|
||
| public LocalDateTime getLocalDateTime() { return localDateTime; } | ||
| public void setLocalDateTime(LocalDateTime localDateTime) { this.localDateTime = localDateTime; } | ||
|
|
||
| public OffsetDateTime getOffsetDateTime() { return offsetDateTime; } | ||
| public void setOffsetDateTime(OffsetDateTime offsetDateTime) { this.offsetDateTime = offsetDateTime; } | ||
|
|
||
| public ZonedDateTime getZonedDateTime() { return zonedDateTime; } | ||
| public void setZonedDateTime(ZonedDateTime zonedDateTime) { this.zonedDateTime = zonedDateTime; } | ||
|
|
||
| public Instant getInstant() { return instant; } | ||
| public void setInstant(Instant instant) { this.instant = instant; } | ||
|
|
||
| public LocalDate getLocalDate() { return localDate; } | ||
| public void setLocalDate(LocalDate localDate) { this.localDate = localDate; } | ||
|
|
||
| public OffsetTime getOffsetTime() { return offsetTime; } | ||
| public void setOffsetTime(OffsetTime offsetTime) { this.offsetTime = offsetTime; } | ||
|
|
||
| public Duration getDuration() { return duration; } | ||
| public void setDuration(Duration duration) { this.duration = duration; } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.