Skip to content

Commit 421b438

Browse files
committed
add java unit test
1 parent 299cc80 commit 421b438

2 files changed

Lines changed: 79 additions & 2 deletions

File tree

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

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,6 +1064,73 @@ public void generateNonSerializableModelWithXimplements() throws Exception {
10641064
);
10651065
}
10661066

1067+
@Test
1068+
public void generateModelWithRequiredFalseWithDefaults() throws Exception {
1069+
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
1070+
output.deleteOnExit();
1071+
String outputPath = output.getAbsolutePath().replace('\\', '/');
1072+
1073+
KotlinSpringServerCodegen codegen = new KotlinSpringServerCodegen();
1074+
codegen.setOutputDir(output.getAbsolutePath());
1075+
codegen.additionalProperties().put(CodegenConstants.SERIALIZABLE_MODEL, true);
1076+
1077+
ClientOptInput input = new ClientOptInput()
1078+
.openAPI(TestUtils.parseSpec("src/test/resources/3_0/kotlin/petstore-with-x-kotlin-implements.yaml"))
1079+
.config(codegen);
1080+
DefaultGenerator generator = new DefaultGenerator();
1081+
1082+
generator.setGeneratorPropertyDefault(CodegenConstants.MODELS, "true");
1083+
generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_TESTS, "false");
1084+
generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_DOCS, "false");
1085+
generator.setGeneratorPropertyDefault(CodegenConstants.APIS, "false");
1086+
generator.setGeneratorPropertyDefault(CodegenConstants.SUPPORTING_FILES, "false");
1087+
1088+
generator.opts(input).generate();
1089+
1090+
Path dogPath = Paths.get(outputPath + "/src/main/kotlin/org/openapitools/model/Dog.kt");
1091+
assertFileContains(
1092+
dogPath,
1093+
"@get:JsonProperty(\"category\", required = false)",
1094+
"override val category: Category? = null", // without default (fallback) value is nullable
1095+
"@get:JsonProperty(\"nonRequiredWithDefaultList\", required = false)",
1096+
"override val nonRequiredWithDefaultList: kotlin.collections.List<kotlin.String> = arrayListOf()", // elsewhere with default (fallback) value is not nullable
1097+
"@get:JsonProperty(\"nonRequiredWithDefaultSet\", required = false)",
1098+
"override val nonRequiredWithDefaultSet: kotlin.collections.Set<kotlin.String> = setOf()",
1099+
"@get:JsonProperty(\"nonRequiredWithDefaultString\", required = false)",
1100+
"override val nonRequiredWithDefaultString: kotlin.String = \"defaultValue\"",
1101+
"@get:JsonProperty(\"nonRequiredWithDefaultInt\", required = false)",
1102+
"override val nonRequiredWithDefaultInt: java.math.BigDecimal = java.math.BigDecimal(\"15\")",
1103+
"@get:JsonProperty(\"nonRequiredWithDefaultLong\", required = false)",
1104+
"override val nonRequiredWithDefaultLong: java.math.BigDecimal = java.math.BigDecimal(\"15\")",
1105+
"@get:JsonProperty(\"nonRequiredWithDefaultFloat\", required = false)",
1106+
"override val nonRequiredWithDefaultFloat: kotlin.Float = 15.45f",
1107+
"@get:JsonProperty(\"nonRequiredWithDefaultDouble\", required = false)",
1108+
"override val nonRequiredWithDefaultDouble: kotlin.Double = 15.45",
1109+
"@get:JsonProperty(\"nonRequiredWithDefaultEnum\", required = false)",
1110+
"override val nonRequiredWithDefaultEnum: Dog.NonRequiredWithDefaultEnum = NonRequiredWithDefaultEnum.THIS",
1111+
"@get:JsonProperty(\"nonRequiredWithDefaultEnumList\", required = false)",
1112+
"override val nonRequiredWithDefaultEnumList: kotlin.collections.List<Dog.NonRequiredWithDefaultEnumList> = arrayListOf(NonRequiredWithDefaultEnumList.THESE,NonRequiredWithDefaultEnumList.THOSE)",
1113+
"@get:JsonProperty(\"nonRequiredWithDefaultEnumSet\", required = false)",
1114+
"override val nonRequiredWithDefaultEnumSet: kotlin.collections.Set<Dog.NonRequiredWithDefaultEnumSet> = setOf(NonRequiredWithDefaultEnumSet.THEM,NonRequiredWithDefaultEnumSet.THOSE)"
1115+
);
1116+
1117+
Path petPath = Paths.get(outputPath + "/src/main/kotlin/org/openapitools/model/Pet.kt");
1118+
assertFileContains(
1119+
petPath,
1120+
"override val category: Category?", // without default (fallback) value is nullable
1121+
"val nonRequiredWithDefaultList: kotlin.collections.List<kotlin.String>", // elsewhere with default (fallback) value is not nullable
1122+
"val nonRequiredWithDefaultSet: kotlin.collections.Set<kotlin.String>",
1123+
"val nonRequiredWithDefaultString: kotlin.String",
1124+
"val nonRequiredWithDefaultInt: java.math.BigDecimal",
1125+
"val nonRequiredWithDefaultLong: java.math.BigDecimal",
1126+
"val nonRequiredWithDefaultFloat: kotlin.Float",
1127+
"val nonRequiredWithDefaultDouble: kotlin.Double",
1128+
"val nonRequiredWithDefaultEnum: Pet.NonRequiredWithDefaultEnum",
1129+
"val nonRequiredWithDefaultEnumList: Pet.NonRequiredWithDefaultEnumList",
1130+
"val nonRequiredWithDefaultEnumSet: Pet.NonRequiredWithDefaultEnumSet"
1131+
);
1132+
}
1133+
10671134
@Test
10681135
public void reactiveWithoutFlow() throws Exception {
10691136
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();

modules/openapi-generator/src/test/resources/3_0/kotlin/petstore-with-x-kotlin-implements.yaml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,16 @@ components:
546546
format: int64
547547
required: false
548548
default: 15
549+
nonRequiredWithDefaultFloat:
550+
type: number
551+
format: float
552+
required: false
553+
default: 15.45
554+
nonRequiredWithDefaultDouble:
555+
type: number
556+
format: double
557+
required: false
558+
default: 15.45
549559
nonRequiredWithDefaultEnum:
550560
type: string
551561
enum:
@@ -562,7 +572,7 @@ components:
562572
- THOSE
563573
- THEM
564574
required: false
565-
default: [THESE, THOSE]
575+
default: [ THESE, THOSE ]
566576
nonRequiredWithDefaultEnumSet:
567577
type: array
568578
uniqueItems: true
@@ -573,7 +583,7 @@ components:
573583
- THOSE
574584
- THEM
575585
required: false
576-
default: [THEM, THOSE]
586+
default: [ THEM, THOSE ]
577587
photoUrls:
578588
type: array
579589
items:

0 commit comments

Comments
 (0)