|
19 | 19 |
|
20 | 20 | import io.swagger.v3.oas.models.OpenAPI;
|
21 | 21 | import io.swagger.v3.oas.models.media.*;
|
22 |
| -import lombok.Getter; |
23 |
| -import org.antlr.v4.runtime.*; |
| 22 | +import org.antlr.v4.runtime.CharStreams; |
| 23 | +import org.antlr.v4.runtime.CommonTokenStream; |
24 | 24 | import org.antlr.v4.runtime.tree.ParseTree;
|
25 | 25 | import org.antlr.v4.runtime.tree.ParseTreeWalker;
|
26 | 26 | import org.openapitools.codegen.*;
|
27 | 27 | import org.openapitools.codegen.antlr4.KotlinLexer;
|
28 | 28 | import org.openapitools.codegen.antlr4.KotlinParser;
|
29 |
| -import org.openapitools.codegen.antlr4.KotlinParserBaseListener; |
30 | 29 | import org.openapitools.codegen.config.CodegenConfigurator;
|
31 | 30 | import org.openapitools.codegen.languages.KotlinClientCodegen;
|
32 |
| -import org.openapitools.codegen.languages.KotlinServerCodegen; |
33 | 31 | import org.openapitools.codegen.testutils.ConfigAssert;
|
34 | 32 | import org.testng.Assert;
|
35 | 33 | import org.testng.annotations.DataProvider;
|
|
45 | 43 | import java.util.Map;
|
46 | 44 |
|
47 | 45 | import static org.openapitools.codegen.CodegenConstants.*;
|
48 |
| -import static org.openapitools.codegen.languages.KotlinServerCodegen.Constants.INTERFACE_ONLY; |
49 |
| -import static org.openapitools.codegen.languages.KotlinServerCodegen.Constants.RETURN_RESPONSE; |
50 | 46 |
|
51 | 47 | @SuppressWarnings("static-method")
|
52 | 48 | public class KotlinClientCodegenModelTest {
|
@@ -534,7 +530,7 @@ private void givenSchemaObjectPropertyNameContainsDollarSignWhenGenerateThenDoll
|
534 | 530 | }
|
535 | 531 |
|
536 | 532 | @Test(description = "generate polymorphic kotlinx_serialization model")
|
537 |
| - public void polymorphicKotlinxSerialzation() throws IOException { |
| 533 | + public void polymorphicKotlinxSerialization() throws IOException { |
538 | 534 | File output = Files.createTempDirectory("test").toFile();
|
539 | 535 | output.deleteOnExit();
|
540 | 536 |
|
@@ -573,6 +569,54 @@ public void polymorphicKotlinxSerialzation() throws IOException {
|
573 | 569 | TestUtils.assertFileContains(birdKt, "@SerialName(value = \"BIRD\")");
|
574 | 570 | }
|
575 | 571 |
|
| 572 | + @Test(description = "generate polymorphic jackson model") |
| 573 | + public void polymorphicJacksonSerialization() throws IOException { |
| 574 | + File output = Files.createTempDirectory("test").toFile(); |
| 575 | +// output.deleteOnExit(); |
| 576 | + |
| 577 | + final CodegenConfigurator configurator = new CodegenConfigurator() |
| 578 | + .setGeneratorName("kotlin") |
| 579 | + .setLibrary("jvm-okhttp4") |
| 580 | + .setAdditionalProperties(new HashMap<>() {{ |
| 581 | + put(CodegenConstants.SERIALIZATION_LIBRARY, "jackson"); |
| 582 | + put(CodegenConstants.MODEL_PACKAGE, "xyz.abcdef.model"); |
| 583 | + }}) |
| 584 | + .setInputSpec("src/test/resources/3_0/kotlin/polymorphism.yaml") |
| 585 | + .setOutputDir(output.getAbsolutePath().replace("\\", "/")); |
| 586 | + |
| 587 | + final ClientOptInput clientOptInput = configurator.toClientOptInput(); |
| 588 | + DefaultGenerator generator = new DefaultGenerator(); |
| 589 | + List<File> files = generator.opts(clientOptInput).generate(); |
| 590 | + |
| 591 | + Assert.assertEquals(files.size(), 28); |
| 592 | + |
| 593 | + final Path animalKt = Paths.get(output + "/src/main/kotlin/xyz/abcdef/model/Animal.kt"); |
| 594 | + // base has extra jackson imports |
| 595 | + TestUtils.assertFileContains(animalKt, "import com.fasterxml.jackson.annotation.JsonIgnoreProperties"); |
| 596 | + TestUtils.assertFileContains(animalKt, "import com.fasterxml.jackson.annotation.JsonSubTypes"); |
| 597 | + TestUtils.assertFileContains(animalKt, "import com.fasterxml.jackson.annotation.JsonTypeInfo"); |
| 598 | + // and these are being used |
| 599 | + TestUtils.assertFileContains(animalKt, "@JsonIgnoreProperties"); |
| 600 | + TestUtils.assertFileContains(animalKt, "@JsonSubTypes"); |
| 601 | + TestUtils.assertFileContains(animalKt, "@JsonTypeInfo"); |
| 602 | + // base is interface |
| 603 | + TestUtils.assertFileContains(animalKt, "interface Animal"); |
| 604 | + // base properties are present |
| 605 | + TestUtils.assertFileContains(animalKt, "val id"); |
| 606 | + TestUtils.assertFileContains(animalKt, "val optionalProperty"); |
| 607 | + // base doesn't contain discriminator |
| 608 | + TestUtils.assertFileNotContains(animalKt, "val discriminator"); |
| 609 | + |
| 610 | + final Path birdKt = Paths.get(output + "/src/main/kotlin/xyz/abcdef/model/Bird.kt"); |
| 611 | + // derived has serial name set to mapping key |
| 612 | + TestUtils.assertFileContains(birdKt, "data class Bird"); |
| 613 | + // derived properties are overridden |
| 614 | + TestUtils.assertFileContains(birdKt, "override val id"); |
| 615 | + TestUtils.assertFileContains(birdKt, "override val optionalProperty"); |
| 616 | + // derived doesn't contain disciminator |
| 617 | + TestUtils.assertFileNotContains(birdKt, "val discriminator"); |
| 618 | + } |
| 619 | + |
576 | 620 | private static class ModelNameTest {
|
577 | 621 | private final String expectedName;
|
578 | 622 | private final String expectedClassName;
|
|
0 commit comments