Skip to content

Commit

Permalink
Add underscores automatically when space are present - closes #182 (#192
Browse files Browse the repository at this point in the history
)

* Fixed -Add underscores automatically when space are present #182
* Added test cases to check whether the fields' name contains space #182
  • Loading branch information
jiachen1120 authored and NicholasAzar committed Dec 14, 2018
1 parent c79ada4 commit 903d34e
Show file tree
Hide file tree
Showing 5 changed files with 215 additions and 109 deletions.
204 changes: 104 additions & 100 deletions light-rest-4j/pom.xml
Original file line number Diff line number Diff line change
@@ -1,100 +1,104 @@
<!--
~ Copyright (c) 2016 Network New Technologies Inc.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ You may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.networknt</groupId>
<artifactId>light-codegen</artifactId>
<version>1.5.24</version>
<relativePath>..</relativePath>
</parent>

<artifactId>light-rest-4j-generator</artifactId>
<packaging>jar</packaging>
<description>light-rest-4j code generator.</description>

<dependencies>
<dependency>
<groupId>com.networknt</groupId>
<artifactId>codegen-core</artifactId>
</dependency>
<dependency>
<groupId>com.networknt</groupId>
<artifactId>utility</artifactId>
</dependency>
<dependency>
<groupId>com.networknt</groupId>
<artifactId>openapi-parser</artifactId>
</dependency>

<dependency>
<groupId>com.jsoniter</groupId>
<artifactId>jsoniter</artifactId>
</dependency>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
</dependency>
<dependency>
<groupId>com.fizzed</groupId>
<artifactId>rocker-runtime</artifactId>
</dependency>

<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>com.fizzed</groupId>
<artifactId>rocker-maven-plugin</artifactId>
<version>${version.rocker}</version>
<executions>
<execution>
<id>generate-rocker-templates</id>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<templateDirectory>src/main/resources</templateDirectory>
</configuration>
</execution>
</executions>
</plugin>

</plugins>
</build>
</project>
<!--
~ Copyright (c) 2016 Network New Technologies Inc.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ You may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.networknt</groupId>
<artifactId>light-codegen</artifactId>
<version>1.5.24</version>
<relativePath>..</relativePath>
</parent>

<artifactId>light-rest-4j-generator</artifactId>
<packaging>jar</packaging>
<description>light-rest-4j code generator.</description>

<dependencies>
<dependency>
<groupId>com.networknt</groupId>
<artifactId>codegen-core</artifactId>
</dependency>
<dependency>
<groupId>com.networknt</groupId>
<artifactId>utility</artifactId>
</dependency>
<dependency>
<groupId>com.networknt</groupId>
<artifactId>openapi-parser</artifactId>
</dependency>

<dependency>
<groupId>com.jsoniter</groupId>
<artifactId>jsoniter</artifactId>
</dependency>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
</dependency>
<dependency>
<groupId>com.fizzed</groupId>
<artifactId>rocker-runtime</artifactId>
</dependency>

<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.thoughtworks.qdox</groupId>
<artifactId>qdox</artifactId>
<version>${qdox.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>com.fizzed</groupId>
<artifactId>rocker-maven-plugin</artifactId>
<version>${version.rocker}</version>
<executions>
<execution>
<id>generate-rocker-templates</id>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<templateDirectory>src/main/resources</templateDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Any> propMap = new HashMap<>();
String name = entryProp.getKey();
name = name.trim().replaceAll(" ", "_");
propMap.put("jsonProperty", Any.wrap(name));
if(name.startsWith("@")) {
name = name.substring(1);
Expand Down Expand Up @@ -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())) {
Expand Down Expand Up @@ -485,4 +487,15 @@ private static String getBasePath(OpenApi3 openApi3) {
}
return basePath;
}

private static void addUnderscores(Map.Entry<String, Any> entryElement) {
Iterator<Any> iterator = entryElement.getValue().iterator();
List<Any> 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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<String, Any> propMap = new HashMap<>();
String name = entryProp.getKey();
name = name.trim().replaceAll(" ", "_");
propMap.put("jsonProperty", Any.wrap(name));
if(name.startsWith("@")) {
name = name.substring(1);
Expand Down Expand Up @@ -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())) {
Expand Down Expand Up @@ -360,4 +359,14 @@ public List<Map<String, Any>> getOperationList(Object model) {
}
return result;
}
private static void addUnderscores(Map.Entry<String, Any> entryElement) {
Iterator<Any> iterator = entryElement.getValue().iterator();
List<Any> 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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -20,19 +23,20 @@
* @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 {
// create the output directory
Files.createDirectories(Paths.get(targetPath));
}

//@AfterClass
// @AfterClass
public static void tearDown() throws IOException {
Files.deleteIfExists(Paths.get(targetPath));
}
Expand Down Expand Up @@ -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<JavaField> 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<JavaField> fields = javaClass.getFields();
for (JavaClass javaNestedClass : javaClass.getNestedClasses()) {
fields.addAll(javaNestedClass.getFields());
}
for (JavaField field : fields) {
Assert.assertFalse(field.getName().contains(" "));
}
}
}
}
Loading

0 comments on commit 903d34e

Please sign in to comment.