Skip to content

Commit 1e60f43

Browse files
authored
1 parent 1b26181 commit 1e60f43

7 files changed

Lines changed: 124 additions & 8 deletions

File tree

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,7 @@ public void processOpts() {
672672
additionalProperties.put("jsr310", "true");
673673
typeMapping.put("date", "LocalDate");
674674
importMapping.put("LocalDate", "java.time.LocalDate");
675+
typeMapping.put("time-local","LocalTime");
675676
importMapping.put("LocalTime", "java.time.LocalTime");
676677
if ("java8-localdatetime".equals(dateLibrary)) {
677678
typeMapping.put("DateTime", "LocalDateTime");
@@ -1442,6 +1443,13 @@ public String toDefaultValue(CodegenProperty cp, Schema schema) {
14421443
return "URI.create(\"" + escapeText(String.valueOf(schema.getDefault())) + "\")";
14431444
}
14441445
return null;
1446+
} else if (ModelUtils.isTimeLocalSchema(schema)) {
1447+
if (schema.getDefault() != null) {
1448+
if ("java8".equals(getDateLibrary())) {
1449+
return String.format(Locale.ROOT, "LocalTime.parse(\"%s\")", schema.getDefault());
1450+
}
1451+
}
1452+
return null;
14451453
} else if (ModelUtils.isStringSchema(schema)) {
14461454
if (schema.getDefault() != null) {
14471455
String _default;
@@ -1524,6 +1532,10 @@ public String toDefaultValue(CodegenProperty cp, Schema schema) {
15241532
value.asText(),
15251533
"java.time.format.DateTimeFormatter.ISO_ZONED_DATE_TIME.withZone(java.time.ZoneId.systemDefault())");
15261534
}
1535+
} else if(ModelUtils.isTimeLocalSchema(propertySchema)) {
1536+
if("java8".equals(getDateLibrary())) {
1537+
defaultPropertyExpression = String.format(Locale.ROOT, "java.time.LocalTime.parse(\"%s\")", value.asText());
1538+
}
15271539
} else if(ModelUtils.isUUIDSchema(propertySchema)) {
15281540
defaultPropertyExpression = "java.util.UUID.fromString(\"" + value.asText() + "\")";
15291541
} 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 isTimeLocalSchema(Schema schema) {
719+
// format: time-local, see https://spec.openapis.org/registry/format/time-local.html
720+
return (SchemaTypeUtil.STRING_TYPE.equals(getType(schema))
721+
&& "time-local".equals(schema.getFormat()));
722+
}
723+
718724
public static boolean isPasswordSchema(Schema schema) {
719725
return (schema instanceof PasswordSchema) ||
720726
// double

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

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,7 @@
3838

3939
import java.io.File;
4040
import java.math.BigDecimal;
41-
import java.time.LocalDate;
42-
import java.time.OffsetDateTime;
43-
import java.time.ZoneId;
44-
import java.time.ZonedDateTime;
41+
import java.time.*;
4542
import java.util.*;
4643
import java.util.stream.Collectors;
4744

@@ -531,6 +528,15 @@ public void toDefaultValueDateTimeLegacyTest() {
531528

532529
// dateLibrary <> java8
533530
Assert.assertEquals(defaultValue, "1984-12-19T03:39:57-09:00");
531+
532+
// Test default value for time-local format
533+
StringSchema timeLocalSchema = new StringSchema();
534+
timeLocalSchema.setFormat("time-local");
535+
timeLocalSchema.setDefault(LocalTime.parse("10:15:30"));
536+
defaultValue = codegen.toDefaultValue(timeLocalSchema);
537+
538+
// dateLibrary <> java8
539+
Assert.assertEquals(defaultValue, "10:15:30");
534540
}
535541

536542
@Test
@@ -593,6 +599,13 @@ public void toDefaultValueTest() {
593599
numberSchema.setFormat("double");
594600
defaultValue = codegen.toDefaultValue(codegen.fromProperty("", schema), numberSchema);
595601
Assert.assertEquals(defaultValue, doubleValue + "d");
602+
603+
// Test default value for time-local format
604+
StringSchema timeLocalSchema = new StringSchema();
605+
timeLocalSchema.setFormat("time-local");
606+
timeLocalSchema.setDefault("10:15:30");
607+
defaultValue = codegen.toDefaultValue(codegen.fromProperty("", timeLocalSchema), timeLocalSchema);
608+
Assert.assertEquals(defaultValue, "LocalTime.parse(\"10:15:30\")");
596609
}
597610

598611
@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
@@ -383,6 +383,35 @@ public void generateFormatForDateAndDateTimeQueryParam() throws IOException {
383383
.containsWithNameAndAttributes("DateTimeFormat", ImmutableMap.of("iso", "DateTimeFormat.ISO.DATE_TIME"));
384384
}
385385

386+
@Test
387+
public void generateLocalTimeForTimeLocalFormat() throws IOException {
388+
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
389+
output.deleteOnExit();
390+
String outputPath = output.getAbsolutePath().replace('\\', '/');
391+
392+
OpenAPI openAPI = new OpenAPIParser()
393+
.readLocation("src/test/resources/3_0/spring/date-time-parameter-types-for-testing.yml", null, new ParseOptions()).getOpenAPI();
394+
395+
SpringCodegen codegen = new SpringCodegen();
396+
codegen.setOutputDir(output.getAbsolutePath());
397+
398+
ClientOptInput input = new ClientOptInput();
399+
input.openAPI(openAPI);
400+
input.config(codegen);
401+
402+
DefaultGenerator generator = new DefaultGenerator();
403+
generator.setGeneratorPropertyDefault(CodegenConstants.MODELS, "true");
404+
generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_TESTS, "false");
405+
generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_DOCS, "false");
406+
generator.setGeneratorPropertyDefault(CodegenConstants.APIS, "false");
407+
generator.setGenerateMetadata(false);
408+
generator.opts(input).generate();
409+
410+
JavaFileAssert.assertThat(Paths.get(outputPath + "/src/main/java/org/openapitools/model/Pet.java"))
411+
.hasImports("java.time.LocalTime")
412+
.assertProperty("feedingTime").withType("LocalTime");
413+
}
414+
386415
@Test
387416
public void interfaceDefaultImplDisableWithResponseWrapper() {
388417
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+
feedingTime:
106+
type: string
107+
format: time-local
108+
default: '10: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.LocalTime;
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 LocalTime feedingTime = LocalTime.parse("10: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 feedingTime(LocalTime feedingTime) {
184+
this.feedingTime = feedingTime;
185+
return this;
186+
}
187+
188+
/**
189+
* Get feedingTime
190+
* @return feedingTime
191+
*/
192+
@Valid
193+
@Schema(name = "feedingTime", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
194+
@JsonProperty("feedingTime")
195+
public LocalTime getFeedingTime() {
196+
return feedingTime;
197+
}
198+
199+
@JsonProperty("feedingTime")
200+
public void setFeedingTime(LocalTime feedingTime) {
201+
this.feedingTime = feedingTime;
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.feedingTime, pet.feedingTime);
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, feedingTime);
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(" feedingTime: ").append(toIndentedString(feedingTime)).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.LocalTime;
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 LocalTime feedingTime = LocalTime.parse("10: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 feedingTime(LocalTime feedingTime) {
184+
this.feedingTime = feedingTime;
185+
return this;
186+
}
187+
188+
/**
189+
* Get feedingTime
190+
* @return feedingTime
191+
*/
192+
@Valid
193+
@Schema(name = "feedingTime", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
194+
@JsonProperty("feedingTime")
195+
public LocalTime getFeedingTime() {
196+
return feedingTime;
197+
}
198+
199+
@JsonProperty("feedingTime")
200+
public void setFeedingTime(LocalTime feedingTime) {
201+
this.feedingTime = feedingTime;
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.feedingTime, pet.feedingTime);
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, feedingTime);
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(" feedingTime: ").append(toIndentedString(feedingTime)).append("\n");
212238
sb.append("}");
213239
return sb.toString();
214240
}

0 commit comments

Comments
 (0)