Skip to content

Commit 8cd32ab

Browse files
authored
[java] Support 'date-time-local' format (#23395)
See https://spec.openapis.org/registry/format/date-time-local.html
1 parent 600c13a commit 8cd32ab

7 files changed

Lines changed: 125 additions & 6 deletions

File tree

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
@@ -674,9 +674,10 @@ public void processOpts() {
674674
importMapping.put("LocalDate", "java.time.LocalDate");
675675
typeMapping.put("time-local","LocalTime");
676676
importMapping.put("LocalTime", "java.time.LocalTime");
677+
typeMapping.put("date-time-local", "LocalDateTime");
678+
importMapping.put("LocalDateTime", "java.time.LocalDateTime");
677679
if ("java8-localdatetime".equals(dateLibrary)) {
678680
typeMapping.put("DateTime", "LocalDateTime");
679-
importMapping.put("LocalDateTime", "java.time.LocalDateTime");
680681
} else {
681682
typeMapping.put("DateTime", "OffsetDateTime");
682683
importMapping.put("OffsetDateTime", "java.time.OffsetDateTime");
@@ -1450,6 +1451,13 @@ public String toDefaultValue(CodegenProperty cp, Schema schema) {
14501451
}
14511452
}
14521453
return null;
1454+
} else if (ModelUtils.isDateTimeLocalSchema(schema)) {
1455+
if (schema.getDefault() != null) {
1456+
if ("java8".equals(getDateLibrary())) {
1457+
return String.format(Locale.ROOT, "LocalDateTime.parse(\"%s\")", String.valueOf(schema.getDefault()));
1458+
}
1459+
}
1460+
return null;
14531461
} else if (ModelUtils.isStringSchema(schema)) {
14541462
if (schema.getDefault() != null) {
14551463
String _default;
@@ -1536,6 +1544,10 @@ public String toDefaultValue(CodegenProperty cp, Schema schema) {
15361544
if("java8".equals(getDateLibrary())) {
15371545
defaultPropertyExpression = String.format(Locale.ROOT, "java.time.LocalTime.parse(\"%s\")", value.asText());
15381546
}
1547+
} else if(ModelUtils.isDateTimeLocalSchema(propertySchema)) {
1548+
if("java8".equals(getDateLibrary())) {
1549+
defaultPropertyExpression = String.format(Locale.ROOT, "java.time.LocalDateTime.parse(\"%s\")", value.asText());
1550+
}
15391551
} else if(ModelUtils.isUUIDSchema(propertySchema)) {
15401552
defaultPropertyExpression = "java.util.UUID.fromString(\"" + value.asText() + "\")";
15411553
} 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
@@ -715,6 +715,12 @@ public static boolean isDateTimeSchema(Schema schema) {
715715
&& SchemaTypeUtil.DATE_TIME_FORMAT.equals(schema.getFormat()));
716716
}
717717

718+
public static boolean isDateTimeLocalSchema(Schema schema) {
719+
// format: date-time-local, see https://spec.openapis.org/registry/format/date-time-local.html
720+
return (SchemaTypeUtil.STRING_TYPE.equals(getType(schema))
721+
&& "date-time-local".equals(schema.getFormat()));
722+
}
723+
718724
public static boolean isTimeLocalSchema(Schema schema) {
719725
// format: time-local, see https://spec.openapis.org/registry/format/time-local.html
720726
return (SchemaTypeUtil.STRING_TYPE.equals(getType(schema))

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
@@ -537,6 +537,15 @@ public void toDefaultValueDateTimeLegacyTest() {
537537

538538
// dateLibrary <> java8
539539
Assert.assertEquals(defaultValue, "10:15:30");
540+
541+
// Test default value for date-time-local format
542+
StringSchema dateTimeLocalSchema = new StringSchema();
543+
dateTimeLocalSchema.setFormat("date-time-local");
544+
dateTimeLocalSchema.setDefault(LocalDateTime.parse("2007-12-03T10:15:30"));
545+
defaultValue = codegen.toDefaultValue(dateTimeLocalSchema);
546+
547+
// dateLibrary <> java8
548+
Assert.assertEquals(defaultValue, "2007-12-03T10:15:30");
540549
}
541550

542551
@Test
@@ -606,6 +615,13 @@ public void toDefaultValueTest() {
606615
timeLocalSchema.setDefault("10:15:30");
607616
defaultValue = codegen.toDefaultValue(codegen.fromProperty("", timeLocalSchema), timeLocalSchema);
608617
Assert.assertEquals(defaultValue, "LocalTime.parse(\"10:15:30\")");
618+
619+
// Test default value for date-time-local format
620+
StringSchema dateTimeLocalSchema = new StringSchema();
621+
dateTimeLocalSchema.setFormat("date-time-local");
622+
dateTimeLocalSchema.setDefault("2007-12-03T10:15:30");
623+
defaultValue = codegen.toDefaultValue(codegen.fromProperty("", dateTimeLocalSchema), dateTimeLocalSchema);
624+
Assert.assertEquals(defaultValue, "LocalDateTime.parse(\"2007-12-03T10:15:30\")");
609625
}
610626

611627
@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
@@ -412,6 +412,35 @@ public void generateLocalTimeForTimeLocalFormat() throws IOException {
412412
.assertProperty("feedingTime").withType("LocalTime");
413413
}
414414

415+
@Test
416+
public void generateLocalDateTimeForDateTimeLocalFormat() throws IOException {
417+
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
418+
output.deleteOnExit();
419+
String outputPath = output.getAbsolutePath().replace('\\', '/');
420+
421+
OpenAPI openAPI = new OpenAPIParser()
422+
.readLocation("src/test/resources/3_0/spring/date-time-parameter-types-for-testing.yml", null, new ParseOptions()).getOpenAPI();
423+
424+
SpringCodegen codegen = new SpringCodegen();
425+
codegen.setOutputDir(output.getAbsolutePath());
426+
427+
ClientOptInput input = new ClientOptInput();
428+
input.openAPI(openAPI);
429+
input.config(codegen);
430+
431+
DefaultGenerator generator = new DefaultGenerator();
432+
generator.setGeneratorPropertyDefault(CodegenConstants.MODELS, "true");
433+
generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_TESTS, "false");
434+
generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_DOCS, "false");
435+
generator.setGeneratorPropertyDefault(CodegenConstants.APIS, "false");
436+
generator.setGenerateMetadata(false);
437+
generator.opts(input).generate();
438+
439+
JavaFileAssert.assertThat(Paths.get(outputPath + "/src/main/java/org/openapitools/model/Pet.java"))
440+
.hasImports("java.time.LocalDateTime")
441+
.assertProperty("adoptionDate").withType("LocalDateTime");
442+
}
443+
415444
@Test
416445
public void interfaceDefaultImplDisableWithResponseWrapper() {
417446
final SpringCodegen codegen = new SpringCodegen();

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,8 @@ components:
105105
feedingTime:
106106
type: string
107107
format: time-local
108-
default: '10:15:30'
108+
default: '10:15:30'
109+
adoptionDate:
110+
type: string
111+
format: date-time-local
112+
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.LocalTime;
1011
import java.time.OffsetDateTime;
1112
import org.springframework.format.annotation.DateTimeFormat;
@@ -43,6 +44,8 @@ public class Pet {
4344

4445
private LocalTime feedingTime = LocalTime.parse("10:15:30");
4546

47+
private LocalDateTime adoptionDate = LocalDateTime.parse("2007-12-03T10:15:30");
48+
4649
public Pet() {
4750
super();
4851
}
@@ -201,6 +204,27 @@ public void setFeedingTime(LocalTime feedingTime) {
201204
this.feedingTime = feedingTime;
202205
}
203206

207+
public Pet adoptionDate(LocalDateTime adoptionDate) {
208+
this.adoptionDate = adoptionDate;
209+
return this;
210+
}
211+
212+
/**
213+
* Get adoptionDate
214+
* @return adoptionDate
215+
*/
216+
@Valid
217+
@Schema(name = "adoptionDate", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
218+
@JsonProperty("adoptionDate")
219+
public LocalDateTime getAdoptionDate() {
220+
return adoptionDate;
221+
}
222+
223+
@JsonProperty("adoptionDate")
224+
public void setAdoptionDate(LocalDateTime adoptionDate) {
225+
this.adoptionDate = adoptionDate;
226+
}
227+
204228
@Override
205229
public boolean equals(Object o) {
206230
if (this == o) {
@@ -216,12 +240,13 @@ public boolean equals(Object o) {
216240
Objects.equals(this.price, pet.price) &&
217241
Objects.equals(this.lastFeed, pet.lastFeed) &&
218242
Objects.equals(this.dateOfBirth, pet.dateOfBirth) &&
219-
Objects.equals(this.feedingTime, pet.feedingTime);
243+
Objects.equals(this.feedingTime, pet.feedingTime) &&
244+
Objects.equals(this.adoptionDate, pet.adoptionDate);
220245
}
221246

222247
@Override
223248
public int hashCode() {
224-
return Objects.hash(atType, age, happy, price, lastFeed, dateOfBirth, feedingTime);
249+
return Objects.hash(atType, age, happy, price, lastFeed, dateOfBirth, feedingTime, adoptionDate);
225250
}
226251

227252
@Override
@@ -235,6 +260,7 @@ public String toString() {
235260
sb.append(" lastFeed: ").append(toIndentedString(lastFeed)).append("\n");
236261
sb.append(" dateOfBirth: ").append(toIndentedString(dateOfBirth)).append("\n");
237262
sb.append(" feedingTime: ").append(toIndentedString(feedingTime)).append("\n");
263+
sb.append(" adoptionDate: ").append(toIndentedString(adoptionDate)).append("\n");
238264
sb.append("}");
239265
return sb.toString();
240266
}

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.LocalTime;
1011
import java.time.OffsetDateTime;
1112
import org.springframework.format.annotation.DateTimeFormat;
@@ -43,6 +44,8 @@ public class Pet {
4344

4445
private LocalTime feedingTime = LocalTime.parse("10:15:30");
4546

47+
private LocalDateTime adoptionDate = LocalDateTime.parse("2007-12-03T10:15:30");
48+
4649
public Pet() {
4750
super();
4851
}
@@ -201,6 +204,27 @@ public void setFeedingTime(LocalTime feedingTime) {
201204
this.feedingTime = feedingTime;
202205
}
203206

207+
public Pet adoptionDate(LocalDateTime adoptionDate) {
208+
this.adoptionDate = adoptionDate;
209+
return this;
210+
}
211+
212+
/**
213+
* Get adoptionDate
214+
* @return adoptionDate
215+
*/
216+
@Valid
217+
@Schema(name = "adoptionDate", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
218+
@JsonProperty("adoptionDate")
219+
public LocalDateTime getAdoptionDate() {
220+
return adoptionDate;
221+
}
222+
223+
@JsonProperty("adoptionDate")
224+
public void setAdoptionDate(LocalDateTime adoptionDate) {
225+
this.adoptionDate = adoptionDate;
226+
}
227+
204228
@Override
205229
public boolean equals(Object o) {
206230
if (this == o) {
@@ -216,12 +240,13 @@ public boolean equals(Object o) {
216240
Objects.equals(this.price, pet.price) &&
217241
Objects.equals(this.lastFeed, pet.lastFeed) &&
218242
Objects.equals(this.dateOfBirth, pet.dateOfBirth) &&
219-
Objects.equals(this.feedingTime, pet.feedingTime);
243+
Objects.equals(this.feedingTime, pet.feedingTime) &&
244+
Objects.equals(this.adoptionDate, pet.adoptionDate);
220245
}
221246

222247
@Override
223248
public int hashCode() {
224-
return Objects.hash(atType, age, happy, price, lastFeed, dateOfBirth, feedingTime);
249+
return Objects.hash(atType, age, happy, price, lastFeed, dateOfBirth, feedingTime, adoptionDate);
225250
}
226251

227252
@Override
@@ -235,6 +260,7 @@ public String toString() {
235260
sb.append(" lastFeed: ").append(toIndentedString(lastFeed)).append("\n");
236261
sb.append(" dateOfBirth: ").append(toIndentedString(dateOfBirth)).append("\n");
237262
sb.append(" feedingTime: ").append(toIndentedString(feedingTime)).append("\n");
263+
sb.append(" adoptionDate: ").append(toIndentedString(adoptionDate)).append("\n");
238264
sb.append("}");
239265
return sb.toString();
240266
}

0 commit comments

Comments
 (0)