Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed -Add underscores automatically when space are present #182 #192

Merged
merged 3 commits into from
Dec 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -20,10 +20,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 @@ -196,6 +193,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 @@ -242,6 +240,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 @@ -460,4 +459,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 @@ -163,6 +160,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 @@ -209,6 +207,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 @@ -341,4 +340,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