From d8e1e9409ecde91f23ff6ff7db36d6d5ca3c3554 Mon Sep 17 00:00:00 2001 From: Nikhil Chilwant Date: Thu, 3 Oct 2024 13:14:27 +0200 Subject: [PATCH] Fix #11899: Add ignoreUnknownJacksonAnnotation configuration property Add unit tests for ignoreUnknownJacksonAnnotation configuration property --- .../v3/service/GeneratorServiceTest.java | 115 ++++++++++++++++++ pom.xml | 2 +- 2 files changed, 116 insertions(+), 1 deletion(-) diff --git a/modules/swagger-codegen/src/test/java/io/swagger/codegen/v3/service/GeneratorServiceTest.java b/modules/swagger-codegen/src/test/java/io/swagger/codegen/v3/service/GeneratorServiceTest.java index 33d611e3444..03b427402d0 100644 --- a/modules/swagger-codegen/src/test/java/io/swagger/codegen/v3/service/GeneratorServiceTest.java +++ b/modules/swagger-codegen/src/test/java/io/swagger/codegen/v3/service/GeneratorServiceTest.java @@ -1,6 +1,9 @@ package io.swagger.codegen.v3.service; import com.fasterxml.jackson.databind.JsonNode; +import io.swagger.codegen.CodegenConstants; +import io.swagger.codegen.DefaultGenerator; +import io.swagger.codegen.config.CodegenConfigurator; import io.swagger.util.Json; import io.swagger.util.Yaml; import io.swagger.v3.oas.models.OpenAPI; @@ -15,7 +18,12 @@ import java.io.IOException; import java.io.InputStream; import java.nio.file.Files; +import java.util.HashMap; import java.util.List; +import java.util.Map; + +import static org.testng.Assert.assertFalse; +import static org.testng.Assert.assertTrue; public class GeneratorServiceTest { @@ -210,6 +218,32 @@ public void testGeneratorService_notNullJacksonAnnotationJava_True() throws IOEx } } + @Test + public void testIgnoreUnknownJacksonAnnotationJava_True() throws IOException { + String path = getTmpFolder().getAbsolutePath(); + GenerationRequest request = new GenerationRequest(); + request + .codegenVersion(GenerationRequest.CodegenVersion.V3) + .type(GenerationRequest.Type.CLIENT) + .lang("java") + .spec(loadSpecAsNode("3_0_0/issue-9203.yaml", true, false)) + .options( + new Options() + .outputDir(path) + .addAdditionalProperty("ignoreUnknownJacksonAnnotation", true) + ); + + List files = new GeneratorService().generationRequest(request).generate(); + Assert.assertFalse(files.isEmpty()); + for (File f: files) { + String relPath = f.getAbsolutePath().substring(path.length()); + if ("/src/main/java/io/swagger/client/model/OrderLineAudit.java".equals(relPath)) { + Assert.assertTrue(FileUtils.readFileToString(f).contains("@JsonIgnoreProperties(ignoreUnknown = true)")); + Assert.assertTrue(FileUtils.readFileToString(f).contains("import com.fasterxml.jackson.annotation.JsonIgnoreProperties;")); + } + } + } + @Test(description = "test generator service with java enum parameters in type") public void testGeneratorService_WithEnumParametersType() throws IOException { @@ -264,6 +298,33 @@ public void testGeneratorService_notNullJacksonAnnotationJava_False() throws IOE } } + @Test(description = "test generator service with java") + public void testGeneratorService_ignoreUnknownJacksonAnnotationJava_False() throws IOException { + + String path = getTmpFolder().getAbsolutePath(); + GenerationRequest request = new GenerationRequest(); + request + .codegenVersion(GenerationRequest.CodegenVersion.V3) + .type(GenerationRequest.Type.CLIENT) + .lang("java") + .spec(loadSpecAsNode("3_0_0/issue-9203.yaml", true, false)) + .options( + new Options() + .outputDir(path) + .addAdditionalProperty("ignoreUnknownJacksonAnnotation", false) + ); + + List files = new GeneratorService().generationRequest(request).generate(); + Assert.assertFalse(files.isEmpty()); + for (File f: files) { + String relPath = f.getAbsolutePath().substring(path.length()); + if ("/src/main/java/io/swagger/client/model/OrderLineAudit.java".equals(relPath)) { + Assert.assertFalse(FileUtils.readFileToString(f).contains("@JsonIgnoreProperties(ignoreUnknown = true)")); + Assert.assertFalse(FileUtils.readFileToString(f).contains("import com.fasterxml.jackson.annotation.JsonIgnoreProperties")); + } + } + } + @Test(description = "test generator service with spring") public void testGeneratorService_notNullJacksonAnnotationSpring_True() throws IOException { @@ -292,6 +353,33 @@ public void testGeneratorService_notNullJacksonAnnotationSpring_True() throws IO } } + @Test(description = "test generator service with spring") + public void testGeneratorService_ignoreUnknownJacksonAnnotationSpring_True() throws IOException { + + String path = getTmpFolder().getAbsolutePath(); + GenerationRequest request = new GenerationRequest(); + request + .codegenVersion(GenerationRequest.CodegenVersion.V3) + .type(GenerationRequest.Type.CLIENT) + .lang("spring") + .spec(loadSpecAsNode("3_0_0/issue-9203.yaml", true, false)) + .options( + new Options() + .outputDir(path) + .addAdditionalProperty("ignoreUnknownJacksonAnnotation", true) + ); + + List files = new GeneratorService().generationRequest(request).generate(); + Assert.assertFalse(files.isEmpty()); + for (File f: files) { + String relPath = f.getAbsolutePath().substring(path.length()); + if ("/src/main/java/io/swagger/client/model/OrderLineAudit.java".equals(relPath)) { + Assert.assertTrue(FileUtils.readFileToString(f).contains("@JsonIgnoreProperties(ignoreUnknown = true)")); + Assert.assertTrue(FileUtils.readFileToString(f).contains("import com.fasterxml.jackson.annotation.JsonIgnoreProperties")); + } + } + } + @Test(description = "test generator service with spring") public void testGeneratorService_notNullJacksonAnnotationSpring_False() throws IOException { @@ -319,6 +407,33 @@ public void testGeneratorService_notNullJacksonAnnotationSpring_False() throws I } } + @Test(description = "test generator service with spring") + public void testGeneratorService_ignoreUnknownJacksonAnnotationSpring_False() throws IOException { + + String path = getTmpFolder().getAbsolutePath(); + GenerationRequest request = new GenerationRequest(); + request + .codegenVersion(GenerationRequest.CodegenVersion.V3) + .type(GenerationRequest.Type.CLIENT) + .lang("spring") + .spec(loadSpecAsNode("3_0_0/issue-9203.yaml", true, false)) + .options( + new Options() + .outputDir(path) + .addAdditionalProperty("ignoreUnknownJacksonAnnotation", false) + ); + + List files = new GeneratorService().generationRequest(request).generate(); + Assert.assertFalse(files.isEmpty()); + for (File f: files) { + String relPath = f.getAbsolutePath().substring(path.length()); + if ("/src/main/java/io/swagger/client/model/OrderLineAudit.java".equals(relPath)) { + Assert.assertFalse(FileUtils.readFileToString(f).contains("@JsonIgnoreProperties(ignoreUnknown = true)")); + Assert.assertFalse(FileUtils.readFileToString(f).contains("import com.fasterxml.jackson.annotation.JsonIgnoreProperties")); + } + } + } + @Test(description = "test generator oneOf ComposedSchema Properties") public void testGenerator_FlattenInlineComposedSchema() throws IOException { diff --git a/pom.xml b/pom.xml index 186c3c76e0b..081021c02d1 100644 --- a/pom.xml +++ b/pom.xml @@ -1201,7 +1201,7 @@ 8 - 1.0.52 + 1.0.53-SNAPSHOT 2.2.22 1.6.14 2.1.22