Skip to content

Commit 9e782e0

Browse files
committed
1 parent ed3acc8 commit 9e782e0

7 files changed

Lines changed: 125 additions & 5 deletions

File tree

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,8 @@ public void processOpts() {
676676
importMapping.put("LocalTime", "java.time.LocalTime");
677677
typeMapping.put("date-time-local", "LocalDateTime");
678678
importMapping.put("LocalDateTime", "java.time.LocalDateTime");
679+
typeMapping.put("duration", "Duration");
680+
importMapping.put("Duration", "java.time.Duration");
679681
if ("java8-localdatetime".equals(dateLibrary)) {
680682
typeMapping.put("DateTime", "LocalDateTime");
681683
} else {
@@ -1458,6 +1460,13 @@ public String toDefaultValue(CodegenProperty cp, Schema schema) {
14581460
}
14591461
}
14601462
return null;
1463+
} else if (ModelUtils.isDurationSchema(schema)) {
1464+
if (schema.getDefault() != null) {
1465+
if ("java8".equals(getDateLibrary())) {
1466+
return String.format(Locale.ROOT, "Duration.parse(\"%s\")", schema.getDefault());
1467+
}
1468+
}
1469+
return null;
14611470
} else if (ModelUtils.isStringSchema(schema)) {
14621471
if (schema.getDefault() != null) {
14631472
String _default;
@@ -1548,6 +1557,10 @@ public String toDefaultValue(CodegenProperty cp, Schema schema) {
15481557
if("java8".equals(getDateLibrary())) {
15491558
defaultPropertyExpression = String.format(Locale.ROOT, "java.time.LocalDateTime.parse(\"%s\")", value.asText());
15501559
}
1560+
} else if(ModelUtils.isDurationSchema(propertySchema)) {
1561+
if("java8".equals(getDateLibrary())) {
1562+
defaultPropertyExpression = String.format(Locale.ROOT, "java.time.Duration.parse(\"%s\")", value.asText());
1563+
}
15511564
} else if(ModelUtils.isUUIDSchema(propertySchema)) {
15521565
defaultPropertyExpression = "java.util.UUID.fromString(\"" + value.asText() + "\")";
15531566
} 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
@@ -721,6 +721,12 @@ public static boolean isDateTimeLocalSchema(Schema schema) {
721721
&& "date-time-local".equals(schema.getFormat()));
722722
}
723723

724+
public static boolean isDurationSchema(Schema schema) {
725+
// format: duration, see https://spec.openapis.org/registry/format/duration.html
726+
return (SchemaTypeUtil.STRING_TYPE.equals(getType(schema))
727+
&& "duration".equals(schema.getFormat()));
728+
}
729+
724730
public static boolean isTimeLocalSchema(Schema schema) {
725731
// format: time-local, see https://spec.openapis.org/registry/format/time-local.html
726732
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
@@ -546,6 +546,15 @@ public void toDefaultValueDateTimeLegacyTest() {
546546

547547
// dateLibrary <> java8
548548
Assert.assertEquals(defaultValue, "2007-12-03T10:15:30");
549+
550+
// Test default value for duration format
551+
StringSchema durationSchema = new StringSchema();
552+
durationSchema.setFormat("duration");
553+
durationSchema.setDefault(Duration.parse("PT20.345S"));
554+
defaultValue = codegen.toDefaultValue(durationSchema);
555+
556+
// dateLibrary <> java8
557+
Assert.assertEquals(defaultValue, "PT20.345S");
549558
}
550559

551560
@Test
@@ -622,6 +631,13 @@ public void toDefaultValueTest() {
622631
dateTimeLocalSchema.setDefault("2007-12-03T10:15:30");
623632
defaultValue = codegen.toDefaultValue(codegen.fromProperty("", dateTimeLocalSchema), dateTimeLocalSchema);
624633
Assert.assertEquals(defaultValue, "LocalDateTime.parse(\"2007-12-03T10:15:30\")");
634+
635+
// Test default value for duration format
636+
StringSchema durationSchema = new StringSchema();
637+
durationSchema.setFormat("duration");
638+
durationSchema.setDefault("PT20.345S");
639+
defaultValue = codegen.toDefaultValue(codegen.fromProperty("", durationSchema), durationSchema);
640+
Assert.assertEquals(defaultValue, "Duration.parse(\"PT20.345S\")");
625641
}
626642

627643
@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
@@ -441,6 +441,35 @@ public void generateLocalDateTimeForDateTimeLocalFormat() throws IOException {
441441
.assertProperty("adoptionDate").withType("LocalDateTime");
442442
}
443443

444+
@Test
445+
public void generateDurationForDurationFormat() throws IOException {
446+
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
447+
output.deleteOnExit();
448+
String outputPath = output.getAbsolutePath().replace('\\', '/');
449+
450+
OpenAPI openAPI = new OpenAPIParser()
451+
.readLocation("src/test/resources/3_0/spring/date-time-parameter-types-for-testing.yml", null, new ParseOptions()).getOpenAPI();
452+
453+
SpringCodegen codegen = new SpringCodegen();
454+
codegen.setOutputDir(output.getAbsolutePath());
455+
456+
ClientOptInput input = new ClientOptInput();
457+
input.openAPI(openAPI);
458+
input.config(codegen);
459+
460+
DefaultGenerator generator = new DefaultGenerator();
461+
generator.setGeneratorPropertyDefault(CodegenConstants.MODELS, "true");
462+
generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_TESTS, "false");
463+
generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_DOCS, "false");
464+
generator.setGeneratorPropertyDefault(CodegenConstants.APIS, "false");
465+
generator.setGenerateMetadata(false);
466+
generator.opts(input).generate();
467+
468+
JavaFileAssert.assertThat(Paths.get(outputPath + "/src/main/java/org/openapitools/model/Pet.java"))
469+
.hasImports("java.time.Duration")
470+
.assertProperty("tripDuration").withType("Duration");
471+
}
472+
444473
@Test
445474
public void interfaceDefaultImplDisableWithResponseWrapper() {
446475
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
@@ -109,4 +109,8 @@ components:
109109
adoptionDate:
110110
type: string
111111
format: date-time-local
112-
default: '2007-12-03T10:15:30'
112+
default: '2007-12-03T10:15:30'
113+
tripDuration:
114+
type: string
115+
format: duration
116+
default: 'PT10H15M30S'

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
@@ -5,6 +5,7 @@
55
import com.fasterxml.jackson.annotation.JsonProperty;
66
import com.fasterxml.jackson.annotation.JsonCreator;
77
import java.math.BigDecimal;
8+
import java.time.Duration;
89
import java.time.LocalDate;
910
import java.time.LocalDateTime;
1011
import java.time.LocalTime;
@@ -46,6 +47,8 @@ public class Pet {
4647

4748
private LocalDateTime adoptionDate = LocalDateTime.parse("2007-12-03T10:15:30");
4849

50+
private Duration tripDuration = Duration.parse("PT10H15M30S");
51+
4952
public Pet() {
5053
super();
5154
}
@@ -225,6 +228,27 @@ public void setAdoptionDate(LocalDateTime adoptionDate) {
225228
this.adoptionDate = adoptionDate;
226229
}
227230

231+
public Pet tripDuration(Duration tripDuration) {
232+
this.tripDuration = tripDuration;
233+
return this;
234+
}
235+
236+
/**
237+
* Get tripDuration
238+
* @return tripDuration
239+
*/
240+
@Valid
241+
@Schema(name = "tripDuration", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
242+
@JsonProperty("tripDuration")
243+
public Duration getTripDuration() {
244+
return tripDuration;
245+
}
246+
247+
@JsonProperty("tripDuration")
248+
public void setTripDuration(Duration tripDuration) {
249+
this.tripDuration = tripDuration;
250+
}
251+
228252
@Override
229253
public boolean equals(Object o) {
230254
if (this == o) {
@@ -241,12 +265,13 @@ public boolean equals(Object o) {
241265
Objects.equals(this.lastFeed, pet.lastFeed) &&
242266
Objects.equals(this.dateOfBirth, pet.dateOfBirth) &&
243267
Objects.equals(this.feedingTime, pet.feedingTime) &&
244-
Objects.equals(this.adoptionDate, pet.adoptionDate);
268+
Objects.equals(this.adoptionDate, pet.adoptionDate) &&
269+
Objects.equals(this.tripDuration, pet.tripDuration);
245270
}
246271

247272
@Override
248273
public int hashCode() {
249-
return Objects.hash(atType, age, happy, price, lastFeed, dateOfBirth, feedingTime, adoptionDate);
274+
return Objects.hash(atType, age, happy, price, lastFeed, dateOfBirth, feedingTime, adoptionDate, tripDuration);
250275
}
251276

252277
@Override
@@ -261,6 +286,7 @@ public String toString() {
261286
sb.append(" dateOfBirth: ").append(toIndentedString(dateOfBirth)).append("\n");
262287
sb.append(" feedingTime: ").append(toIndentedString(feedingTime)).append("\n");
263288
sb.append(" adoptionDate: ").append(toIndentedString(adoptionDate)).append("\n");
289+
sb.append(" tripDuration: ").append(toIndentedString(tripDuration)).append("\n");
264290
sb.append("}");
265291
return sb.toString();
266292
}

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
@@ -5,6 +5,7 @@
55
import com.fasterxml.jackson.annotation.JsonProperty;
66
import com.fasterxml.jackson.annotation.JsonCreator;
77
import java.math.BigDecimal;
8+
import java.time.Duration;
89
import java.time.LocalDate;
910
import java.time.LocalDateTime;
1011
import java.time.LocalTime;
@@ -46,6 +47,8 @@ public class Pet {
4647

4748
private LocalDateTime adoptionDate = LocalDateTime.parse("2007-12-03T10:15:30");
4849

50+
private Duration tripDuration = Duration.parse("PT10H15M30S");
51+
4952
public Pet() {
5053
super();
5154
}
@@ -225,6 +228,27 @@ public void setAdoptionDate(LocalDateTime adoptionDate) {
225228
this.adoptionDate = adoptionDate;
226229
}
227230

231+
public Pet tripDuration(Duration tripDuration) {
232+
this.tripDuration = tripDuration;
233+
return this;
234+
}
235+
236+
/**
237+
* Get tripDuration
238+
* @return tripDuration
239+
*/
240+
@Valid
241+
@Schema(name = "tripDuration", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
242+
@JsonProperty("tripDuration")
243+
public Duration getTripDuration() {
244+
return tripDuration;
245+
}
246+
247+
@JsonProperty("tripDuration")
248+
public void setTripDuration(Duration tripDuration) {
249+
this.tripDuration = tripDuration;
250+
}
251+
228252
@Override
229253
public boolean equals(Object o) {
230254
if (this == o) {
@@ -241,12 +265,13 @@ public boolean equals(Object o) {
241265
Objects.equals(this.lastFeed, pet.lastFeed) &&
242266
Objects.equals(this.dateOfBirth, pet.dateOfBirth) &&
243267
Objects.equals(this.feedingTime, pet.feedingTime) &&
244-
Objects.equals(this.adoptionDate, pet.adoptionDate);
268+
Objects.equals(this.adoptionDate, pet.adoptionDate) &&
269+
Objects.equals(this.tripDuration, pet.tripDuration);
245270
}
246271

247272
@Override
248273
public int hashCode() {
249-
return Objects.hash(atType, age, happy, price, lastFeed, dateOfBirth, feedingTime, adoptionDate);
274+
return Objects.hash(atType, age, happy, price, lastFeed, dateOfBirth, feedingTime, adoptionDate, tripDuration);
250275
}
251276

252277
@Override
@@ -261,6 +286,7 @@ public String toString() {
261286
sb.append(" dateOfBirth: ").append(toIndentedString(dateOfBirth)).append("\n");
262287
sb.append(" feedingTime: ").append(toIndentedString(feedingTime)).append("\n");
263288
sb.append(" adoptionDate: ").append(toIndentedString(adoptionDate)).append("\n");
289+
sb.append(" tripDuration: ").append(toIndentedString(tripDuration)).append("\n");
264290
sb.append("}");
265291
return sb.toString();
266292
}

0 commit comments

Comments
 (0)