Skip to content

Commit

Permalink
Fixes #263
Browse files Browse the repository at this point in the history
Suppress Generation of Controller / Decorator for specific endpoints
  • Loading branch information
Aleksandar Stojsavljevic committed Jun 13, 2018
1 parent 4c1bbaa commit a644f8b
Show file tree
Hide file tree
Showing 8 changed files with 205 additions and 3 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,10 @@ NOTE: This is different from a previous default. Use `RESOURCES` to get `0.x` be
### overrideNamingLogicWith
(optional, default: "") The way to override naming logic for Java methods and arguments. Possible values:
- `DISPLAY_NAME` (`displayName` attribute (if found) will be cleaned and used. `displayName` key is natively supported by RAML spec)
- `ANNOTATION` (`javaName` annotation (if found) will be used as is). Refer to RAML [Annotation Types](https://github.com/raml-org/raml-spec/blob/master/versions/raml-10/raml-10.md#annotations) for more details.
- `ANNOTATION` (`javaName` annotation (if found) will be used as is). Refer to RAML [Annotation](https://github.com/raml-org/raml-spec/blob/master/versions/raml-10/raml-10.md#annotations) for more details.

### dontGenerateForAnnotation
(optional, default: "") When defined, code generation will be skipped for resources and methods annotated with this [Annotation](https://github.com/raml-org/raml-spec/blob/master/versions/raml-10/raml-10.md#annotations). When annotation is set on resource - all methods in the resource and all sub-resources will be ignored. Value of the annotation is not important.

### 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 @@ -34,6 +34,9 @@ public class Config {
private static final OverrideNamingLogicWith DEFAULT_OVERRIDE_NAMING_LOGIC_WITH = null;
private static OverrideNamingLogicWith overrideNamingLogicWith = DEFAULT_OVERRIDE_NAMING_LOGIC_WITH;

private static final String DEFAULT_DONT_GENERATE_FOR_ANNOTATION = null;
private static String dontGenerateForAnnotation = DEFAULT_DONT_GENERATE_FOR_ANNOTATION;

Config() {
}

Expand Down Expand Up @@ -145,6 +148,17 @@ protected static void setOverrideNamingLogicWith(OverrideNamingLogicWith overrid
Config.overrideNamingLogicWith = overrideNamingLogicWith;
}

public static String getDontGenerateForAnnotation() {
if (springMvcEndpointGeneratorMojo != null) {
return springMvcEndpointGeneratorMojo.dontGenerateForAnnotation;
}
return dontGenerateForAnnotation;
}

protected static void setDontGenerateForAnnotation(String dontGenerateForAnnotation) {
Config.dontGenerateForAnnotation = dontGenerateForAnnotation;
}

public static String getPojoPackage() {
return getBasePackage() + NamingHelper.getDefaultModelPackage();
}
Expand All @@ -159,6 +173,7 @@ protected static void resetFields() {
setSeperateMethodsByContentType(DEFAULT_SEPERATE_METHODS_BY_CONTENTTYPE);
setMethodsNamingLogic(DEFAULT_METHODS_NAMING_LOGIC);
setOverrideNamingLogicWith(DEFAULT_OVERRIDE_NAMING_LOGIC_WITH);
setDontGenerateForAnnotation(DEFAULT_DONT_GENERATE_FOR_ANNOTATION);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,13 @@ public class SpringMvcEndpointGeneratorMojo extends AbstractMojo {
@Parameter(required = false, readonly = true)
protected OverrideNamingLogicWith overrideNamingLogicWith;

/**
* Skip code generation for endpoints (resources and methods) annotated with
* this annotation.
*/
@Parameter(required = false, readonly = true)
protected String dontGenerateForAnnotation;

private ClassRealm classRealm;

private String resolvedSchemaLocation;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@
import java.util.stream.Stream;

import org.raml.v2.api.model.v10.datamodel.TypeDeclaration;
import org.raml.v2.api.model.v10.declarations.AnnotationRef;
import org.raml.v2.api.model.v10.methods.Method;
import org.raml.v2.api.model.v10.resources.Resource;
import org.springframework.util.StringUtils;

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.raml.RamlAction;
import com.phoenixnap.oss.ramlplugin.raml2code.raml.RamlActionType;
import com.phoenixnap.oss.ramlplugin.raml2code.raml.RamlResource;
Expand Down Expand Up @@ -44,7 +47,18 @@ private void rebuildChildren() {
List<Resource> resources = delegate.resources();
if (resources != null) {
for (Resource resource : resources) {
childResourceMap.put(resource.relativeUri().value(), new RJP10V2RamlResource(resource));
boolean skipResource = false;
if (StringUtils.hasText(Config.getDontGenerateForAnnotation())) {
for (AnnotationRef annotation : resource.annotations()) {
if (("(" + Config.getDontGenerateForAnnotation() + ")").equals(annotation.name())) {
skipResource = true;
}
}
}

if (!skipResource) {
childResourceMap.put(resource.relativeUri().value(), new RJP10V2RamlResource(resource));
}
}
}
}
Expand All @@ -68,7 +82,19 @@ public String getRelativeUri() {
public Map<RamlActionType, RamlAction> getActions() {
Map<RamlActionType, RamlAction> actions = new HashMap<RamlActionType, RamlAction>();
for (Method method : this.delegate.methods()) {
actions.put(RamlActionType.valueOf(method.method().toUpperCase()), new RJP10V2RamlAction(method));

boolean skipMethod = false;
if (StringUtils.hasText(Config.getDontGenerateForAnnotation())) {
for (AnnotationRef annotation : method.annotations()) {
if (("(" + Config.getDontGenerateForAnnotation() + ")").equals(annotation.name())) {
skipMethod = true;
}
}
}

if (!skipMethod) {
actions.put(RamlActionType.valueOf(method.method().toUpperCase()), new RJP10V2RamlAction(method));
}
}
return actions;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.phoenixnap.oss.ramlplugin.raml2code.github;

import org.junit.Test;

import com.phoenixnap.oss.ramlplugin.raml2code.plugin.TestConfig;
import com.phoenixnap.oss.ramlplugin.raml2code.rules.GitHubAbstractRuleTestBase;
import com.phoenixnap.oss.ramlplugin.raml2code.rules.Spring4ControllerDecoratorRule;

/**
* @author aleksandars
* @since 2.0.2
*/
public class Issue263RulesTest extends GitHubAbstractRuleTestBase {

@Test
public void testDontGenerateForAnnotation() throws Exception {
TestConfig.setDontGenerateForAnnotation("skipThis");
loadRaml("issue-263.raml");
rule = new Spring4ControllerDecoratorRule();
rule.apply(getControllerMetadata(), jCodeModel);
verifyGeneratedCode("Issue263Spring4ControllerDecorator");
TestConfig.setDontGenerateForAnnotation(null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,8 @@ public static void setTimeType(String timeType) {
((TestPojoConfig) Config.getPojoConfig()).setTimeType(timeType);
}

public static void setDontGenerateForAnnotation(String dontGenerateForAnnotation) {
Config.setDontGenerateForAnnotation(dontGenerateForAnnotation);
}

}
40 changes: 40 additions & 0 deletions src/test/resources/ramls/github/issue-263.raml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#%RAML 1.0
title: Booking API

annotationTypes:
skipThis:
type: boolean
allowedTargets: [Resource, Method]

/topLevel1:
get:
responses:
200:
body:
application/json:
post:
(skipThis): true
responses:
200:
body:
application/json:
/level2:
put:
body:
application/json:
type: string
/level3:
(skipThis): true
get:
responses:
200:
body:
application/json:
type: integer
/level4:
get:
responses:
200:
body:
application/json:
type: number
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
-----------------------------------com.gen.test.TopLevel1Controller.java-----------------------------------

package com.gen.test;

import javax.validation.Valid;
import org.springframework.http.ResponseEntity;


/**
* No description
* (Generated with springmvc-raml-parser [email protected]@)
*
*/
public interface TopLevel1Controller {


/**
* No description
*
*/
public ResponseEntity<?> getObject();

/**
* No description
*
*/
public ResponseEntity<?> updateString(
@Valid
String string);

}
-----------------------------------com.gen.test.TopLevel1ControllerDecorator.java-----------------------------------

package com.gen.test;

import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;


/**
* No description
* (Generated with springmvc-raml-parser [email protected]@)
*
*/
@RestController
@RequestMapping("/api/topLevel1")
@Validated
public class TopLevel1ControllerDecorator
implements TopLevel1Controller
{

@Autowired
private TopLevel1Controller topLevel1ControllerDelegate;

/**
* No description
*
*/
@RequestMapping(value = "", method = RequestMethod.GET)
public ResponseEntity<?> getObject() {
return this.topLevel1ControllerDelegate.getObject();
}

/**
* No description
*
*/
@RequestMapping(value = "/level2", method = RequestMethod.PUT)
public ResponseEntity<?> updateString(
@Valid
@RequestBody
String string) {
return this.topLevel1ControllerDelegate.updateString(string);
}

}

0 comments on commit a644f8b

Please sign in to comment.