Skip to content

Commit

Permalink
Fixes #163
Browse files Browse the repository at this point in the history
Access HttpServletRequest in Spring Controller that implements generated interface
  • Loading branch information
Aleksandar Stojsavljevic committed Dec 7, 2018
1 parent fe21d9e commit ee36735
Show file tree
Hide file tree
Showing 9 changed files with 224 additions and 1 deletion.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ Then simply include the following code in the POM of the project you wish to gen
### injectHttpHeadersParameter
(optional, default: `false`) If set to true, we will generate a `HttpHeaders` parameter for each method to allow using request HTTP headers directly.

### injectHttpRequestParameter
(optional, default: `false`) If set to true, we will generate a `HttpServletRequest` parameter for each method to allow using request HTTP request directly.

### seperateMethodsByContentType
(optional, default: `false`) Should we generate separate API methods for endpoints which define multiple content types in their 200 response.

Expand All @@ -130,7 +133,7 @@ NOTE: This is different from a previous default. Use `RESOURCES` to get `0.x` be
(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](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.

Expand Down
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@
</exclusions>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>


<!-- Maven -->
<dependency>
<groupId>org.apache.maven</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ public class Config {
private static final String DEFAULT_DONT_GENERATE_FOR_ANNOTATION = null;
private static String dontGenerateForAnnotation = DEFAULT_DONT_GENERATE_FOR_ANNOTATION;

private static final Boolean DEFAULT_INJECT_HTTP_REQUEST_PARAMETER = Boolean.FALSE;
private static Boolean injectHttpRequestParameter = DEFAULT_INJECT_HTTP_REQUEST_PARAMETER;

Config() {
}

Expand Down Expand Up @@ -159,6 +162,17 @@ protected static void setDontGenerateForAnnotation(String dontGenerateForAnnotat
Config.dontGenerateForAnnotation = dontGenerateForAnnotation;
}

public static Boolean isInjectHttpRequestParameter() {
if (springMvcEndpointGeneratorMojo != null) {
return springMvcEndpointGeneratorMojo.injectHttpRequestParameter;
}
return injectHttpRequestParameter;
}

public static void setInjectHttpRequestParameter(Boolean injectHttpRequestParameter) {
Config.injectHttpRequestParameter = injectHttpRequestParameter;
}

public static String getPojoPackage() {
return getBasePackage() + NamingHelper.getDefaultModelPackage();
}
Expand All @@ -174,6 +188,7 @@ protected static void resetFields() {
setMethodsNamingLogic(DEFAULT_METHODS_NAMING_LOGIC);
setOverrideNamingLogicWith(DEFAULT_OVERRIDE_NAMING_LOGIC_WITH);
setDontGenerateForAnnotation(DEFAULT_DONT_GENERATE_FOR_ANNOTATION);
setInjectHttpRequestParameter(DEFAULT_INJECT_HTTP_REQUEST_PARAMETER);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,12 @@ public class SpringMvcEndpointGeneratorMojo extends AbstractMojo {
@Parameter(required = false, readonly = true)
protected String dontGenerateForAnnotation;

/**
* If set to true, we will generate methods with HttpRequest as a parameter
*/
@Parameter(required = false, readonly = true, defaultValue = "false")
protected Boolean injectHttpRequestParameter;

private ClassRealm classRealm;

private String resolvedSchemaLocation;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.List;
import java.util.Optional;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
Expand Down Expand Up @@ -112,6 +113,10 @@ public JMethod apply(ApiActionMetadata endpointMetadata, CodeModelHelper.JExtMet
paramHttpHeaders(generatableType);
}

if (Config.isInjectHttpRequestParameter()) {
paramHttpRequest(generatableType);
}

return generatableType.get();
}

Expand Down Expand Up @@ -235,4 +240,12 @@ protected JVar paramHttpHeaders(CodeModelHelper.JExtMethod generatableType) {
return paramHttpHeaders;
}

protected JVar paramHttpRequest(CodeModelHelper.JExtMethod generatableType) {
JVar paramHttpRequest = generatableType.get().param(HttpServletRequest.class, "httpRequest");
if (addParameterJavadoc) {
generatableType.get().javadoc().addParam("httpRequest The HTTP request");
}
return paramHttpRequest;
}

}
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.5
*/
public class Issue163RulesTest extends GitHubAbstractRuleTestBase {

@Test
public void check_http_request_as_method_parameter() throws Exception {
TestConfig.setInjectHttpRequestParameter(true);
loadRaml("issue-163.raml");
rule = new Spring4ControllerDecoratorRule();
rule.apply(getControllerMetadata(), jCodeModel);
verifyGeneratedCode("Issue163Spring4ControllerDecoratorRule");
TestConfig.setInjectHttpRequestParameter(false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,8 @@ public static void setDontGenerateForAnnotation(String dontGenerateForAnnotation
public static void setInitializeCollections(boolean initializeCollections) {
((TestPojoConfig) Config.getPojoConfig()).setInitializeCollections(initializeCollections);
}

public static void setInjectHttpRequestParameter(boolean injectHttpRequestParameter) {
Config.setInjectHttpRequestParameter(injectHttpRequestParameter);
}
}
22 changes: 22 additions & 0 deletions src/test/resources/ramls/github/issue-163.raml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#%RAML 1.0
title: Access HttpServletRequest in Spring Controller that implements generated interface #163

/test:
get:
queryParameters:
color: string
post:
body:
application/json:
type: string
/{testId}:
uriParameters:
testId: number
put:
body:
application/json:
type: string
delete:
responses:
201:

Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
-----------------------------------com.gen.test.TestController.java-----------------------------------

package com.gen.test;

import java.math.BigDecimal;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import org.springframework.http.ResponseEntity;


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


/**
* No description
*
*/
public ResponseEntity<?> getObjectByColor(java.lang.String color, HttpServletRequest httpRequest);

/**
* No description
*
*/
public ResponseEntity<?> createString(
@Valid
String string, HttpServletRequest httpRequest);

/**
* No description
*
*/
public ResponseEntity<?> updateString(BigDecimal testId,
@Valid
String string, HttpServletRequest httpRequest);

/**
* No description
*
*/
public ResponseEntity<?> deleteTestByTestId(BigDecimal testId, HttpServletRequest httpRequest);

}
-----------------------------------com.gen.test.TestControllerDecorator.java-----------------------------------

package com.gen.test;

import java.math.BigDecimal;
import javax.servlet.http.HttpServletRequest;
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.PathVariable;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;


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

@Autowired
private TestController testControllerDelegate;

/**
* No description
*
*/
@RequestMapping(value = "", method = RequestMethod.GET)
public ResponseEntity<?> getObjectByColor(
@RequestParam
java.lang.String color, HttpServletRequest httpRequest) {
return this.testControllerDelegate.getObjectByColor(color, httpRequest);
}

/**
* No description
*
*/
@RequestMapping(value = "", method = RequestMethod.POST)
public ResponseEntity<?> createString(
@Valid
@RequestBody
String string, HttpServletRequest httpRequest) {
return this.testControllerDelegate.createString(string, httpRequest);
}

/**
* No description
*
*/
@RequestMapping(value = "/{testId}", method = RequestMethod.PUT)
public ResponseEntity<?> updateString(
@PathVariable
BigDecimal testId,
@Valid
@RequestBody
String string, HttpServletRequest httpRequest) {
return this.testControllerDelegate.updateString(testId, string, httpRequest);
}

/**
* No description
*
*/
@RequestMapping(value = "/{testId}", method = RequestMethod.DELETE)
public ResponseEntity<?> deleteTestByTestId(
@PathVariable
BigDecimal testId, HttpServletRequest httpRequest) {
return this.testControllerDelegate.deleteTestByTestId(testId, httpRequest);
}

}

0 comments on commit ee36735

Please sign in to comment.