Skip to content

Commit a644f8b

Browse files
author
Aleksandar Stojsavljevic
committed
Fixes #263
Suppress Generation of Controller / Decorator for specific endpoints
1 parent 4c1bbaa commit a644f8b

File tree

8 files changed

+205
-3
lines changed

8 files changed

+205
-3
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,10 @@ NOTE: This is different from a previous default. Use `RESOURCES` to get `0.x` be
128128
### overrideNamingLogicWith
129129
(optional, default: "") The way to override naming logic for Java methods and arguments. Possible values:
130130
- `DISPLAY_NAME` (`displayName` attribute (if found) will be cleaned and used. `displayName` key is natively supported by RAML spec)
131-
- `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.
131+
- `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.
132+
133+
### dontGenerateForAnnotation
134+
(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.
132135

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

src/main/java/com/phoenixnap/oss/ramlplugin/raml2code/plugin/Config.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ public class Config {
3434
private static final OverrideNamingLogicWith DEFAULT_OVERRIDE_NAMING_LOGIC_WITH = null;
3535
private static OverrideNamingLogicWith overrideNamingLogicWith = DEFAULT_OVERRIDE_NAMING_LOGIC_WITH;
3636

37+
private static final String DEFAULT_DONT_GENERATE_FOR_ANNOTATION = null;
38+
private static String dontGenerateForAnnotation = DEFAULT_DONT_GENERATE_FOR_ANNOTATION;
39+
3740
Config() {
3841
}
3942

@@ -145,6 +148,17 @@ protected static void setOverrideNamingLogicWith(OverrideNamingLogicWith overrid
145148
Config.overrideNamingLogicWith = overrideNamingLogicWith;
146149
}
147150

151+
public static String getDontGenerateForAnnotation() {
152+
if (springMvcEndpointGeneratorMojo != null) {
153+
return springMvcEndpointGeneratorMojo.dontGenerateForAnnotation;
154+
}
155+
return dontGenerateForAnnotation;
156+
}
157+
158+
protected static void setDontGenerateForAnnotation(String dontGenerateForAnnotation) {
159+
Config.dontGenerateForAnnotation = dontGenerateForAnnotation;
160+
}
161+
148162
public static String getPojoPackage() {
149163
return getBasePackage() + NamingHelper.getDefaultModelPackage();
150164
}
@@ -159,6 +173,7 @@ protected static void resetFields() {
159173
setSeperateMethodsByContentType(DEFAULT_SEPERATE_METHODS_BY_CONTENTTYPE);
160174
setMethodsNamingLogic(DEFAULT_METHODS_NAMING_LOGIC);
161175
setOverrideNamingLogicWith(DEFAULT_OVERRIDE_NAMING_LOGIC_WITH);
176+
setDontGenerateForAnnotation(DEFAULT_DONT_GENERATE_FOR_ANNOTATION);
162177
}
163178

164179
}

src/main/java/com/phoenixnap/oss/ramlplugin/raml2code/plugin/SpringMvcEndpointGeneratorMojo.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,13 @@ public class SpringMvcEndpointGeneratorMojo extends AbstractMojo {
213213
@Parameter(required = false, readonly = true)
214214
protected OverrideNamingLogicWith overrideNamingLogicWith;
215215

216+
/**
217+
* Skip code generation for endpoints (resources and methods) annotated with
218+
* this annotation.
219+
*/
220+
@Parameter(required = false, readonly = true)
221+
protected String dontGenerateForAnnotation;
222+
216223
private ClassRealm classRealm;
217224

218225
private String resolvedSchemaLocation;

src/main/java/com/phoenixnap/oss/ramlplugin/raml2code/raml/raml10/RJP10V2RamlResource.java

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@
99
import java.util.stream.Stream;
1010

1111
import org.raml.v2.api.model.v10.datamodel.TypeDeclaration;
12+
import org.raml.v2.api.model.v10.declarations.AnnotationRef;
1213
import org.raml.v2.api.model.v10.methods.Method;
1314
import org.raml.v2.api.model.v10.resources.Resource;
15+
import org.springframework.util.StringUtils;
1416

1517
import com.phoenixnap.oss.ramlplugin.raml2code.helpers.NamingHelper;
1618
import com.phoenixnap.oss.ramlplugin.raml2code.helpers.RamlTypeHelper;
19+
import com.phoenixnap.oss.ramlplugin.raml2code.plugin.Config;
1720
import com.phoenixnap.oss.ramlplugin.raml2code.raml.RamlAction;
1821
import com.phoenixnap.oss.ramlplugin.raml2code.raml.RamlActionType;
1922
import com.phoenixnap.oss.ramlplugin.raml2code.raml.RamlResource;
@@ -44,7 +47,18 @@ private void rebuildChildren() {
4447
List<Resource> resources = delegate.resources();
4548
if (resources != null) {
4649
for (Resource resource : resources) {
47-
childResourceMap.put(resource.relativeUri().value(), new RJP10V2RamlResource(resource));
50+
boolean skipResource = false;
51+
if (StringUtils.hasText(Config.getDontGenerateForAnnotation())) {
52+
for (AnnotationRef annotation : resource.annotations()) {
53+
if (("(" + Config.getDontGenerateForAnnotation() + ")").equals(annotation.name())) {
54+
skipResource = true;
55+
}
56+
}
57+
}
58+
59+
if (!skipResource) {
60+
childResourceMap.put(resource.relativeUri().value(), new RJP10V2RamlResource(resource));
61+
}
4862
}
4963
}
5064
}
@@ -68,7 +82,19 @@ public String getRelativeUri() {
6882
public Map<RamlActionType, RamlAction> getActions() {
6983
Map<RamlActionType, RamlAction> actions = new HashMap<RamlActionType, RamlAction>();
7084
for (Method method : this.delegate.methods()) {
71-
actions.put(RamlActionType.valueOf(method.method().toUpperCase()), new RJP10V2RamlAction(method));
85+
86+
boolean skipMethod = false;
87+
if (StringUtils.hasText(Config.getDontGenerateForAnnotation())) {
88+
for (AnnotationRef annotation : method.annotations()) {
89+
if (("(" + Config.getDontGenerateForAnnotation() + ")").equals(annotation.name())) {
90+
skipMethod = true;
91+
}
92+
}
93+
}
94+
95+
if (!skipMethod) {
96+
actions.put(RamlActionType.valueOf(method.method().toUpperCase()), new RJP10V2RamlAction(method));
97+
}
7298
}
7399
return actions;
74100
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.phoenixnap.oss.ramlplugin.raml2code.github;
2+
3+
import org.junit.Test;
4+
5+
import com.phoenixnap.oss.ramlplugin.raml2code.plugin.TestConfig;
6+
import com.phoenixnap.oss.ramlplugin.raml2code.rules.GitHubAbstractRuleTestBase;
7+
import com.phoenixnap.oss.ramlplugin.raml2code.rules.Spring4ControllerDecoratorRule;
8+
9+
/**
10+
* @author aleksandars
11+
* @since 2.0.2
12+
*/
13+
public class Issue263RulesTest extends GitHubAbstractRuleTestBase {
14+
15+
@Test
16+
public void testDontGenerateForAnnotation() throws Exception {
17+
TestConfig.setDontGenerateForAnnotation("skipThis");
18+
loadRaml("issue-263.raml");
19+
rule = new Spring4ControllerDecoratorRule();
20+
rule.apply(getControllerMetadata(), jCodeModel);
21+
verifyGeneratedCode("Issue263Spring4ControllerDecorator");
22+
TestConfig.setDontGenerateForAnnotation(null);
23+
}
24+
}

src/test/java/com/phoenixnap/oss/ramlplugin/raml2code/plugin/TestConfig.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,8 @@ public static void setTimeType(String timeType) {
5555
((TestPojoConfig) Config.getPojoConfig()).setTimeType(timeType);
5656
}
5757

58+
public static void setDontGenerateForAnnotation(String dontGenerateForAnnotation) {
59+
Config.setDontGenerateForAnnotation(dontGenerateForAnnotation);
60+
}
61+
5862
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#%RAML 1.0
2+
title: Booking API
3+
4+
annotationTypes:
5+
skipThis:
6+
type: boolean
7+
allowedTargets: [Resource, Method]
8+
9+
/topLevel1:
10+
get:
11+
responses:
12+
200:
13+
body:
14+
application/json:
15+
post:
16+
(skipThis): true
17+
responses:
18+
200:
19+
body:
20+
application/json:
21+
/level2:
22+
put:
23+
body:
24+
application/json:
25+
type: string
26+
/level3:
27+
(skipThis): true
28+
get:
29+
responses:
30+
200:
31+
body:
32+
application/json:
33+
type: integer
34+
/level4:
35+
get:
36+
responses:
37+
200:
38+
body:
39+
application/json:
40+
type: number
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
-----------------------------------com.gen.test.TopLevel1Controller.java-----------------------------------
2+
3+
package com.gen.test;
4+
5+
import javax.validation.Valid;
6+
import org.springframework.http.ResponseEntity;
7+
8+
9+
/**
10+
* No description
11+
* (Generated with springmvc-raml-parser v.@project.version@)
12+
*
13+
*/
14+
public interface TopLevel1Controller {
15+
16+
17+
/**
18+
* No description
19+
*
20+
*/
21+
public ResponseEntity<?> getObject();
22+
23+
/**
24+
* No description
25+
*
26+
*/
27+
public ResponseEntity<?> updateString(
28+
@Valid
29+
String string);
30+
31+
}
32+
-----------------------------------com.gen.test.TopLevel1ControllerDecorator.java-----------------------------------
33+
34+
package com.gen.test;
35+
36+
import javax.validation.Valid;
37+
import org.springframework.beans.factory.annotation.Autowired;
38+
import org.springframework.http.ResponseEntity;
39+
import org.springframework.validation.annotation.Validated;
40+
import org.springframework.web.bind.annotation.RequestBody;
41+
import org.springframework.web.bind.annotation.RequestMapping;
42+
import org.springframework.web.bind.annotation.RequestMethod;
43+
import org.springframework.web.bind.annotation.RestController;
44+
45+
46+
/**
47+
* No description
48+
* (Generated with springmvc-raml-parser v.@project.version@)
49+
*
50+
*/
51+
@RestController
52+
@RequestMapping("/api/topLevel1")
53+
@Validated
54+
public class TopLevel1ControllerDecorator
55+
implements TopLevel1Controller
56+
{
57+
58+
@Autowired
59+
private TopLevel1Controller topLevel1ControllerDelegate;
60+
61+
/**
62+
* No description
63+
*
64+
*/
65+
@RequestMapping(value = "", method = RequestMethod.GET)
66+
public ResponseEntity<?> getObject() {
67+
return this.topLevel1ControllerDelegate.getObject();
68+
}
69+
70+
/**
71+
* No description
72+
*
73+
*/
74+
@RequestMapping(value = "/level2", method = RequestMethod.PUT)
75+
public ResponseEntity<?> updateString(
76+
@Valid
77+
@RequestBody
78+
String string) {
79+
return this.topLevel1ControllerDelegate.updateString(string);
80+
}
81+
82+
}
83+

0 commit comments

Comments
 (0)