Skip to content

Commit aee5ba3

Browse files
committed
[spring] Support 'date-time-local' format
See https://spec.openapis.org/registry/format/date-time-local.html
1 parent 05dd7fb commit aee5ba3

File tree

7 files changed

+124
-5
lines changed

7 files changed

+124
-5
lines changed

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractJavaCodegen.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -662,9 +662,10 @@ public void processOpts() {
662662
typeMapping.put("date", "LocalDate");
663663
importMapping.put("LocalDate", "java.time.LocalDate");
664664
importMapping.put("LocalTime", "java.time.LocalTime");
665+
typeMapping.put("date-time-local", "LocalDateTime");
666+
importMapping.put("LocalDateTime", "java.time.LocalDateTime");
665667
if ("java8-localdatetime".equals(dateLibrary)) {
666668
typeMapping.put("DateTime", "LocalDateTime");
667-
importMapping.put("LocalDateTime", "java.time.LocalDateTime");
668669
} else {
669670
typeMapping.put("DateTime", "OffsetDateTime");
670671
importMapping.put("OffsetDateTime", "java.time.OffsetDateTime");
@@ -1411,6 +1412,13 @@ public String toDefaultValue(CodegenProperty cp, Schema schema) {
14111412
return "URI.create(\"" + escapeText(String.valueOf(schema.getDefault())) + "\")";
14121413
}
14131414
return null;
1415+
} else if (ModelUtils.isDateTimeLocalSchema(schema)) {
1416+
if (schema.getDefault() != null) {
1417+
if ("java8".equals(getDateLibrary())) {
1418+
return String.format(Locale.ROOT, "LocalDateTime.parse(\"%s\")", String.valueOf(schema.getDefault()));
1419+
}
1420+
}
1421+
return null;
14141422
} else if (ModelUtils.isStringSchema(schema)) {
14151423
if (schema.getDefault() != null) {
14161424
String _default;
@@ -1493,6 +1501,10 @@ public String toDefaultValue(CodegenProperty cp, Schema schema) {
14931501
value.asText(),
14941502
"java.time.format.DateTimeFormatter.ISO_ZONED_DATE_TIME.withZone(java.time.ZoneId.systemDefault())");
14951503
}
1504+
} else if(ModelUtils.isDateTimeLocalSchema(propertySchema)) {
1505+
if("java8".equals(getDateLibrary())) {
1506+
defaultPropertyExpression = String.format(Locale.ROOT, "java.time.LocalDateTime.parse(\"%s\")", value.asText());
1507+
}
14961508
} else if(ModelUtils.isUUIDSchema(propertySchema)) {
14971509
defaultPropertyExpression = "java.util.UUID.fromString(\"" + value.asText() + "\")";
14981510
} else if(ModelUtils.isStringSchema(propertySchema)) {

modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,12 @@ public static boolean isDateTimeSchema(Schema schema) {
696696
&& SchemaTypeUtil.DATE_TIME_FORMAT.equals(schema.getFormat()));
697697
}
698698

699+
public static boolean isDateTimeLocalSchema(Schema schema) {
700+
// format: date-time-local, see https://spec.openapis.org/registry/format/date-time-local.html
701+
return (SchemaTypeUtil.STRING_TYPE.equals(getType(schema))
702+
&& "date-time-local".equals(schema.getFormat()));
703+
}
704+
699705
public static boolean isPasswordSchema(Schema schema) {
700706
return (schema instanceof PasswordSchema) ||
701707
// double

modules/openapi-generator/src/test/java/org/openapitools/codegen/java/AbstractJavaCodegenTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,15 @@ public void toDefaultValueDateTimeLegacyTest() {
531531

532532
// dateLibrary <> java8
533533
Assert.assertEquals(defaultValue, "1984-12-19T03:39:57-09:00");
534+
535+
// Test default value for date-time-local format
536+
StringSchema dateTimeLocalSchema = new StringSchema();
537+
dateTimeLocalSchema.setFormat("date-time-local");
538+
dateTimeLocalSchema.setDefault("2007-12-03T10:15:30");
539+
defaultValue = codegen.toDefaultValue(dateTimeLocalSchema);
540+
541+
// dateLibrary <> java8, should return null
542+
Assert.assertNull(defaultValue);
534543
}
535544

536545
@Test
@@ -593,6 +602,13 @@ public void toDefaultValueTest() {
593602
numberSchema.setFormat("double");
594603
defaultValue = codegen.toDefaultValue(codegen.fromProperty("", schema), numberSchema);
595604
Assert.assertEquals(defaultValue, doubleValue + "d");
605+
606+
// Test default value for date-time-local format
607+
StringSchema dateTimeLocalSchema = new StringSchema();
608+
dateTimeLocalSchema.setFormat("date-time-local");
609+
dateTimeLocalSchema.setDefault("2007-12-03T10:15:30");
610+
defaultValue = codegen.toDefaultValue(codegen.fromProperty("", dateTimeLocalSchema), dateTimeLocalSchema);
611+
Assert.assertEquals(defaultValue, "LocalDateTime.parse(\"2007-12-03T10:15:30\")");
596612
}
597613

598614
@Test

modules/openapi-generator/src/test/java/org/openapitools/codegen/java/spring/SpringCodegenTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,35 @@ public void generateFormatForDateAndDateTimeQueryParam() throws IOException {
381381
.containsWithNameAndAttributes("DateTimeFormat", ImmutableMap.of("iso", "DateTimeFormat.ISO.DATE_TIME"));
382382
}
383383

384+
@Test
385+
public void generateLocalDateTimeForDateTimeLocalFormat() throws IOException {
386+
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
387+
output.deleteOnExit();
388+
String outputPath = output.getAbsolutePath().replace('\\', '/');
389+
390+
OpenAPI openAPI = new OpenAPIParser()
391+
.readLocation("src/test/resources/3_0/spring/date-time-parameter-types-for-testing.yml", null, new ParseOptions()).getOpenAPI();
392+
393+
SpringCodegen codegen = new SpringCodegen();
394+
codegen.setOutputDir(output.getAbsolutePath());
395+
396+
ClientOptInput input = new ClientOptInput();
397+
input.openAPI(openAPI);
398+
input.config(codegen);
399+
400+
DefaultGenerator generator = new DefaultGenerator();
401+
generator.setGeneratorPropertyDefault(CodegenConstants.MODELS, "true");
402+
generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_TESTS, "false");
403+
generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_DOCS, "false");
404+
generator.setGeneratorPropertyDefault(CodegenConstants.APIS, "false");
405+
generator.setGenerateMetadata(false);
406+
generator.opts(input).generate();
407+
408+
JavaFileAssert.assertThat(Paths.get(outputPath + "/src/main/java/org/openapitools/model/Pet.java"))
409+
.hasImports("java.time.LocalDateTime")
410+
.assertProperty("adoptionDate").withType("LocalDateTime");
411+
}
412+
384413
@Test
385414
public void interfaceDefaultImplDisableWithResponseWrapper() {
386415
final SpringCodegen codegen = new SpringCodegen();

modules/openapi-generator/src/test/resources/3_0/spring/date-time-parameter-types-for-testing.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,7 @@ components:
102102
type: string
103103
format: date
104104
default: '2021-01-01'
105+
adoptionDate:
106+
type: string
107+
format: date-time-local
108+
default: '2007-12-03T10:15:30'

samples/client/petstore/spring-cloud-date-time/src/main/java/org/openapitools/model/Pet.java

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.fasterxml.jackson.annotation.JsonCreator;
77
import java.math.BigDecimal;
88
import java.time.LocalDate;
9+
import java.time.LocalDateTime;
910
import java.time.OffsetDateTime;
1011
import org.springframework.format.annotation.DateTimeFormat;
1112
import org.springframework.lang.Nullable;
@@ -40,6 +41,8 @@ public class Pet {
4041
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
4142
private LocalDate dateOfBirth = LocalDate.parse("2021-01-01");
4243

44+
private LocalDateTime adoptionDate = LocalDateTime.parse("2007-12-03T10:15:30");
45+
4346
public Pet() {
4447
super();
4548
}
@@ -177,6 +180,27 @@ public void setDateOfBirth(LocalDate dateOfBirth) {
177180
this.dateOfBirth = dateOfBirth;
178181
}
179182

183+
public Pet adoptionDate(LocalDateTime adoptionDate) {
184+
this.adoptionDate = adoptionDate;
185+
return this;
186+
}
187+
188+
/**
189+
* Get adoptionDate
190+
* @return adoptionDate
191+
*/
192+
@Valid
193+
@Schema(name = "adoptionDate", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
194+
@JsonProperty("adoptionDate")
195+
public LocalDateTime getAdoptionDate() {
196+
return adoptionDate;
197+
}
198+
199+
@JsonProperty("adoptionDate")
200+
public void setAdoptionDate(LocalDateTime adoptionDate) {
201+
this.adoptionDate = adoptionDate;
202+
}
203+
180204
@Override
181205
public boolean equals(Object o) {
182206
if (this == o) {
@@ -191,12 +215,13 @@ public boolean equals(Object o) {
191215
Objects.equals(this.happy, pet.happy) &&
192216
Objects.equals(this.price, pet.price) &&
193217
Objects.equals(this.lastFeed, pet.lastFeed) &&
194-
Objects.equals(this.dateOfBirth, pet.dateOfBirth);
218+
Objects.equals(this.dateOfBirth, pet.dateOfBirth) &&
219+
Objects.equals(this.adoptionDate, pet.adoptionDate);
195220
}
196221

197222
@Override
198223
public int hashCode() {
199-
return Objects.hash(atType, age, happy, price, lastFeed, dateOfBirth);
224+
return Objects.hash(atType, age, happy, price, lastFeed, dateOfBirth, adoptionDate);
200225
}
201226

202227
@Override
@@ -209,6 +234,7 @@ public String toString() {
209234
sb.append(" price: ").append(toIndentedString(price)).append("\n");
210235
sb.append(" lastFeed: ").append(toIndentedString(lastFeed)).append("\n");
211236
sb.append(" dateOfBirth: ").append(toIndentedString(dateOfBirth)).append("\n");
237+
sb.append(" adoptionDate: ").append(toIndentedString(adoptionDate)).append("\n");
212238
sb.append("}");
213239
return sb.toString();
214240
}

samples/openapi3/client/petstore/spring-cloud-date-time/src/main/java/org/openapitools/model/Pet.java

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.fasterxml.jackson.annotation.JsonCreator;
77
import java.math.BigDecimal;
88
import java.time.LocalDate;
9+
import java.time.LocalDateTime;
910
import java.time.OffsetDateTime;
1011
import org.springframework.format.annotation.DateTimeFormat;
1112
import org.springframework.lang.Nullable;
@@ -40,6 +41,8 @@ public class Pet {
4041
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
4142
private LocalDate dateOfBirth = LocalDate.parse("2021-01-01");
4243

44+
private LocalDateTime adoptionDate = LocalDateTime.parse("2007-12-03T10:15:30");
45+
4346
public Pet() {
4447
super();
4548
}
@@ -177,6 +180,27 @@ public void setDateOfBirth(LocalDate dateOfBirth) {
177180
this.dateOfBirth = dateOfBirth;
178181
}
179182

183+
public Pet adoptionDate(LocalDateTime adoptionDate) {
184+
this.adoptionDate = adoptionDate;
185+
return this;
186+
}
187+
188+
/**
189+
* Get adoptionDate
190+
* @return adoptionDate
191+
*/
192+
@Valid
193+
@Schema(name = "adoptionDate", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
194+
@JsonProperty("adoptionDate")
195+
public LocalDateTime getAdoptionDate() {
196+
return adoptionDate;
197+
}
198+
199+
@JsonProperty("adoptionDate")
200+
public void setAdoptionDate(LocalDateTime adoptionDate) {
201+
this.adoptionDate = adoptionDate;
202+
}
203+
180204
@Override
181205
public boolean equals(Object o) {
182206
if (this == o) {
@@ -191,12 +215,13 @@ public boolean equals(Object o) {
191215
Objects.equals(this.happy, pet.happy) &&
192216
Objects.equals(this.price, pet.price) &&
193217
Objects.equals(this.lastFeed, pet.lastFeed) &&
194-
Objects.equals(this.dateOfBirth, pet.dateOfBirth);
218+
Objects.equals(this.dateOfBirth, pet.dateOfBirth) &&
219+
Objects.equals(this.adoptionDate, pet.adoptionDate);
195220
}
196221

197222
@Override
198223
public int hashCode() {
199-
return Objects.hash(atType, age, happy, price, lastFeed, dateOfBirth);
224+
return Objects.hash(atType, age, happy, price, lastFeed, dateOfBirth, adoptionDate);
200225
}
201226

202227
@Override
@@ -209,6 +234,7 @@ public String toString() {
209234
sb.append(" price: ").append(toIndentedString(price)).append("\n");
210235
sb.append(" lastFeed: ").append(toIndentedString(lastFeed)).append("\n");
211236
sb.append(" dateOfBirth: ").append(toIndentedString(dateOfBirth)).append("\n");
237+
sb.append(" adoptionDate: ").append(toIndentedString(adoptionDate)).append("\n");
212238
sb.append("}");
213239
return sb.toString();
214240
}

0 commit comments

Comments
 (0)