|
| 1 | +package io.swagger.v3.core.resolving; |
| 2 | + |
| 3 | +import io.swagger.v3.core.converter.AnnotatedType; |
| 4 | +import io.swagger.v3.core.converter.ModelConverterContextImpl; |
| 5 | +import io.swagger.v3.core.jackson.ModelResolver; |
| 6 | +import io.swagger.v3.core.matchers.SerializationMatchers; |
| 7 | +import io.swagger.v3.core.resolving.resources.TestObjectJava8Dates; |
| 8 | +import io.swagger.v3.core.resolving.resources.TestObject2992; |
| 9 | +import io.swagger.v3.core.util.PrimitiveType; |
| 10 | +import io.swagger.v3.oas.models.media.DurationSchema; |
| 11 | +import io.swagger.v3.oas.models.media.Schema; |
| 12 | +import io.swagger.v3.oas.models.media.TimeLocalSchema; |
| 13 | +import io.swagger.v3.oas.models.media.TimeSchema; |
| 14 | +import org.testng.annotations.Test; |
| 15 | + |
| 16 | +import java.time.Duration; |
| 17 | +import java.time.LocalDateTime; |
| 18 | +import java.time.LocalTime; |
| 19 | +import java.time.OffsetTime; |
| 20 | +import java.util.Map; |
| 21 | + |
| 22 | +import static org.testng.Assert.assertEquals; |
| 23 | +import static org.testng.Assert.assertNotNull; |
| 24 | +import static org.testng.Assert.assertTrue; |
| 25 | + |
| 26 | +/** |
| 27 | + * Verifies Java 8 date/time type → OpenAPI format mappings (issue #5172). |
| 28 | + * |
| 29 | + * Default behaviour (fixed; previous complex-object expansion was always incorrect): |
| 30 | + * OffsetTime → "time" |
| 31 | + * Duration → "duration" |
| 32 | + * LocalTime → "time-local" |
| 33 | + * |
| 34 | + * Default behaviour (unchanged for compatibility): |
| 35 | + * LocalDateTime → "date-time" |
| 36 | + * |
| 37 | + * Opt-in via PrimitiveType.enableJava8Formats(): |
| 38 | + * LocalDateTime → "date-time-local" |
| 39 | + */ |
| 40 | +public class Java8DateFormatsTest extends SwaggerTestBase { |
| 41 | + |
| 42 | + @Test |
| 43 | + public void testDefaultFormats() throws Exception { |
| 44 | + final ModelResolver modelResolver = new ModelResolver(mapper()); |
| 45 | + final ModelConverterContextImpl context = new ModelConverterContextImpl(modelResolver); |
| 46 | + |
| 47 | + context.resolve(new AnnotatedType(TestObjectJava8Dates.class)); |
| 48 | + |
| 49 | + SerializationMatchers.assertEqualsToYaml(context.getDefinedModels(), "TestObjectJava8Dates:\n" + |
| 50 | + " type: object\n" + |
| 51 | + " properties:\n" + |
| 52 | + " localDateTime:\n" + |
| 53 | + " type: string\n" + |
| 54 | + " format: date-time\n" + |
| 55 | + " offsetDateTime:\n" + |
| 56 | + " type: string\n" + |
| 57 | + " format: date-time\n" + |
| 58 | + " zonedDateTime:\n" + |
| 59 | + " type: string\n" + |
| 60 | + " format: date-time\n" + |
| 61 | + " instant:\n" + |
| 62 | + " type: string\n" + |
| 63 | + " format: date-time\n" + |
| 64 | + " localDate:\n" + |
| 65 | + " type: string\n" + |
| 66 | + " format: date\n" + |
| 67 | + " offsetTime:\n" + |
| 68 | + " type: string\n" + |
| 69 | + " format: time\n" + |
| 70 | + " duration:\n" + |
| 71 | + " type: string\n" + |
| 72 | + " format: duration"); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void testEnableJava8Formats() throws Exception { |
| 77 | + // Save current state so other tests are not affected by the static customClasses map |
| 78 | + final Map<String, PrimitiveType> custom = PrimitiveType.customClasses(); |
| 79 | + final PrimitiveType prevLocalDateTime = custom.get("java.time.LocalDateTime"); |
| 80 | + final PrimitiveType prevLocalTime = custom.get("java.time.LocalTime"); |
| 81 | + |
| 82 | + PrimitiveType.enableJava8Formats(); |
| 83 | + try { |
| 84 | + final ModelResolver modelResolver = new ModelResolver(mapper()); |
| 85 | + final ModelConverterContextImpl context = new ModelConverterContextImpl(modelResolver); |
| 86 | + |
| 87 | + context.resolve(new AnnotatedType(TestObject2992.class)); |
| 88 | + |
| 89 | + // LocalDateTime → "date-time-local", LocalTime → "time-local" after opt-in |
| 90 | + SerializationMatchers.assertEqualsToYaml(context.getDefinedModels(), "TestObject2992:\n" + |
| 91 | + " type: object\n" + |
| 92 | + " properties:\n" + |
| 93 | + " name:\n" + |
| 94 | + " type: string\n" + |
| 95 | + " a:\n" + |
| 96 | + " type: string\n" + |
| 97 | + " format: time-local\n" + |
| 98 | + " b:\n" + |
| 99 | + " type: string\n" + |
| 100 | + " format: time-local\n" + |
| 101 | + " c:\n" + |
| 102 | + " type: string\n" + |
| 103 | + " format: time-local\n" + |
| 104 | + " d:\n" + |
| 105 | + " type: string\n" + |
| 106 | + " format: date-time-local\n" + |
| 107 | + " e:\n" + |
| 108 | + " type: string\n" + |
| 109 | + " format: date-time-local\n" + |
| 110 | + " f:\n" + |
| 111 | + " type: string\n" + |
| 112 | + " format: date-time-local"); |
| 113 | + } finally { |
| 114 | + // Restore previous state so subsequent tests are not affected |
| 115 | + if (prevLocalDateTime == null) custom.remove("java.time.LocalDateTime"); |
| 116 | + else custom.put("java.time.LocalDateTime", prevLocalDateTime); |
| 117 | + if (prevLocalTime == null) custom.remove("java.time.LocalTime"); |
| 118 | + else custom.put("java.time.LocalTime", prevLocalTime); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + public void testDefaultLocalTime() { |
| 124 | + // Isolate from any prior enablePartialTime()/enableJava8Formats() call left in the |
| 125 | + // shared static customClasses map by other tests (e.g. Ticket2992Test), so this checks |
| 126 | + // the true PrimitiveType default, regardless of test execution order. |
| 127 | + final String key = "java.time.LocalTime"; |
| 128 | + final Map<String, PrimitiveType> custom = PrimitiveType.customClasses(); |
| 129 | + final PrimitiveType previous = custom.remove(key); |
| 130 | + try { |
| 131 | + final Schema<?> schema = PrimitiveType.createProperty(LocalTime.class); |
| 132 | + assertNotNull(schema); |
| 133 | + assertEquals(schema.getClass(), TimeLocalSchema.class); |
| 134 | + assertEquals(schema.getFormat(), "time-local"); |
| 135 | + } finally { |
| 136 | + if (previous == null) custom.remove(key); |
| 137 | + else custom.put(key, previous); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + @Test |
| 142 | + public void testDefaultOffsetTime() { |
| 143 | + final Schema<?> schema = PrimitiveType.createProperty(OffsetTime.class); |
| 144 | + assertNotNull(schema); |
| 145 | + assertEquals(schema.getClass(), TimeSchema.class); |
| 146 | + assertEquals(schema.getFormat(), "time"); |
| 147 | + } |
| 148 | + |
| 149 | + @Test |
| 150 | + public void testDefaultDuration() { |
| 151 | + final Schema<?> schema = PrimitiveType.createProperty(Duration.class); |
| 152 | + assertNotNull(schema); |
| 153 | + assertEquals(schema.getClass(), DurationSchema.class); |
| 154 | + assertEquals(schema.getFormat(), "duration"); |
| 155 | + } |
| 156 | + |
| 157 | + @Test |
| 158 | + public void testDefaultFormats31() { |
| 159 | + assertSchema31(PrimitiveType.createProperty(OffsetTime.class, true), "time"); |
| 160 | + assertSchema31(PrimitiveType.createProperty(Duration.class, true), "duration"); |
| 161 | + |
| 162 | + // LocalTime: same guard as testDefaultLocalTime — isolate from customClasses |
| 163 | + final String key = "java.time.LocalTime"; |
| 164 | + final Map<String, PrimitiveType> custom = PrimitiveType.customClasses(); |
| 165 | + final PrimitiveType previous = custom.remove(key); |
| 166 | + try { |
| 167 | + assertSchema31(PrimitiveType.createProperty(LocalTime.class, true), "time-local"); |
| 168 | + } finally { |
| 169 | + if (previous == null) custom.remove(key); |
| 170 | + else custom.put(key, previous); |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + @Test |
| 175 | + public void testEnableJava8Formats31() { |
| 176 | + final Map<String, PrimitiveType> custom = PrimitiveType.customClasses(); |
| 177 | + final PrimitiveType previous = custom.get("java.time.LocalDateTime"); |
| 178 | + |
| 179 | + PrimitiveType.enableJava8Formats(); |
| 180 | + try { |
| 181 | + assertSchema31(PrimitiveType.createProperty(LocalDateTime.class, true), "date-time-local"); |
| 182 | + } finally { |
| 183 | + if (previous == null) custom.remove("java.time.LocalDateTime"); |
| 184 | + else custom.put("java.time.LocalDateTime", previous); |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + private static void assertSchema31(Schema<?> schema, String format) { |
| 189 | + assertNotNull(schema); |
| 190 | + assertNotNull(schema.getTypes()); |
| 191 | + assertTrue(schema.getTypes().contains("string")); |
| 192 | + assertEquals(schema.getFormat(), format); |
| 193 | + } |
| 194 | +} |
0 commit comments