Skip to content

Commit

Permalink
Fixes #250
Browse files Browse the repository at this point in the history
Better naming control
  • Loading branch information
Aleksandar Stojsavljevic committed Apr 26, 2018
1 parent 0404b46 commit 2a3a80a
Show file tree
Hide file tree
Showing 27 changed files with 920 additions and 27 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ Then simply include the following code in the POM of the project you wish to gen
### reverseOrderInClassNames
(optional, default: false) Reverse order of resource path that will be included in generated class names. If set to false URI will be included in class name from left to right.

### logicForParamsAndMethodsNaming
(optional, default: DEFAULT) Logic used for Java names of arguments and methods generation. Possible values: `DEFAULT`, `DISPLAY_NAME` (`displayName` attribute will be cleaned and used), `ANNOTATION` (value of `javaName` annotation will be used as is).

### ruleConfiguration
(optional) This is a key/value map for configuration of individual rules. Not all rules support configuration.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.stream.Collectors;

import org.raml.v2.api.model.v10.datamodel.JSONTypeDeclaration;
import org.raml.v2.api.model.v10.declarations.AnnotationRef;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;

Expand Down Expand Up @@ -88,10 +89,6 @@ public String toString() {
}

public Set<ApiParameterMetadata> getPathVariables() {
// FIXME Alex - comment this out!
// if (pathVariables != null) {
// return pathVariables;
// }
pathVariables = new LinkedHashSet<>();

RamlResource targetResource = action.getResource();
Expand Down Expand Up @@ -385,4 +382,12 @@ public String getRequestBodyMime() {
public void setRequestBodyMime(String requestBodyMime) {
this.requestBodyMime = requestBodyMime;
}

public String getDisplayName() {
return action.getDisplayName();
}

public List<AnnotationRef> getAnnotations() {
return action.getAnnotations();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@
import java.util.Set;

import org.apache.commons.lang.NullArgumentException;
import org.raml.v2.api.model.v10.declarations.AnnotationRef;
import org.springframework.util.StringUtils;

import com.phoenixnap.oss.ramlplugin.raml2code.helpers.NamingHelper;
import com.phoenixnap.oss.ramlplugin.raml2code.helpers.SchemaHelper;
import com.phoenixnap.oss.ramlplugin.raml2code.plugin.Config;
import com.phoenixnap.oss.ramlplugin.raml2code.plugin.SpringMvcEndpointGeneratorMojo.LogicForParamsAndMethodsNaming;
import com.phoenixnap.oss.ramlplugin.raml2code.raml.RamlAbstractParam;
import com.phoenixnap.oss.ramlplugin.raml2code.raml.RamlUriParameter;
import com.sun.codemodel.JCodeModel;
Expand Down Expand Up @@ -223,4 +227,27 @@ public boolean isArray() {
public JCodeModel getCodeModel() {
return codeModel;
}

public List<AnnotationRef> getAnnotations() {
return ramlParam.getAnnotations();
}

public String getJavaName() {
String javaName = null;
if (Config.getLogicForParamsAndMethodsNaming() == LogicForParamsAndMethodsNaming.DISPLAY_NAME
&& !StringUtils.isEmpty(this.getDisplayName())) {
javaName = NamingHelper.getParameterName(this.getDisplayName());
} else if (Config.getLogicForParamsAndMethodsNaming() == LogicForParamsAndMethodsNaming.ANNOTATION) {
for (AnnotationRef annotation : this.getAnnotations()) {
if ("(javaName)".equals(annotation.name())) {
javaName = String.valueOf(annotation.structuredValue().value());
break;
}
}
}
if (StringUtils.isEmpty(javaName)) {
javaName = NamingHelper.getParameterName(this.getName());
}
return javaName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,16 @@
import java.util.regex.Pattern;

import org.jsonschema2pojo.util.NameHelper;
import org.raml.v2.api.model.v10.declarations.AnnotationRef;
import org.raml.v2.internal.utils.Inflector;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;

import com.phoenixnap.oss.ramlplugin.raml2code.data.ApiActionMetadata;
import com.phoenixnap.oss.ramlplugin.raml2code.data.ApiBodyMetadata;
import com.phoenixnap.oss.ramlplugin.raml2code.data.ApiParameterMetadata;
import com.phoenixnap.oss.ramlplugin.raml2code.plugin.Config;
import com.phoenixnap.oss.ramlplugin.raml2code.plugin.SpringMvcEndpointGeneratorMojo.LogicForParamsAndMethodsNaming;
import com.phoenixnap.oss.ramlplugin.raml2code.raml.RamlActionType;
import com.phoenixnap.oss.ramlplugin.raml2code.raml.RamlResource;

Expand Down Expand Up @@ -228,11 +231,11 @@ public static String convertToClassName(String clazz) {
*/
public static String getResourceName(RamlResource resource, boolean singularize) {
String url = resource.getRelativeUri();
if (StringUtils.hasText(url)) {
if (url.contains("/") && (url.lastIndexOf("/") < url.length())) {
return getResourceName(url.substring(url.lastIndexOf("/") + 1), singularize);
}

if (StringUtils.hasText(url) && url.contains("/") && (url.lastIndexOf("/") < url.length())) {
return getResourceName(url.substring(url.lastIndexOf("/") + 1), singularize);
}

return null;
}

Expand Down Expand Up @@ -394,6 +397,17 @@ private static boolean doesUriEndsWithParam(String uri) {

public static String getActionName(ApiActionMetadata apiActionMetadata) {

if (Config.getLogicForParamsAndMethodsNaming() == LogicForParamsAndMethodsNaming.DISPLAY_NAME
&& !StringUtils.isEmpty(apiActionMetadata.getDisplayName())) {
return cleanNameForJava(apiActionMetadata.getDisplayName());
} else if (Config.getLogicForParamsAndMethodsNaming() == LogicForParamsAndMethodsNaming.ANNOTATION) {
for (AnnotationRef annotation : apiActionMetadata.getAnnotations()) {
if ("(javaName)".equals(annotation.name())) {
return String.valueOf(annotation.structuredValue().value());
}
}
}

String uri = apiActionMetadata.getResource().getUri();
String name = convertActionTypeToIntent(apiActionMetadata.getActionType(), doesUriEndsWithParam(uri));

Expand All @@ -416,8 +430,7 @@ public static String getActionName(ApiActionMetadata apiActionMetadata) {
name = name + "By";
if (parameterMetadataList.size() == 1) {
ApiParameterMetadata paramMetaData = parameterMetadataList.iterator().next();
name = name + StringUtils.capitalize(cleanNameForJava(
paramMetaData.getDisplayName() != null ? paramMetaData.getDisplayName() : paramMetaData.getName()));
name = name + StringUtils.capitalize(paramMetaData.getJavaName());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.phoenixnap.oss.ramlplugin.raml2code.helpers.NamingHelper;
import com.phoenixnap.oss.ramlplugin.raml2code.helpers.RamlTypeHelper;
import com.phoenixnap.oss.ramlplugin.raml2code.plugin.Config;
import com.phoenixnap.oss.ramlplugin.raml2code.plugin.SpringMvcEndpointGeneratorMojo.LogicForParamsAndMethodsNaming;
import com.phoenixnap.oss.ramlplugin.raml2code.raml.RamlDataType;
import com.phoenixnap.oss.ramlplugin.raml2code.raml.RamlRoot;
import com.sun.codemodel.JClass;
Expand Down Expand Up @@ -154,16 +155,33 @@ public RamlInterpretationResult interpret(RamlRoot document, TypeDeclaration typ

List<String> excludeFieldsFromToString = new ArrayList<>();
for (TypeDeclaration objectProperty : objectType.properties()) {

String fieldName = null;
if (Config.getLogicForParamsAndMethodsNaming() == LogicForParamsAndMethodsNaming.DISPLAY_NAME
&& objectProperty.displayName() != null) {
fieldName = NamingHelper.getParameterName(objectProperty.displayName().value());
} else if (Config.getLogicForParamsAndMethodsNaming() == LogicForParamsAndMethodsNaming.ANNOTATION) {
for (AnnotationRef annotation : objectProperty.annotations()) {
if ("(javaName)".equals(annotation.name())) {
fieldName = String.valueOf(annotation.structuredValue().value());
break;
}
}
}
if (StringUtils.isEmpty(fieldName)) {
fieldName = NamingHelper.getParameterName(objectProperty.name());
}

List<AnnotationRef> annotations = objectProperty.annotations();
for (AnnotationRef annotation : annotations) {
if ("(isSensitive)".equals(annotation.name()) && Boolean.TRUE.equals(annotation.structuredValue().value())) {
excludeFieldsFromToString.add(objectProperty.name());
excludeFieldsFromToString.add(fieldName);
}
}
RamlInterpretationResult childResult = RamlInterpreterFactory.getInterpreterForType(objectProperty).interpret(document,
objectProperty, builderModel, true);
String childType = childResult.getResolvedClassOrBuiltOrObject().fullName();
builder.withField(objectProperty.name(), childType, RamlTypeHelper.getDescription(objectProperty), childResult.getValidations(),
builder.withField(fieldName, childType, RamlTypeHelper.getDescription(objectProperty), childResult.getValidations(),
objectProperty);

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,12 @@ public boolean isRequired() {
}

/**
* Adds validation annotations to the supplied field
* Adds validation annotations to the supplied method
*
* @param field
* to add annotations to
* @param getter
* getter method to add validation annotation to
* @param addValidAnnotation
* if {@code @Valid} annotation dhould be added
*/
public void annotateFieldJSR303(JMethod getter, boolean addValidAnnotation) {
if (isRequired()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.phoenixnap.oss.ramlplugin.raml2code.plugin;

import com.phoenixnap.oss.ramlplugin.raml2code.helpers.NamingHelper;
import com.phoenixnap.oss.ramlplugin.raml2code.plugin.SpringMvcEndpointGeneratorMojo.LogicForParamsAndMethodsNaming;

public class Config {

Expand All @@ -26,6 +27,9 @@ public class Config {
private static final String DEFAULT_BASE_PACKAGE = "com.gen.test";
private static String basePackage = DEFAULT_BASE_PACKAGE;

private static final LogicForParamsAndMethodsNaming DEFAULT_LOGIC_FOR_PARAMS_AND_METHODS_NAMING = LogicForParamsAndMethodsNaming.DEFAULT;
private static LogicForParamsAndMethodsNaming logicForParamsAndMethodsNaming = DEFAULT_LOGIC_FOR_PARAMS_AND_METHODS_NAMING;

Config() {
}

Expand Down Expand Up @@ -112,6 +116,17 @@ public static String getBasePackage() {
return basePackage;
}

public static LogicForParamsAndMethodsNaming getLogicForParamsAndMethodsNaming() {
if (springMvcEndpointGeneratorMojo != null) {
return springMvcEndpointGeneratorMojo.logicForParamsAndMethodsNaming;
}
return logicForParamsAndMethodsNaming;
}

protected static void setLogicForParamsAndMethodsNaming(LogicForParamsAndMethodsNaming logicForParamsAndMethodsNaming) {
Config.logicForParamsAndMethodsNaming = logicForParamsAndMethodsNaming;
}

public static String getPojoPackage() {
return getBasePackage() + NamingHelper.getDefaultModelPackage();
}
Expand All @@ -124,5 +139,7 @@ protected static void resetFields() {
setResourceTopLevelInClassNames(DEFAULT_RESOURCE_TOP_LEVEL_IN_CLASS_NAMES);
setReverseOrderInClassNames(DEFAULT_REVERSE_ORDER_IN_CLASS_NAMES);
setSeperateMethodsByContentType(DEFAULT_SEPERATE_METHODS_BY_CONTENTTYPE);
setLogicForParamsAndMethodsNaming(DEFAULT_LOGIC_FOR_PARAMS_AND_METHODS_NAMING);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,15 @@ public class SpringMvcEndpointGeneratorMojo extends AbstractMojo {
@Parameter(required = false, readonly = true, defaultValue = "false")
protected Boolean reverseOrderInClassNames;

/**
* Logic used for Java names of arguments and methods generation. Possible
* values: <ul> <li>DEFAULT</li><li>DISPLAY_NAME - displayName attribute
* will be cleaned and used</li><li>ANNOTATION - "javaName" annotation will
* be used as is</li> </ul>
*/
@Parameter(required = false, readonly = true, defaultValue = "DEFAULT")
protected LogicForParamsAndMethodsNaming logicForParamsAndMethodsNaming;

private ClassRealm classRealm;

private String resolvedSchemaLocation;
Expand Down Expand Up @@ -504,4 +513,8 @@ public void execute() throws MojoExecutionException, MojoFailureException {
this.getLog().info("Endpoint Generation Completed in:" + (System.currentTimeMillis() - startTime) + "ms");
}

public enum LogicForParamsAndMethodsNaming {
DEFAULT, DISPLAY_NAME, ANNOTATION
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
package com.phoenixnap.oss.ramlplugin.raml2code.raml;

import java.math.BigDecimal;
import java.util.List;

import org.raml.v2.api.model.v10.declarations.AnnotationRef;

/**
* Abstract Representation of a Raml Parameter
Expand Down Expand Up @@ -49,6 +52,8 @@ public abstract class RamlAbstractParam { // extends AbstractParam {

public abstract BigDecimal getMaximum();

public abstract List<AnnotationRef> getAnnotations();

/**
* Convenience method for easier processing. Non supporting parameters are
* assumed to be single.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import java.util.List;
import java.util.Map;

import org.raml.v2.api.model.v10.declarations.AnnotationRef;

/**
* Abstract Representation of a Raml Action
*
Expand Down Expand Up @@ -42,4 +44,6 @@ public interface RamlAction {
String getDisplayName();

List<RamlSecurityReference> getSecuredBy();

List<AnnotationRef> getAnnotations();
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.List;
import java.util.Map;

import org.raml.v2.api.model.v10.declarations.AnnotationRef;
import org.raml.v2.api.model.v10.methods.Method;

import com.phoenixnap.oss.ramlplugin.raml2code.helpers.RamlTypeHelper;
Expand Down Expand Up @@ -127,4 +128,9 @@ public String getDisplayName() {
public List<RamlSecurityReference> getSecuredBy() {
return ramlModelFactory.createRamlSecurityReferences(method.securedBy());
}

@Override
public List<AnnotationRef> getAnnotations() {
return this.method.annotations();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
package com.phoenixnap.oss.ramlplugin.raml2code.raml.raml10;

import java.math.BigDecimal;
import java.util.List;

import org.raml.v2.api.model.v10.datamodel.NumberTypeDeclaration;
import org.raml.v2.api.model.v10.datamodel.StringTypeDeclaration;
import org.raml.v2.api.model.v10.datamodel.TypeDeclaration;
import org.raml.v2.api.model.v10.declarations.AnnotationRef;

import com.phoenixnap.oss.ramlplugin.raml2code.data.RamlFormParameter;
import com.phoenixnap.oss.ramlplugin.raml2code.helpers.RamlTypeHelper;
Expand Down Expand Up @@ -137,4 +139,9 @@ public BigDecimal getMaximum() {
public String getName() {
return this.formParameter.name();
}

@Override
public List<AnnotationRef> getAnnotations() {
return this.formParameter.annotations();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
package com.phoenixnap.oss.ramlplugin.raml2code.raml.raml10;

import java.math.BigDecimal;
import java.util.List;

import org.raml.v2.api.model.v10.datamodel.NumberTypeDeclaration;
import org.raml.v2.api.model.v10.datamodel.StringTypeDeclaration;
import org.raml.v2.api.model.v10.datamodel.TypeDeclaration;
import org.raml.v2.api.model.v10.declarations.AnnotationRef;

import com.phoenixnap.oss.ramlplugin.raml2code.helpers.RamlTypeHelper;
import com.phoenixnap.oss.ramlplugin.raml2code.raml.RamlHeader;
Expand Down Expand Up @@ -123,4 +125,9 @@ public BigDecimal getMaximum() {
public String getName() {
return this.header.name();
}

@Override
public List<AnnotationRef> getAnnotations() {
return this.header.annotations();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,6 @@ private List<RamlFormParameter> getList(TypeDeclaration typeDeclaration) {
@Override
public String getSchema() {
return mimeType.type();
// FIXME Alex
// if (RamlTypeHelper.isSchemaType(mimeType)) {
// return ((ExternalTypeDeclaration) mimeType).schemaContent();
// } else {
// return null;
// }
}

@Override
Expand Down
Loading

0 comments on commit 2a3a80a

Please sign in to comment.