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