Skip to content

Commit

Permalink
Fixes #152, Fixes #288
Browse files Browse the repository at this point in the history
Collision with java keywords
Import Statement for BigDecimal/BigInteger not generated for request body
  • Loading branch information
Aleksandar Stojsavljevic committed Sep 28, 2018
1 parent 5ad8ccc commit 1368ef1
Show file tree
Hide file tree
Showing 35 changed files with 2,711 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import static org.apache.commons.lang3.StringUtils.upperCase;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
Expand Down Expand Up @@ -57,6 +58,14 @@ public class NamingHelper {

private static final String ILLEGAL_CHARACTER_REGEX = "[^0-9a-zA-Z_$]";

private static final String KEYWORDS[] = { "abstract", "assert", "boolean", "break", "byte", "case", "catch", "char", "class", "const",
"continue", "default", "do", "double", "else", "extends", "false", "final", "finally", "float", "for", "goto", "if",
"implements", "import", "instanceof", "int", "interface", "long", "native", "new", "null", "package", "private", "protected",
"public", "return", "short", "static", "strictfp", "super", "switch", "synchronized", "this", "throw", "throws", "transient",
"true", "try", "void", "volatile", "while" };

private static List<String> keywordsList;

private static NameHelper cachedNameHelper;

private static NameHelper getNameHelper() {
Expand Down Expand Up @@ -217,7 +226,7 @@ public static String cleanLeadingAndTrailingNewLineAndChars(String input) {
* @return The name for this using Java class convention
*/
public static String convertToClassName(String clazz) {
return StringUtils.capitalize(cleanNameForJava(clazz));
return StringUtils.capitalize(cleanNameForJava(clazz, false));
}

/**
Expand Down Expand Up @@ -330,7 +339,7 @@ public static String pluralize(String target) {
* @return A name suitable for a Java parameter
*/
public static String getParameterName(String name) {
return StringUtils.uncapitalize(cleanNameForJava(name));
return filterKeywords(StringUtils.uncapitalize(cleanNameForJava(name)));
}

/**
Expand All @@ -341,14 +350,31 @@ public static String getParameterName(String name) {
* @return cleaned string
*/
public static String cleanNameForJava(String resourceName) {

return cleanNameForJava(resourceName, true);
}

private static String cleanNameForJava(String resourceName, boolean filterKeywords) {
String outString = resourceName;
if (StringUtils.hasText(resourceName)) {
outString = getNameHelper().replaceIllegalCharacters(resourceName);
if (StringUtils.hasText(outString)) {
outString = getNameHelper().normalizeName(outString);
}
}
return outString;

if (!filterKeywords) {
return outString;
}

return filterKeywords(outString);
}

public static String filterKeywords(String name) {
if (getKeywords().contains(name)) {
return name + "Custom";
}
return name;
}

/**
Expand All @@ -375,7 +401,7 @@ public static String cleanNameForJavaEnum(String enumConstant) {
enumName = "_" + enumName;
}

return enumName;
return filterKeywords(enumName);
}

private static boolean doesUriEndsWithParam(String uri) {
Expand Down Expand Up @@ -632,4 +658,11 @@ public static String getDefaultModelPackage() {
return ".model";
}

private static List<String> getKeywords() {
if (keywordsList == null || keywordsList.isEmpty()) {
keywordsList = Arrays.asList(KEYWORDS);
}
return keywordsList;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import static com.phoenixnap.oss.ramlplugin.raml2code.helpers.CodeModelHelper.findFirstClassBySimpleName;
import static org.springframework.util.StringUtils.uncapitalize;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -174,7 +176,15 @@ protected JVar paramQueryForm(ApiParameterMetadata paramMetaData, CodeModelHelpe
}

protected JVar paramObjects(ApiActionMetadata endpointMetadata, CodeModelHelper.JExtMethod generatableType) {

String requestBodyName = endpointMetadata.getRequestBody().getName();
String requestBodyFullName = requestBodyName;
if (BigDecimal.class.getSimpleName().equals(requestBodyName)) {
requestBodyFullName = BigDecimal.class.getName();
} else if (BigInteger.class.getSimpleName().equals(requestBodyName)) {
requestBodyFullName = BigInteger.class.getName();
}

boolean array = endpointMetadata.getRequestBody().isArray();

List<JCodeModel> codeModels = new ArrayList<>();
Expand All @@ -186,15 +196,15 @@ protected JVar paramObjects(ApiActionMetadata endpointMetadata, CodeModelHelper.
codeModels.add(generatableType.owner());
}

JClass requestBodyType = findFirstClassBySimpleName(codeModels.toArray(new JCodeModel[codeModels.size()]), requestBodyName);
JClass requestBodyType = findFirstClassBySimpleName(codeModels.toArray(new JCodeModel[codeModels.size()]), requestBodyFullName);
if (allowArrayParameters && array) {
JClass arrayType = generatableType.owner().ref(List.class);
requestBodyType = arrayType.narrow(requestBodyType);
}
if (addParameterJavadoc) {
generatableType.get().javadoc().addParam(uncapitalize(requestBodyName) + " The Request Body Payload");
generatableType.get().javadoc().addParam(NamingHelper.getParameterName(requestBodyName) + " The Request Body Payload");
}
JVar param = generatableType.get().param(requestBodyType, uncapitalize(requestBodyName));
JVar param = generatableType.get().param(requestBodyType, NamingHelper.getParameterName(requestBodyName));
if (Config.getPojoConfig().isIncludeJsr303Annotations() && !RamlActionType.PATCH.equals(endpointMetadata.getActionType())) {
// skip Valid annotation for PATCH actions since it's a partial
// update so some required fields might be omitted
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.phoenixnap.oss.ramlplugin.raml2code.data.ApiActionMetadata;
import com.phoenixnap.oss.ramlplugin.raml2code.data.ApiParameterMetadata;
import com.phoenixnap.oss.ramlplugin.raml2code.helpers.CodeModelHelper;
import com.phoenixnap.oss.ramlplugin.raml2code.helpers.NamingHelper;
import com.phoenixnap.oss.ramlplugin.raml2code.helpers.RamlHelper;
import com.phoenixnap.oss.ramlplugin.raml2code.helpers.RuleHelper;
import com.phoenixnap.oss.ramlplugin.raml2code.plugin.Config;
Expand Down Expand Up @@ -152,7 +153,7 @@ public JMethod apply(ApiActionMetadata endpointMetadata, CodeModelHelper.JExtMet
JInvocation init = JExpr._new(httpEntityClass);

if (endpointMetadata.getRequestBody() != null) {
init.arg(methodParamMap.get(CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_CAMEL, endpointMetadata.getRequestBody().getName())));
init.arg(methodParamMap.get(NamingHelper.getParameterName(endpointMetadata.getRequestBody().getName())));
}
init.arg(httpHeaders);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.phoenixnap.oss.ramlplugin.raml2code.github;

import org.junit.Before;
import org.junit.Test;

import com.phoenixnap.oss.ramlplugin.raml2code.data.ApiResourceMetadata;
import com.phoenixnap.oss.ramlplugin.raml2code.plugin.Config;
import com.phoenixnap.oss.ramlplugin.raml2code.rules.ConfigurableRule;
import com.phoenixnap.oss.ramlplugin.raml2code.rules.GitHubAbstractRuleTestBase;
import com.phoenixnap.oss.ramlplugin.raml2code.rules.Spring4ControllerDecoratorRule;
import com.phoenixnap.oss.ramlplugin.raml2code.rules.Spring4ControllerInterfaceRule;
import com.phoenixnap.oss.ramlplugin.raml2code.rules.Spring4RestTemplateClientRule;
import com.phoenixnap.oss.ramlplugin.raml2code.rules.TestPojoConfig;
import com.sun.codemodel.JCodeModel;
import com.sun.codemodel.JDefinedClass;

/**
* @author aleksandars
* @since 2.0.4
*/
public class Issue152RulesTest extends GitHubAbstractRuleTestBase {

private ConfigurableRule<JCodeModel, JDefinedClass, ApiResourceMetadata> rule;

@Before
public void init() {
super.setGitHubValidatorBase(DEFAULT_GITHUB_VALIDATOR_BASE + "issue-152/");
}

@Test
public void collision_java_keywords_decorator() throws Exception {
((TestPojoConfig) Config.getPojoConfig()).setUseBigIntegers(true);
loadRaml("issue-152.raml");
rule = new Spring4ControllerDecoratorRule();
rule.apply(getControllerMetadata(), jCodeModel);
verifyGeneratedCode("Issue152-Spring4ControllerDecorator");
((TestPojoConfig) Config.getPojoConfig()).setUseBigIntegers(false);
}

@Test
public void collision_java_keywords_interface() throws Exception {
((TestPojoConfig) Config.getPojoConfig()).setUseBigIntegers(true);
loadRaml("issue-152.raml");
rule = new Spring4ControllerInterfaceRule();
rule.apply(getControllerMetadata(), jCodeModel);
verifyGeneratedCode("Issue152-Spring4ControllerInterface");
((TestPojoConfig) Config.getPojoConfig()).setUseBigIntegers(false);
}

@Test
public void collision_java_keywords_client() throws Exception {
((TestPojoConfig) Config.getPojoConfig()).setUseBigIntegers(true);
loadRaml("issue-152.raml");
rule = new Spring4RestTemplateClientRule();
rule.apply(getControllerMetadata(), jCodeModel);
verifyGeneratedCode("Issue152-Spring4RestTemplateClient");
((TestPojoConfig) Config.getPojoConfig()).setUseBigIntegers(false);
}

}
Loading

0 comments on commit 1368ef1

Please sign in to comment.