Skip to content

Commit 7b59bdd

Browse files
authored
feat: support date-time-local in scala generators (#22990)
1 parent e5d40f4 commit 7b59bdd

4 files changed

Lines changed: 104 additions & 0 deletions

File tree

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@
4747
public abstract class AbstractScalaCodegen extends DefaultCodegen {
4848
private final Logger LOGGER = LoggerFactory.getLogger(AbstractScalaCodegen.class);
4949

50+
// https://spec.openapis.org/registry/format/date-time-local.html
51+
protected static final String DATE_TIME_LOCAL_FORMAT = "date-time-local";
52+
5053
@Getter protected String modelPropertyNaming = CodegenConstants.ENUM_PROPERTY_NAMING_TYPE.camelCase.name();
5154
@Setter protected String invokerPackage = "org.openapitools.client";
5255
@Getter @Setter
@@ -137,6 +140,7 @@ public AbstractScalaCodegen() {
137140

138141
// Scala specific openApi types mapping
139142
typeMapping.put("ByteArray", "Array[Byte]");
143+
typeMapping.put(DATE_TIME_LOCAL_FORMAT, "LocalDateTime");
140144

141145

142146
importMapping = new HashMap<String, String>();
@@ -226,8 +230,10 @@ public void processOpts() {
226230
if (additionalProperties.containsKey(DATE_LIBRARY)) {
227231
this.setDateLibrary(additionalProperties.get(DATE_LIBRARY).toString(), false);
228232
}
233+
this.typeMapping.put(DATE_TIME_LOCAL_FORMAT, "LocalDateTime");
229234
if (DateLibraries.java8.name().equals(dateLibrary)) {
230235
this.importMapping.put("LocalDate", "java.time.LocalDate");
236+
this.importMapping.put("LocalDateTime", "java.time.LocalDateTime");
231237
this.importMapping.put("OffsetDateTime", "java.time.OffsetDateTime");
232238
this.typeMapping.put("date", "LocalDate");
233239
this.typeMapping.put("DateTime", "OffsetDateTime");

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,27 @@ public void checkScalaTypeImportMapping() {
100100
"BigInt is a Scala type and must not be imported");
101101
}
102102

103+
@Test
104+
void checkDateTimeLocalTypeMappingJava8() {
105+
P_AbstractScalaCodegen codegen = new P_AbstractScalaCodegen();
106+
codegen.processOpts();
107+
Assert.assertEquals(codegen.typeMapping().get("date-time-local"), "LocalDateTime",
108+
"date-time-local format should map to LocalDateTime");
109+
Assert.assertEquals(codegen.importMapping().get("LocalDateTime"), "java.time.LocalDateTime",
110+
"LocalDateTime should import from java.time when using java8 date library");
111+
}
112+
113+
@Test
114+
void checkDateTimeLocalTypeDeclaration() {
115+
P_AbstractScalaCodegen codegen = new P_AbstractScalaCodegen();
116+
codegen.processOpts();
117+
Schema<?> schema = new ObjectSchema();
118+
schema.setType("string");
119+
schema.setFormat("date-time-local");
120+
Assert.assertEquals(codegen.getTypeDeclaration(schema), "LocalDateTime",
121+
"Schema with date-time-local format should produce LocalDateTime type");
122+
}
123+
103124
@Test
104125
void checkTypeDeclarationWithByteString() {
105126
Schema<?> byteArraySchema = new ObjectSchema();

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,40 @@ public void verifyApiKeyLocations() throws IOException {
5353
assertFileContains(path, ".header(\"X-Api-Key\", apiKeyHeader)");
5454
assertFileContains(path, ".cookie(\"apikey\", apiKeyCookie)");
5555
}
56+
57+
@Test
58+
public void verifyDateTimeLocalGeneratesLocalDateTime() throws IOException {
59+
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
60+
output.deleteOnExit();
61+
String outputPath = output.getAbsolutePath().replace('\\', '/');
62+
63+
OpenAPI openAPI = new OpenAPIParser()
64+
.readLocation("src/test/resources/3_0/scala/date-time-local.yaml", null, new ParseOptions()).getOpenAPI();
65+
66+
ScalaSttp4ClientCodegen codegen = new ScalaSttp4ClientCodegen();
67+
codegen.setOutputDir(output.getAbsolutePath());
68+
69+
ClientOptInput input = new ClientOptInput();
70+
input.openAPI(openAPI);
71+
input.config(codegen);
72+
73+
DefaultGenerator generator = new DefaultGenerator();
74+
75+
generator.setGeneratorPropertyDefault(CodegenConstants.MODELS, "true");
76+
generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_TESTS, "false");
77+
generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_DOCS, "false");
78+
generator.setGeneratorPropertyDefault(CodegenConstants.APIS, "false");
79+
generator.setGeneratorPropertyDefault(CodegenConstants.SUPPORTING_FILES, "false");
80+
generator.opts(input).generate();
81+
82+
Path modelPath = Paths.get(outputPath + "/src/main/scala/org/openapitools/client/model/Event.scala");
83+
// date-time-local maps to LocalDateTime
84+
assertFileContains(modelPath, "import java.time.LocalDateTime");
85+
assertFileContains(modelPath, "startTime: LocalDateTime");
86+
assertFileContains(modelPath, "endTime: Option[LocalDateTime]");
87+
// date-time maps to OffsetDateTime
88+
assertFileContains(modelPath, "import java.time.OffsetDateTime");
89+
assertFileContains(modelPath, "createdAt: OffsetDateTime");
90+
assertFileContains(modelPath, "updatedAt: Option[OffsetDateTime]");
91+
}
5692
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
openapi: 3.0.3
2+
info:
3+
title: Date Time Local Test API
4+
version: 1.0.0
5+
paths:
6+
/events:
7+
get:
8+
operationId: getEvents
9+
summary: Get events
10+
responses:
11+
'200':
12+
description: Successful response
13+
content:
14+
application/json:
15+
schema:
16+
type: array
17+
items:
18+
$ref: '#/components/schemas/Event'
19+
components:
20+
schemas:
21+
Event:
22+
type: object
23+
required:
24+
- name
25+
- startTime
26+
- createdAt
27+
properties:
28+
name:
29+
type: string
30+
startTime:
31+
type: string
32+
format: date-time-local
33+
endTime:
34+
type: string
35+
format: date-time-local
36+
createdAt:
37+
type: string
38+
format: date-time
39+
updatedAt:
40+
type: string
41+
format: date-time

0 commit comments

Comments
 (0)