Skip to content

Commit d8b4e83

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

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed

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

Lines changed: 2 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");

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 generateLocalDateTimeForLocalDateFormat() 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-local.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/EventParams.java"))
409+
.hasImports("java.time.LocalDateTime")
410+
.assertProperty("startAt").withType("LocalDateTime");
411+
}
412+
384413
@Test
385414
public void interfaceDefaultImplDisableWithResponseWrapper() {
386415
final SpringCodegen codegen = new SpringCodegen();
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
openapi: 3.0.0
2+
info:
3+
version: 1.0.0
4+
title: Local Date Format Test
5+
paths:
6+
/events:
7+
get:
8+
operationId: getEvents
9+
parameters:
10+
- in: query
11+
name: eventParams
12+
style: form
13+
explode: true
14+
schema:
15+
$ref: '#/components/schemas/EventParams'
16+
responses:
17+
'200':
18+
description: OK
19+
components:
20+
schemas:
21+
EventParams:
22+
type: object
23+
properties:
24+
startAt:
25+
type: string
26+
format: date-time-local
27+
example: '2024-01-15T10:30:00'
28+
name:
29+
type: string
30+
example: 'My Event'

0 commit comments

Comments
 (0)