diff --git a/README.md b/README.md index 8eba4034..6562692b 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/src/main/java/com/phoenixnap/oss/ramlplugin/raml2code/plugin/Config.java b/src/main/java/com/phoenixnap/oss/ramlplugin/raml2code/plugin/Config.java index ecef5392..59cc0ed2 100644 --- a/src/main/java/com/phoenixnap/oss/ramlplugin/raml2code/plugin/Config.java +++ b/src/main/java/com/phoenixnap/oss/ramlplugin/raml2code/plugin/Config.java @@ -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() { } @@ -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(); } @@ -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); } } diff --git a/src/main/java/com/phoenixnap/oss/ramlplugin/raml2code/plugin/SpringMvcEndpointGeneratorMojo.java b/src/main/java/com/phoenixnap/oss/ramlplugin/raml2code/plugin/SpringMvcEndpointGeneratorMojo.java index 7dc8d9f5..9b414b72 100644 --- a/src/main/java/com/phoenixnap/oss/ramlplugin/raml2code/plugin/SpringMvcEndpointGeneratorMojo.java +++ b/src/main/java/com/phoenixnap/oss/ramlplugin/raml2code/plugin/SpringMvcEndpointGeneratorMojo.java @@ -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; diff --git a/src/main/java/com/phoenixnap/oss/ramlplugin/raml2code/raml/raml10/RJP10V2RamlResource.java b/src/main/java/com/phoenixnap/oss/ramlplugin/raml2code/raml/raml10/RJP10V2RamlResource.java index f623945f..05d16d27 100644 --- a/src/main/java/com/phoenixnap/oss/ramlplugin/raml2code/raml/raml10/RJP10V2RamlResource.java +++ b/src/main/java/com/phoenixnap/oss/ramlplugin/raml2code/raml/raml10/RJP10V2RamlResource.java @@ -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; @@ -44,7 +47,18 @@ private void rebuildChildren() { List 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)); + } } } } @@ -68,7 +82,19 @@ public String getRelativeUri() { public Map getActions() { Map actions = new HashMap(); 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; } diff --git a/src/test/java/com/phoenixnap/oss/ramlplugin/raml2code/github/Issue263RulesTest.java b/src/test/java/com/phoenixnap/oss/ramlplugin/raml2code/github/Issue263RulesTest.java new file mode 100644 index 00000000..e94c24f9 --- /dev/null +++ b/src/test/java/com/phoenixnap/oss/ramlplugin/raml2code/github/Issue263RulesTest.java @@ -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); + } +} diff --git a/src/test/java/com/phoenixnap/oss/ramlplugin/raml2code/plugin/TestConfig.java b/src/test/java/com/phoenixnap/oss/ramlplugin/raml2code/plugin/TestConfig.java index bc67b46c..6b081f64 100644 --- a/src/test/java/com/phoenixnap/oss/ramlplugin/raml2code/plugin/TestConfig.java +++ b/src/test/java/com/phoenixnap/oss/ramlplugin/raml2code/plugin/TestConfig.java @@ -55,4 +55,8 @@ public static void setTimeType(String timeType) { ((TestPojoConfig) Config.getPojoConfig()).setTimeType(timeType); } + public static void setDontGenerateForAnnotation(String dontGenerateForAnnotation) { + Config.setDontGenerateForAnnotation(dontGenerateForAnnotation); + } + } diff --git a/src/test/resources/ramls/github/issue-263.raml b/src/test/resources/ramls/github/issue-263.raml new file mode 100644 index 00000000..adef6309 --- /dev/null +++ b/src/test/resources/ramls/github/issue-263.raml @@ -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 \ No newline at end of file diff --git a/src/test/resources/validations/github/Issue263Spring4ControllerDecorator.java.txt b/src/test/resources/validations/github/Issue263Spring4ControllerDecorator.java.txt new file mode 100644 index 00000000..dd19ae16 --- /dev/null +++ b/src/test/resources/validations/github/Issue263Spring4ControllerDecorator.java.txt @@ -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 v.@project.version@) + * + */ +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 v.@project.version@) + * + */ +@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); + } + +} +