forked from loafle/openapi-generator-original
Allow array items in kotlin to be nullable (#19080)
This commit is contained in:
parent
c8caa7cf49
commit
b897a99ebb
@ -347,6 +347,12 @@ public abstract class AbstractKotlinCodegen extends DefaultCodegen implements Co
|
|||||||
return toModelName(type);
|
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
|
* 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;
|
Schema<?> target = ModelUtils.isGenerateAliasAsModel() ? p : schema;
|
||||||
if (ModelUtils.isArraySchema(target)) {
|
if (ModelUtils.isArraySchema(target)) {
|
||||||
Schema<?> items = ModelUtils.getSchemaItems( schema);
|
Schema<?> items = ModelUtils.getSchemaItems( schema);
|
||||||
return getSchemaType(target) + "<" + getTypeDeclaration(items) + ">";
|
return getSchemaType(target) + "<" + getItemsTypeDeclaration(items) + ">";
|
||||||
} else if (ModelUtils.isMapSchema(target)) {
|
} else if (ModelUtils.isMapSchema(target)) {
|
||||||
// Note: ModelUtils.isMapSchema(p) returns true when p is a composed schema that also defines
|
// Note: ModelUtils.isMapSchema(p) returns true when p is a composed schema that also defines
|
||||||
// additionalproperties: true
|
// additionalproperties: true
|
||||||
|
@ -362,6 +362,37 @@ public class KotlinSpringServerCodegenTest {
|
|||||||
"ApiUtil");
|
"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
|
@Test
|
||||||
public void doNotGenerateRequestParamForObjectQueryParam() throws IOException {
|
public void doNotGenerateRequestParamForObjectQueryParam() throws IOException {
|
||||||
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
|
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
|
||||||
|
@ -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
|
Loading…
x
Reference in New Issue
Block a user