Skip to content

Commit b897a99

Browse files
authored
Allow array items in kotlin to be nullable (#19080)
1 parent c8caa7c commit b897a99

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-1
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,12 @@ public String getSchemaType(Schema p) {
347347
return toModelName(type);
348348
}
349349

350+
private String getItemsTypeDeclaration(Schema items) {
351+
String itemsTypeDeclaration = getTypeDeclaration(items);
352+
String nullable = items.getNullable() != null && items.getNullable() && !itemsTypeDeclaration.endsWith("?") ? "?" : "";
353+
return itemsTypeDeclaration + nullable;
354+
}
355+
350356
/**
351357
* Output the type declaration of the property
352358
*
@@ -359,7 +365,7 @@ public String getTypeDeclaration(Schema p) {
359365
Schema<?> target = ModelUtils.isGenerateAliasAsModel() ? p : schema;
360366
if (ModelUtils.isArraySchema(target)) {
361367
Schema<?> items = ModelUtils.getSchemaItems( schema);
362-
return getSchemaType(target) + "<" + getTypeDeclaration(items) + ">";
368+
return getSchemaType(target) + "<" + getItemsTypeDeclaration(items) + ">";
363369
} else if (ModelUtils.isMapSchema(target)) {
364370
// Note: ModelUtils.isMapSchema(p) returns true when p is a composed schema that also defines
365371
// additionalproperties: true

modules/openapi-generator/src/test/java/org/openapitools/codegen/kotlin/spring/KotlinSpringServerCodegenTest.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,37 @@ public void delegateReactiveWithTags() throws Exception {
362362
"ApiUtil");
363363
}
364364

365+
@Test
366+
public void arrayItemsCanBeNullable() throws IOException {
367+
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
368+
output.deleteOnExit();
369+
String outputPath = output.getAbsolutePath().replace('\\', '/');
370+
371+
OpenAPI openAPI = new OpenAPIParser()
372+
.readLocation("src/test/resources/3_0/array-nullable-items.yaml", null, new ParseOptions()).getOpenAPI();
373+
374+
KotlinSpringServerCodegen codegen = new KotlinSpringServerCodegen();
375+
codegen.setOutputDir(output.getAbsolutePath());
376+
codegen.additionalProperties().put(CXFServerFeatures.LOAD_TEST_DATA_FROM_FILE, "true");
377+
378+
ClientOptInput input = new ClientOptInput();
379+
input.openAPI(openAPI);
380+
input.config(codegen);
381+
382+
DefaultGenerator generator = new DefaultGenerator();
383+
384+
generator.setGeneratorPropertyDefault(CodegenConstants.MODELS, "true");
385+
generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_TESTS, "false");
386+
generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_DOCS, "false");
387+
generator.setGeneratorPropertyDefault(CodegenConstants.APIS, "true");
388+
generator.setGeneratorPropertyDefault(CodegenConstants.SUPPORTING_FILES, "false");
389+
390+
generator.opts(input).generate();
391+
392+
assertFileContains(Paths.get(outputPath + "/src/main/kotlin/org/openapitools/model/ArrayWithNullableItemsModel.kt"), "List<kotlin.String?>");
393+
}
394+
395+
365396
@Test
366397
public void doNotGenerateRequestParamForObjectQueryParam() throws IOException {
367398
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
openapi: 3.0.0
2+
info:
3+
title: 'Array nullable items'
4+
version: latest
5+
paths:
6+
'/':
7+
get:
8+
operationId: operation
9+
responses:
10+
'200':
11+
description: Success
12+
content:
13+
application/json:
14+
schema:
15+
$ref: '#/components/schemas/ArrayWithNullableItemsModel'
16+
components:
17+
schemas:
18+
ArrayWithNullableItemsModel:
19+
required:
20+
- foo
21+
properties:
22+
foo:
23+
type: array
24+
items:
25+
type: string
26+
nullable: true

0 commit comments

Comments
 (0)