Skip to content

Commit

Permalink
Add date time format annotation on pojo for model query parameters (#…
Browse files Browse the repository at this point in the history
…5437)

* Add date time format annotation on pojo for model query parameters

* Regenetare samples

* update spring samples

Co-authored-by: William Cheng <wing328hk@gmail.com>
  • Loading branch information
zalito12 and wing328 authored Apr 12, 2020
1 parent 800293c commit 6a158de
Show file tree
Hide file tree
Showing 39 changed files with 131 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ public class {{classname}} {{#parent}}extends {{{parent}}}{{/parent}}{{^parent}}
private {{>nullableDataType}} {{name}} = {{#isNullable}}JsonNullable.undefined(){{/isNullable}}{{^isNullable}}{{#required}}{{{defaultValue}}}{{/required}}{{^required}}null{{/required}}{{/isNullable}};
{{/isContainer}}
{{^isContainer}}
{{#isDate}}
@org.springframework.format.annotation.DateTimeFormat(iso = org.springframework.format.annotation.DateTimeFormat.ISO.DATE)
{{/isDate}}
{{#isDateTime}}
@org.springframework.format.annotation.DateTimeFormat(iso = org.springframework.format.annotation.DateTimeFormat.ISO.DATE_TIME)
{{/isDateTime}}
private {{>nullableDataType}} {{name}}{{#isNullable}} = JsonNullable.undefined(){{/isNullable}}{{^isNullable}}{{#defaultValue}} = {{{.}}}{{/defaultValue}}{{/isNullable}};
{{/isContainer}}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -544,4 +544,31 @@ public void doGenerateCookieParams() throws IOException {
checkFileContains(generator, outputPath + "/src/main/java/org/openapitools/api/ZebrasApi.java", "@CookieValue");
checkFileNotContains(generator, outputPath + "/src/main/java/org/openapitools/api/BirdsApi.java", "@CookieValue");
}

@Test
public void doAnnotateDatesOnModelParameters() throws IOException {
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
output.deleteOnExit();
String outputPath = output.getAbsolutePath().replace('\\', '/');

OpenAPI openAPI = new OpenAPIParser()
.readLocation("src/test/resources/3_0/issue_5436.yml", null, new ParseOptions()).getOpenAPI();

SpringCodegen codegen = new SpringCodegen();
codegen.setOutputDir(output.getAbsolutePath());
codegen.additionalProperties().put(CXFServerFeatures.LOAD_TEST_DATA_FROM_FILE, "true");

ClientOptInput input = new ClientOptInput();
input.openAPI(openAPI);
input.config(codegen);

MockDefaultGenerator generator = new MockDefaultGenerator();
generator.opts(input).generate();

checkFileContains(generator, outputPath + "/src/main/java/org/openapitools/api/ZebrasApi.java",
"AnimalParams");
checkFileContains(generator, outputPath + "/src/main/java/org/openapitools/model/AnimalParams.java",
"@org.springframework.format.annotation.DateTimeFormat(iso = org.springframework.format.annotation.DateTimeFormat.ISO.DATE)",
"@org.springframework.format.annotation.DateTimeFormat(iso = org.springframework.format.annotation.DateTimeFormat.ISO.DATE_TIME)");
}
}
51 changes: 51 additions & 0 deletions modules/openapi-generator/src/test/resources/3_0/issue_5436.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
openapi: 3.0.0
servers:
- url: 'localhost:8080'
info:
version: 1.0.0
title: OpenAPI Zoo
license:
name: Apache-2.0
url: 'https://www.apache.org/licenses/LICENSE-2.0.html'
paths:
/zebras:
get:
operationId: getZebras
parameters:
- in: query
name: limit
schema:
type: number
- $ref: '#/components/parameters/SearchParams'
components:
parameters:
SearchParams:
name: animalParams
description: Search animal grouped parameters
in: query
style: form
explode: true
schema:
$ref: '#/components/schemas/AnimalParams'
schemas:
AnimalParams:
type: object
properties:
born:
type: string
format: date
example: '2019-12-01'
lastSeen:
type: string
format: date-time
example: '2020-02-22T10:30:00.000'
status:
type: integer
enum: [0,1]
default: 0
name:
type: string
example: 'Marty'
age:
type: integer
example: 15
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class Order {
private Integer quantity;

@JsonProperty("shipDate")
@org.springframework.format.annotation.DateTimeFormat(iso = org.springframework.format.annotation.DateTimeFormat.ISO.DATE_TIME)
private OffsetDateTime shipDate;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class Order {
private Integer quantity;

@JsonProperty("shipDate")
@org.springframework.format.annotation.DateTimeFormat(iso = org.springframework.format.annotation.DateTimeFormat.ISO.DATE_TIME)
private OffsetDateTime shipDate;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class Order {
private Integer quantity;

@JsonProperty("shipDate")
@org.springframework.format.annotation.DateTimeFormat(iso = org.springframework.format.annotation.DateTimeFormat.ISO.DATE_TIME)
private OffsetDateTime shipDate;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,11 @@ public class FormatTest {
private Resource binary;

@JsonProperty("date")
@org.springframework.format.annotation.DateTimeFormat(iso = org.springframework.format.annotation.DateTimeFormat.ISO.DATE)
private LocalDate date;

@JsonProperty("dateTime")
@org.springframework.format.annotation.DateTimeFormat(iso = org.springframework.format.annotation.DateTimeFormat.ISO.DATE_TIME)
private OffsetDateTime dateTime;

@JsonProperty("uuid")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class MixedPropertiesAndAdditionalPropertiesClass {
private UUID uuid;

@JsonProperty("dateTime")
@org.springframework.format.annotation.DateTimeFormat(iso = org.springframework.format.annotation.DateTimeFormat.ISO.DATE_TIME)
private OffsetDateTime dateTime;

@JsonProperty("map")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class Order {
private Integer quantity;

@JsonProperty("shipDate")
@org.springframework.format.annotation.DateTimeFormat(iso = org.springframework.format.annotation.DateTimeFormat.ISO.DATE_TIME)
private OffsetDateTime shipDate;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,11 @@ public class FormatTest {
private Resource binary;

@JsonProperty("date")
@org.springframework.format.annotation.DateTimeFormat(iso = org.springframework.format.annotation.DateTimeFormat.ISO.DATE)
private LocalDate date;

@JsonProperty("dateTime")
@org.springframework.format.annotation.DateTimeFormat(iso = org.springframework.format.annotation.DateTimeFormat.ISO.DATE_TIME)
private LocalDateTime dateTime;

@JsonProperty("uuid")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class MixedPropertiesAndAdditionalPropertiesClass {
private UUID uuid;

@JsonProperty("dateTime")
@org.springframework.format.annotation.DateTimeFormat(iso = org.springframework.format.annotation.DateTimeFormat.ISO.DATE_TIME)
private LocalDateTime dateTime;

@JsonProperty("map")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class Order {
private Integer quantity;

@JsonProperty("shipDate")
@org.springframework.format.annotation.DateTimeFormat(iso = org.springframework.format.annotation.DateTimeFormat.ISO.DATE_TIME)
private LocalDateTime shipDate;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,11 @@ public class FormatTest {
private Resource binary;

@JsonProperty("date")
@org.springframework.format.annotation.DateTimeFormat(iso = org.springframework.format.annotation.DateTimeFormat.ISO.DATE)
private LocalDate date;

@JsonProperty("dateTime")
@org.springframework.format.annotation.DateTimeFormat(iso = org.springframework.format.annotation.DateTimeFormat.ISO.DATE_TIME)
private OffsetDateTime dateTime;

@JsonProperty("uuid")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class MixedPropertiesAndAdditionalPropertiesClass {
private UUID uuid;

@JsonProperty("dateTime")
@org.springframework.format.annotation.DateTimeFormat(iso = org.springframework.format.annotation.DateTimeFormat.ISO.DATE_TIME)
private OffsetDateTime dateTime;

@JsonProperty("map")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class Order {
private Integer quantity;

@JsonProperty("shipDate")
@org.springframework.format.annotation.DateTimeFormat(iso = org.springframework.format.annotation.DateTimeFormat.ISO.DATE_TIME)
private OffsetDateTime shipDate;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,11 @@ public class FormatTest {
private Resource binary;

@JsonProperty("date")
@org.springframework.format.annotation.DateTimeFormat(iso = org.springframework.format.annotation.DateTimeFormat.ISO.DATE)
private LocalDate date;

@JsonProperty("dateTime")
@org.springframework.format.annotation.DateTimeFormat(iso = org.springframework.format.annotation.DateTimeFormat.ISO.DATE_TIME)
private OffsetDateTime dateTime;

@JsonProperty("uuid")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class MixedPropertiesAndAdditionalPropertiesClass {
private UUID uuid;

@JsonProperty("dateTime")
@org.springframework.format.annotation.DateTimeFormat(iso = org.springframework.format.annotation.DateTimeFormat.ISO.DATE_TIME)
private OffsetDateTime dateTime;

@JsonProperty("map")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class Order {
private Integer quantity;

@JsonProperty("shipDate")
@org.springframework.format.annotation.DateTimeFormat(iso = org.springframework.format.annotation.DateTimeFormat.ISO.DATE_TIME)
private OffsetDateTime shipDate;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,11 @@ public class FormatTest {
private Resource binary;

@JsonProperty("date")
@org.springframework.format.annotation.DateTimeFormat(iso = org.springframework.format.annotation.DateTimeFormat.ISO.DATE)
private LocalDate date;

@JsonProperty("dateTime")
@org.springframework.format.annotation.DateTimeFormat(iso = org.springframework.format.annotation.DateTimeFormat.ISO.DATE_TIME)
private OffsetDateTime dateTime;

@JsonProperty("uuid")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class MixedPropertiesAndAdditionalPropertiesClass {
private UUID uuid;

@JsonProperty("dateTime")
@org.springframework.format.annotation.DateTimeFormat(iso = org.springframework.format.annotation.DateTimeFormat.ISO.DATE_TIME)
private OffsetDateTime dateTime;

@JsonProperty("map")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class Order {
private Integer quantity;

@JsonProperty("shipDate")
@org.springframework.format.annotation.DateTimeFormat(iso = org.springframework.format.annotation.DateTimeFormat.ISO.DATE_TIME)
private OffsetDateTime shipDate;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,11 @@ public class FormatTest {
private Resource binary;

@JsonProperty("date")
@org.springframework.format.annotation.DateTimeFormat(iso = org.springframework.format.annotation.DateTimeFormat.ISO.DATE)
private LocalDate date;

@JsonProperty("dateTime")
@org.springframework.format.annotation.DateTimeFormat(iso = org.springframework.format.annotation.DateTimeFormat.ISO.DATE_TIME)
private OffsetDateTime dateTime;

@JsonProperty("uuid")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class MixedPropertiesAndAdditionalPropertiesClass {
private UUID uuid;

@JsonProperty("dateTime")
@org.springframework.format.annotation.DateTimeFormat(iso = org.springframework.format.annotation.DateTimeFormat.ISO.DATE_TIME)
private OffsetDateTime dateTime;

@JsonProperty("map")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class Order {
private Integer quantity;

@JsonProperty("shipDate")
@org.springframework.format.annotation.DateTimeFormat(iso = org.springframework.format.annotation.DateTimeFormat.ISO.DATE_TIME)
private OffsetDateTime shipDate;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,11 @@ public class FormatTest {
private Resource binary;

@JsonProperty("date")
@org.springframework.format.annotation.DateTimeFormat(iso = org.springframework.format.annotation.DateTimeFormat.ISO.DATE)
private LocalDate date;

@JsonProperty("dateTime")
@org.springframework.format.annotation.DateTimeFormat(iso = org.springframework.format.annotation.DateTimeFormat.ISO.DATE_TIME)
private OffsetDateTime dateTime;

@JsonProperty("uuid")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class MixedPropertiesAndAdditionalPropertiesClass {
private UUID uuid;

@JsonProperty("dateTime")
@org.springframework.format.annotation.DateTimeFormat(iso = org.springframework.format.annotation.DateTimeFormat.ISO.DATE_TIME)
private OffsetDateTime dateTime;

@JsonProperty("map")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class Order {
private Integer quantity;

@JsonProperty("shipDate")
@org.springframework.format.annotation.DateTimeFormat(iso = org.springframework.format.annotation.DateTimeFormat.ISO.DATE_TIME)
private OffsetDateTime shipDate;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,11 @@ public class FormatTest {
private Resource binary;

@JsonProperty("date")
@org.springframework.format.annotation.DateTimeFormat(iso = org.springframework.format.annotation.DateTimeFormat.ISO.DATE)
private LocalDate date;

@JsonProperty("dateTime")
@org.springframework.format.annotation.DateTimeFormat(iso = org.springframework.format.annotation.DateTimeFormat.ISO.DATE_TIME)
private OffsetDateTime dateTime;

@JsonProperty("uuid")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class MixedPropertiesAndAdditionalPropertiesClass {
private UUID uuid;

@JsonProperty("dateTime")
@org.springframework.format.annotation.DateTimeFormat(iso = org.springframework.format.annotation.DateTimeFormat.ISO.DATE_TIME)
private OffsetDateTime dateTime;

@JsonProperty("map")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class Order {
private Integer quantity;

@JsonProperty("shipDate")
@org.springframework.format.annotation.DateTimeFormat(iso = org.springframework.format.annotation.DateTimeFormat.ISO.DATE_TIME)
private OffsetDateTime shipDate;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,11 @@ public class FormatTest {
private Resource binary;

@JsonProperty("date")
@org.springframework.format.annotation.DateTimeFormat(iso = org.springframework.format.annotation.DateTimeFormat.ISO.DATE)
private LocalDate date;

@JsonProperty("dateTime")
@org.springframework.format.annotation.DateTimeFormat(iso = org.springframework.format.annotation.DateTimeFormat.ISO.DATE_TIME)
private OffsetDateTime dateTime;

@JsonProperty("uuid")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class MixedPropertiesAndAdditionalPropertiesClass {
private UUID uuid;

@JsonProperty("dateTime")
@org.springframework.format.annotation.DateTimeFormat(iso = org.springframework.format.annotation.DateTimeFormat.ISO.DATE_TIME)
private OffsetDateTime dateTime;

@JsonProperty("map")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class Order {
private Integer quantity;

@JsonProperty("shipDate")
@org.springframework.format.annotation.DateTimeFormat(iso = org.springframework.format.annotation.DateTimeFormat.ISO.DATE_TIME)
private OffsetDateTime shipDate;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,11 @@ public class FormatTest {
private Resource binary;

@JsonProperty("date")
@org.springframework.format.annotation.DateTimeFormat(iso = org.springframework.format.annotation.DateTimeFormat.ISO.DATE)
private LocalDate date;

@JsonProperty("dateTime")
@org.springframework.format.annotation.DateTimeFormat(iso = org.springframework.format.annotation.DateTimeFormat.ISO.DATE_TIME)
private OffsetDateTime dateTime;

@JsonProperty("uuid")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class MixedPropertiesAndAdditionalPropertiesClass {
private UUID uuid;

@JsonProperty("dateTime")
@org.springframework.format.annotation.DateTimeFormat(iso = org.springframework.format.annotation.DateTimeFormat.ISO.DATE_TIME)
private OffsetDateTime dateTime;

@JsonProperty("map")
Expand Down
Loading

0 comments on commit 6a158de

Please sign in to comment.