|
4 | 4 | import io.swagger.parser.OpenAPIParser; |
5 | 5 | import io.swagger.v3.oas.models.OpenAPI; |
6 | 6 | import io.swagger.v3.oas.models.info.Info; |
| 7 | +import io.swagger.v3.oas.models.media.Schema; |
7 | 8 | import io.swagger.v3.oas.models.servers.Server; |
8 | 9 | import io.swagger.v3.oas.models.Operation; |
9 | 10 | import io.swagger.v3.parser.core.models.ParseOptions; |
|
12 | 13 | import org.jetbrains.annotations.NotNull; |
13 | 14 | import org.openapitools.codegen.ClientOptInput; |
14 | 15 | import org.openapitools.codegen.CodegenConstants; |
| 16 | +import org.openapitools.codegen.CodegenModel; |
| 17 | +import org.openapitools.codegen.CodegenProperty; |
15 | 18 | import org.openapitools.codegen.DefaultGenerator; |
16 | 19 | import org.openapitools.codegen.TestUtils; |
17 | 20 | import org.openapitools.codegen.config.CodegenConfigurator; |
|
25 | 28 | import org.openapitools.codegen.languages.features.DocumentationProviderFeatures.AnnotationLibrary; |
26 | 29 | import org.openapitools.codegen.languages.features.DocumentationProviderFeatures.DocumentationProvider; |
27 | 30 | import org.openapitools.codegen.languages.features.SwaggerUIFeatures; |
| 31 | +import org.openapitools.codegen.model.ModelsMap; |
| 32 | +import org.openapitools.codegen.model.ModelMap; |
28 | 33 | import org.testng.Assert; |
29 | 34 | import org.testng.annotations.DataProvider; |
30 | 35 | import org.testng.annotations.Test; |
@@ -7715,6 +7720,100 @@ public void schemaMappingWithNullableAllOfRendersNullableKotlinProperty() throws |
7715 | 7720 | assertThat(content).contains("com.example.ExternalModel?"); |
7716 | 7721 | } |
7717 | 7722 |
|
| 7723 | + @Test(description = "test enumUnknownDefaultCase option") |
| 7724 | + public void testEnumUnknownDefaultCaseOption() { |
| 7725 | + final KotlinSpringServerCodegen codegen = new KotlinSpringServerCodegen(); |
| 7726 | + |
| 7727 | + // Test default value is false |
| 7728 | + codegen.processOpts(); |
| 7729 | + Assert.assertEquals(codegen.getEnumUnknownDefaultCase(), Boolean.FALSE); |
| 7730 | + |
| 7731 | + // Test setting via additionalProperties |
| 7732 | + codegen.additionalProperties().put(CodegenConstants.ENUM_UNKNOWN_DEFAULT_CASE, "true"); |
| 7733 | + codegen.processOpts(); |
| 7734 | + Assert.assertEquals(codegen.getEnumUnknownDefaultCase(), Boolean.TRUE); |
| 7735 | + } |
| 7736 | + |
| 7737 | + @Test(description = "test enum generation with enumUnknownDefaultCase enabled") |
| 7738 | + public void testEnumGenerationWithUnknownDefaultCase() throws IOException { |
| 7739 | + Map<String, File> files = generateFromContract( |
| 7740 | + "src/test/resources/3_1/enum_unknown_default_case.yaml", |
| 7741 | + Map.of( |
| 7742 | + CodegenConstants.ENUM_UNKNOWN_DEFAULT_CASE, true |
| 7743 | + ), |
| 7744 | + new HashMap<>(), |
| 7745 | + configurator -> {} |
| 7746 | + ); |
| 7747 | + |
| 7748 | + File colorFile = files.get("ColorEnum.kt"); |
| 7749 | + assertThat(colorFile).isNotNull(); |
| 7750 | + |
| 7751 | + String content = Files.readString(colorFile.toPath()); |
| 7752 | + |
| 7753 | + assertThat(content).contains("unknown_default_open_api(\"unknown_default_open_api\")"); |
| 7754 | + assertThat(content).contains("?: unknown_default_open_api"); |
| 7755 | + } |
| 7756 | + |
| 7757 | + @Test(description = "test enum generation with enumUnknownDefaultCase disabled") |
| 7758 | + public void testEnumGenerationWithoutUnknownDefaultCase() throws IOException { |
| 7759 | + Map<String, File> files = generateFromContract( |
| 7760 | + "src/test/resources/3_1/enum_unknown_default_case.yaml", |
| 7761 | + Map.of( |
| 7762 | + CodegenConstants.ENUM_UNKNOWN_DEFAULT_CASE, false |
| 7763 | + ), |
| 7764 | + new HashMap<>(), |
| 7765 | + configurator -> {} |
| 7766 | + ); |
| 7767 | + |
| 7768 | + File colorFile = files.get("ColorEnum.kt"); |
| 7769 | + assertThat(colorFile).isNotNull(); |
| 7770 | + |
| 7771 | + String content = Files.readString(colorFile.toPath()); |
| 7772 | + |
| 7773 | + assertThat(content).doesNotContain("unknown_default_open_api(\"unknown_default_open_api\")"); |
| 7774 | + assertThat(content).doesNotContain("?: unknown_default_open_api"); |
| 7775 | + } |
| 7776 | + |
| 7777 | + @Test(description = "test data class generation containing inline enum with enumUnknownDefaultCase enabled") |
| 7778 | + public void testDataClassGenerationWithUnknownDefaultCase() throws IOException { |
| 7779 | + Map<String, File> files = generateFromContract( |
| 7780 | + "src/test/resources/3_1/dataclass_unknown_default_case.yaml", |
| 7781 | + Map.of( |
| 7782 | + CodegenConstants.ENUM_UNKNOWN_DEFAULT_CASE, true |
| 7783 | + ), |
| 7784 | + new HashMap<>(), |
| 7785 | + configurator -> {} |
| 7786 | + ); |
| 7787 | + |
| 7788 | + File colorResponseFile = files.get("ColorResponse.kt"); |
| 7789 | + assertThat(colorResponseFile).isNotNull(); |
| 7790 | + |
| 7791 | + String content = Files.readString(colorResponseFile.toPath()); |
| 7792 | + |
| 7793 | + assertThat(content).contains("unknown_default_open_api(\"unknown_default_open_api\")"); |
| 7794 | + assertThat(content).contains("?: unknown_default_open_api"); |
| 7795 | + } |
| 7796 | + |
| 7797 | + @Test(description = "test data class generation containing inline enum with enumUnknownDefaultCase disabled") |
| 7798 | + public void testDataClassGenerationWithoutUnknownDefaultCase() throws IOException { |
| 7799 | + Map<String, File> files = generateFromContract( |
| 7800 | + "src/test/resources/3_1/dataclass_unknown_default_case.yaml", |
| 7801 | + Map.of( |
| 7802 | + CodegenConstants.ENUM_UNKNOWN_DEFAULT_CASE, false |
| 7803 | + ), |
| 7804 | + new HashMap<>(), |
| 7805 | + configurator -> {} |
| 7806 | + ); |
| 7807 | + |
| 7808 | + File colorResponseFile = files.get("ColorResponse.kt"); |
| 7809 | + assertThat(colorResponseFile).isNotNull(); |
| 7810 | + |
| 7811 | + String content = Files.readString(colorResponseFile.toPath()); |
| 7812 | + |
| 7813 | + assertThat(content).doesNotContain("unknown_default_open_api(\"unknown_default_open_api\")"); |
| 7814 | + assertThat(content).doesNotContain("?: unknown_default_open_api"); |
| 7815 | + } |
| 7816 | + |
7718 | 7817 | // ========== x-jackson-default-impl / typeInfoDefaultImpls tests ========== |
7719 | 7818 |
|
7720 | 7819 | @Test(description = "x-jackson-default-impl on deduction schema emits defaultImpl in @JsonTypeInfo") |
|
0 commit comments