Allow array items in kotlin to be nullable (#19080)

This commit is contained in:
Not So Chiken 2024-07-06 09:29:49 +01:00 committed by GitHub
parent c8caa7cf49
commit b897a99ebb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 64 additions and 1 deletions

View File

@ -347,6 +347,12 @@ public abstract class AbstractKotlinCodegen extends DefaultCodegen implements Co
return toModelName(type);
}
private String getItemsTypeDeclaration(Schema items) {
String itemsTypeDeclaration = getTypeDeclaration(items);
String nullable = items.getNullable() != null && items.getNullable() && !itemsTypeDeclaration.endsWith("?") ? "?" : "";
return itemsTypeDeclaration + nullable;
}
/**
* Output the type declaration of the property
*
@ -359,7 +365,7 @@ public abstract class AbstractKotlinCodegen extends DefaultCodegen implements Co
Schema<?> target = ModelUtils.isGenerateAliasAsModel() ? p : schema;
if (ModelUtils.isArraySchema(target)) {
Schema<?> items = ModelUtils.getSchemaItems( schema);
return getSchemaType(target) + "<" + getTypeDeclaration(items) + ">";
return getSchemaType(target) + "<" + getItemsTypeDeclaration(items) + ">";
} else if (ModelUtils.isMapSchema(target)) {
// Note: ModelUtils.isMapSchema(p) returns true when p is a composed schema that also defines
// additionalproperties: true

View File

@ -362,6 +362,37 @@ public class KotlinSpringServerCodegenTest {
"ApiUtil");
}
@Test
public void arrayItemsCanBeNullable() 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/array-nullable-items.yaml", null, new ParseOptions()).getOpenAPI();
KotlinSpringServerCodegen codegen = new KotlinSpringServerCodegen();
codegen.setOutputDir(output.getAbsolutePath());
codegen.additionalProperties().put(CXFServerFeatures.LOAD_TEST_DATA_FROM_FILE, "true");
ClientOptInput input = new ClientOptInput();
input.openAPI(openAPI);
input.config(codegen);
DefaultGenerator generator = new DefaultGenerator();
generator.setGeneratorPropertyDefault(CodegenConstants.MODELS, "true");
generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_TESTS, "false");
generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_DOCS, "false");
generator.setGeneratorPropertyDefault(CodegenConstants.APIS, "true");
generator.setGeneratorPropertyDefault(CodegenConstants.SUPPORTING_FILES, "false");
generator.opts(input).generate();
assertFileContains(Paths.get(outputPath + "/src/main/kotlin/org/openapitools/model/ArrayWithNullableItemsModel.kt"), "List<kotlin.String?>");
}
@Test
public void doNotGenerateRequestParamForObjectQueryParam() throws IOException {
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();

View File

@ -0,0 +1,26 @@
openapi: 3.0.0
info:
title: 'Array nullable items'
version: latest
paths:
'/':
get:
operationId: operation
responses:
'200':
description: Success
content:
application/json:
schema:
$ref: '#/components/schemas/ArrayWithNullableItemsModel'
components:
schemas:
ArrayWithNullableItemsModel:
required:
- foo
properties:
foo:
type: array
items:
type: string
nullable: true