From 903d34ed7fc88cd389a51b7c567919055507c7dc Mon Sep 17 00:00:00 2001 From: Jiachen Sun <31008528+jiachen1120@users.noreply.github.com> Date: Fri, 14 Dec 2018 10:28:59 -0500 Subject: [PATCH] Add underscores automatically when space are present - closes #182 (#192) * Fixed -Add underscores automatically when space are present #182 * Added test cases to check whether the fields' name contains space #182 --- light-rest-4j/pom.xml | 204 +++++++++--------- .../codegen/rest/OpenApiGenerator.java | 13 ++ .../codegen/rest/SwaggerGenerator.java | 17 +- .../codegen/OpenApiGeneratorTest.java | 58 ++++- .../codegen/SwaggerGeneratorTest.java | 32 ++- 5 files changed, 215 insertions(+), 109 deletions(-) diff --git a/light-rest-4j/pom.xml b/light-rest-4j/pom.xml index 41c53a230..aac709ed0 100644 --- a/light-rest-4j/pom.xml +++ b/light-rest-4j/pom.xml @@ -1,100 +1,104 @@ - - - - 4.0.0 - - - com.networknt - light-codegen - 1.5.24 - .. - - - light-rest-4j-generator - jar - light-rest-4j code generator. - - - - com.networknt - codegen-core - - - com.networknt - utility - - - com.networknt - openapi-parser - - - - com.jsoniter - jsoniter - - - org.javassist - javassist - - - org.slf4j - slf4j-api - - - org.apache.commons - commons-text - - - com.fizzed - rocker-runtime - - - - ch.qos.logback - logback-classic - test - - - junit - junit - test - - - - - - com.fizzed - rocker-maven-plugin - ${version.rocker} - - - generate-rocker-templates - generate-sources - - generate - - - src/main/resources - - - - - - - - + + + + 4.0.0 + + + com.networknt + light-codegen + 1.5.24 + .. + + + light-rest-4j-generator + jar + light-rest-4j code generator. + + + + com.networknt + codegen-core + + + com.networknt + utility + + + com.networknt + openapi-parser + + + + com.jsoniter + jsoniter + + + org.javassist + javassist + + + org.slf4j + slf4j-api + + + org.apache.commons + commons-text + + + com.fizzed + rocker-runtime + + + + ch.qos.logback + logback-classic + test + + + junit + junit + test + + + com.thoughtworks.qdox + qdox + ${qdox.version} + + + + + + com.fizzed + rocker-maven-plugin + ${version.rocker} + + + generate-rocker-templates + generate-sources + + generate + + + src/main/resources + + + + + + + diff --git a/light-rest-4j/src/main/java/com/networknt/codegen/rest/OpenApiGenerator.java b/light-rest-4j/src/main/java/com/networknt/codegen/rest/OpenApiGenerator.java index 6443ae851..21f7cc13c 100644 --- a/light-rest-4j/src/main/java/com/networknt/codegen/rest/OpenApiGenerator.java +++ b/light-rest-4j/src/main/java/com/networknt/codegen/rest/OpenApiGenerator.java @@ -195,6 +195,7 @@ public void generate(String targetPath, Object model, Any config) throws IOExcep //System.out.println("key = " + entryProp.getKey() + " value = " + entryProp.getValue()); Map propMap = new HashMap<>(); String name = entryProp.getKey(); + name = name.trim().replaceAll(" ", "_"); propMap.put("jsonProperty", Any.wrap(name)); if(name.startsWith("@")) { name = name.substring(1); @@ -241,6 +242,7 @@ public void generate(String targetPath, Object model, Any config) throws IOExcep if("enum".equals(entryElement.getKey())) { propMap.put("isEnum", Any.wrap(true)); propMap.put("nameWithEnum", Any.wrap(name.substring(0, 1).toUpperCase() + name.substring(1) + "Enum")); + this.addUnderscores(entryElement); propMap.put("value", Any.wrap(entryElement.getValue())); } if("format".equals(entryElement.getKey())) { @@ -485,4 +487,15 @@ private static String getBasePath(OpenApi3 openApi3) { } return basePath; } + + private static void addUnderscores(Map.Entry entryElement) { + Iterator iterator = entryElement.getValue().iterator(); + List list = new ArrayList<>(); + while (iterator.hasNext()) { + Any any = iterator.next(); + String value = any.toString().trim().replaceAll(" ", "_"); + list.add(Any.wrap(value)); + } + entryElement.setValue(Any.wrap(list)); + } } diff --git a/light-rest-4j/src/main/java/com/networknt/codegen/rest/SwaggerGenerator.java b/light-rest-4j/src/main/java/com/networknt/codegen/rest/SwaggerGenerator.java index e3d667a24..bee4448f9 100644 --- a/light-rest-4j/src/main/java/com/networknt/codegen/rest/SwaggerGenerator.java +++ b/light-rest-4j/src/main/java/com/networknt/codegen/rest/SwaggerGenerator.java @@ -13,10 +13,7 @@ import java.nio.file.Files; import java.nio.file.Paths; import java.nio.file.StandardCopyOption; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; import static java.io.File.separator; @@ -165,6 +162,7 @@ public void generate(String targetPath, Object model, Any config) throws IOExcep //System.out.println("key = " + entryProp.getKey() + " value = " + entryProp.getValue()); Map propMap = new HashMap<>(); String name = entryProp.getKey(); + name = name.trim().replaceAll(" ", "_"); propMap.put("jsonProperty", Any.wrap(name)); if(name.startsWith("@")) { name = name.substring(1); @@ -211,6 +209,7 @@ public void generate(String targetPath, Object model, Any config) throws IOExcep if("enum".equals(entryElement.getKey())) { propMap.put("isEnum", Any.wrap(true)); propMap.put("nameWithEnum", Any.wrap(name.substring(0, 1).toUpperCase() + name.substring(1) + "Enum")); + this.addUnderscores(entryElement); propMap.put("value", Any.wrap(entryElement.getValue())); } if("format".equals(entryElement.getKey())) { @@ -360,4 +359,14 @@ public List> getOperationList(Object model) { } return result; } + private static void addUnderscores(Map.Entry entryElement) { + Iterator iterator = entryElement.getValue().iterator(); + List list = new ArrayList<>(); + while (iterator.hasNext()) { + Any any = iterator.next(); + String value = any.toString().trim().replaceAll(" ", "_"); + list.add(Any.wrap(value)); + } + entryElement.setValue(Any.wrap(list)); + } } diff --git a/light-rest-4j/src/test/java/com/networknt/codegen/OpenApiGeneratorTest.java b/light-rest-4j/src/test/java/com/networknt/codegen/OpenApiGeneratorTest.java index 4b04defff..b5aa6df2e 100644 --- a/light-rest-4j/src/test/java/com/networknt/codegen/OpenApiGeneratorTest.java +++ b/light-rest-4j/src/test/java/com/networknt/codegen/OpenApiGeneratorTest.java @@ -3,14 +3,17 @@ import com.jsoniter.JsonIterator; import com.jsoniter.any.Any; import com.networknt.codegen.rest.OpenApiGenerator; +import com.thoughtworks.qdox.JavaProjectBuilder; +import com.thoughtworks.qdox.model.JavaClass; +import com.thoughtworks.qdox.model.JavaField; +import com.thoughtworks.qdox.model.JavaPackage; import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; +import java.io.File; import java.io.IOException; -import java.net.URL; import java.nio.ByteBuffer; -import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Paths; import java.util.List; @@ -20,11 +23,12 @@ * @author Steve Hu */ public class OpenApiGeneratorTest { - public static String targetPath = "/tmp/openapi"; + public static String targetPath = System.getProperty("user.dir").replace("light-rest-4j", "openapi-test"); public static String configName = "/config.json"; public static String openapiJson = "/openapi.json"; public static String openapiYaml = "/openapi.yaml"; public static String openapiNoServersYaml = "/openapi-noServers.yaml"; + public static String packageName = "com.networknt.petstore.model"; @BeforeClass public static void setUp() throws IOException { @@ -32,7 +36,7 @@ public static void setUp() throws IOException { Files.createDirectories(Paths.get(targetPath)); } - //@AfterClass + // @AfterClass public static void tearDown() throws IOException { Files.deleteIfExists(Paths.get(targetPath)); } @@ -83,4 +87,50 @@ public void testNoServersGeneratorYaml() throws IOException { generator.generate(targetPath, strModel, anyConfig); } + @Test + public void testInvalidVaribleNameGeneratorYaml() throws IOException { + Any anyConfig = JsonIterator.parse(OpenApiGeneratorTest.class.getResourceAsStream(configName), 1024).readAny(); + String strModel = new Scanner(OpenApiGeneratorTest.class.getResourceAsStream(openapiYaml), "UTF-8").useDelimiter("\\A").next(); + OpenApiGenerator generator = new OpenApiGenerator(); + generator.generate(targetPath, strModel, anyConfig); + + File file = new File(targetPath); + JavaProjectBuilder javaProjectBuilder = new JavaProjectBuilder(); + javaProjectBuilder.addSourceTree(file); + JavaPackage javaPackage = javaProjectBuilder.getPackageByName(packageName); + + for (JavaClass javaClass : javaPackage.getClasses()) { + List fields = javaClass.getFields(); + for (JavaClass javaNestedClass : javaClass.getNestedClasses()) { + fields.addAll(javaNestedClass.getFields()); + } + for (JavaField field : fields) { + Assert.assertFalse(field.getName().contains(" ")); + } + } + } + + @Test + public void testInvalidVaribleNameGeneratorJson() throws IOException { + Any anyConfig = JsonIterator.parse(OpenApiGeneratorTest.class.getResourceAsStream(configName), 1024).readAny(); + Any anyModel = JsonIterator.parse(OpenApiGeneratorTest.class.getResourceAsStream(openapiJson), 1024).readAny(); + + OpenApiGenerator generator = new OpenApiGenerator(); + generator.generate(targetPath, anyModel, anyConfig); + + File file = new File(targetPath); + JavaProjectBuilder javaProjectBuilder = new JavaProjectBuilder(); + javaProjectBuilder.addSourceTree(file); + JavaPackage javaPackage = javaProjectBuilder.getPackageByName(packageName); + + for (JavaClass javaClass : javaPackage.getClasses()) { + List fields = javaClass.getFields(); + for (JavaClass javaNestedClass : javaClass.getNestedClasses()) { + fields.addAll(javaNestedClass.getFields()); + } + for (JavaField field : fields) { + Assert.assertFalse(field.getName().contains(" ")); + } + } + } } diff --git a/light-rest-4j/src/test/java/com/networknt/codegen/SwaggerGeneratorTest.java b/light-rest-4j/src/test/java/com/networknt/codegen/SwaggerGeneratorTest.java index 3f8ea5cac..bbb48ccc4 100644 --- a/light-rest-4j/src/test/java/com/networknt/codegen/SwaggerGeneratorTest.java +++ b/light-rest-4j/src/test/java/com/networknt/codegen/SwaggerGeneratorTest.java @@ -4,10 +4,15 @@ import com.jsoniter.JsonIterator; import com.jsoniter.any.Any; import com.networknt.codegen.rest.SwaggerGenerator; +import com.thoughtworks.qdox.JavaProjectBuilder; +import com.thoughtworks.qdox.model.JavaClass; +import com.thoughtworks.qdox.model.JavaField; +import com.thoughtworks.qdox.model.JavaPackage; import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; +import java.io.File; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.file.Files; @@ -18,9 +23,10 @@ * Created by stevehu on 2017-04-23. */ public class SwaggerGeneratorTest { - public static String targetPath = "/tmp/swagger"; + public static String targetPath = System.getProperty("user.dir").replace("light-rest-4j", "openapi-test");; public static String configName = "/config.json"; public static String swaggerName = "/swagger.json"; + public static String packageName = "com.networknt.petstore.model"; ObjectMapper mapper = new ObjectMapper(); @@ -65,4 +71,28 @@ public void testGetConfigSchema() throws IOException { Assert.assertNotNull(bf); System.out.println(bf.toString()); } + + @Test + public void testInvalidVaribleNameGeneratorJson() throws IOException { + Any anyConfig = JsonIterator.parse(SwaggerGeneratorTest.class.getResourceAsStream(configName), 1024).readAny(); + Any anyModel = JsonIterator.parse(SwaggerGeneratorTest.class.getResourceAsStream(swaggerName), 1024).readAny(); + + SwaggerGenerator generator = new SwaggerGenerator(); + generator.generate(targetPath, anyModel, anyConfig); + + File file = new File(targetPath); + JavaProjectBuilder javaProjectBuilder = new JavaProjectBuilder(); + javaProjectBuilder.addSourceTree(file); + JavaPackage javaPackage = javaProjectBuilder.getPackageByName(packageName); + + for (JavaClass javaClass : javaPackage.getClasses()) { + List fields = javaClass.getFields(); + for (JavaClass javaNestedClass : javaClass.getNestedClasses()) { + fields.addAll(javaNestedClass.getFields()); + } + for (JavaField field : fields) { + Assert.assertFalse(field.getName().contains(" ")); + } + } + } }