Skip to content

Commit

Permalink
Fixes #261
Browse files Browse the repository at this point in the history
Method names for DELETE are not self-explanatory
  • Loading branch information
Aleksandar Stojsavljevic committed Jun 1, 2018
1 parent 4b76860 commit 4c1bbaa
Show file tree
Hide file tree
Showing 10 changed files with 225 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -428,26 +428,23 @@ private static String getActionNameFromObjects(ApiActionMetadata apiActionMetada
name += "Object";
}

List<ApiParameterMetadata> parameterMetadataList = getParameters(apiActionMetadata);

if (!parameterMetadataList.isEmpty()) {
if (parameterMetadataList.size() == 1) {
ApiParameterMetadata paramMetaData = parameterMetadataList.iterator().next();
name = name + "By" + StringUtils.capitalize(paramMetaData.getJavaName());
}
}
name = appendActionNameWithSingleParameter(apiActionMetadata, name);

} else if (apiActionMetadata.getActionType().equals(RamlActionType.DELETE)) {

List<ApiParameterMetadata> parameterMetadataList = getParameters(apiActionMetadata);

if (!parameterMetadataList.isEmpty()) {
name = name + "By";
if (parameterMetadataList.size() == 1) {
name = name + cleanNameForJava(StringUtils.capitalize(parameterMetadataList.iterator().next().getName()));
}
// for DELETE method we'll still use resource name
String url = cleanLeadingAndTrailingNewLineAndChars(apiActionMetadata.getResource().getUri());
String[] splitUrl = SLASH.split(url);
String resourceNameToUse = null;
if (splitUrl.length > 1 && StringUtils.countOccurrencesOf(splitUrl[splitUrl.length - 1], "{") > 0) {
resourceNameToUse = splitUrl[splitUrl.length - 2];
} else {
resourceNameToUse = splitUrl[splitUrl.length - 1];
}

name = name + StringUtils.capitalize(cleanNameForJava(singularize(resourceNameToUse)));
name = appendActionNameWithSingleParameter(apiActionMetadata, name);

} else {
ApiBodyMetadata requestBody = apiActionMetadata.getRequestBody();
String creationObject;
Expand All @@ -473,6 +470,19 @@ private static List<ApiParameterMetadata> getParameters(ApiActionMetadata apiAct
return parameterMetadataList;
}

private static String appendActionNameWithSingleParameter(ApiActionMetadata apiActionMetadata, String methodName) {

String newMethodName = methodName;

List<ApiParameterMetadata> parameterMetadataList = getParameters(apiActionMetadata);
if (parameterMetadataList.size() == 1) {
ApiParameterMetadata paramMetaData = parameterMetadataList.iterator().next();
newMethodName = newMethodName + "By" + StringUtils.capitalize(paramMetaData.getJavaName());
}

return newMethodName;
}

/**
* Attempts to infer the name of an action (intent) from a resource's
* relative URL and action details
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.phoenixnap.oss.ramlplugin.raml2code.github;

import org.junit.Test;

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

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

@Test
public void verify_naming_logic_for_delete() throws Exception {
loadRaml("issue-261.raml");
rule = new Spring4ControllerDecoratorRule();
rule.apply(getControllerMetadata(), jCodeModel);
verifyGeneratedCode("Issue261Spring4ControllerDecorator");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
*/
public abstract class AbstractRuleTestBase {

public static final boolean VISUALISE_CODE = true;
public static final boolean VISUALISE_CODE = false;
public static final String RESOURCE_BASE = "ramls/";
public static final String VALIDATOR_BASE = "validations/";
public static final String LINE_END = System.getProperty("line.separator");
Expand Down
30 changes: 30 additions & 0 deletions src/test/resources/ramls/github/issue-261.raml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#%RAML 1.0
title: Method names for DELETE are not self-explanatory

/deleting:
/test-object/{testId}:
uriParameters:
testId: integer
delete:
/subresource:
delete:
queryParameters:
name: string

/some-other-object/:
delete:
queryParameters:
id: integer
number: integer

/anotherobject:
/{objectId}:
uriParameters:
objectId: integer
delete:
headers:
headerParam: string

/singleobject:
delete:

2 changes: 1 addition & 1 deletion src/test/resources/validations/FeignClient.java.txt
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public interface SongClient {
*
*/
@RequestMapping(value = "/{songId}", method = RequestMethod.DELETE)
public ResponseEntity deleteBySongId(
public ResponseEntity deleteSongBySongId(
@PathVariable
String songId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ public interface MessageController {
* deleteMessage
*
*/
public ResponseEntity<?> deleteByMessageId(String messageId);
public ResponseEntity<?> deleteMessageByMessageId(String messageId);

/**
* subresource
Expand Down Expand Up @@ -317,10 +317,10 @@ public class MessageControllerDecorator
*
*/
@RequestMapping(value = "/{messageId}", method = RequestMethod.DELETE)
public ResponseEntity<?> deleteByMessageId(
public ResponseEntity<?> deleteMessageByMessageId(
@PathVariable
String messageId) {
return this.messageControllerDelegate.deleteByMessageId(messageId);
return this.messageControllerDelegate.deleteMessageByMessageId(messageId);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ public interface PersonManagerController {
*
*/
@DeleteMapping("/{id}")
public ResponseEntity<?> deleteById(
public ResponseEntity<?> deleteManagerById(
@PathVariable
String id);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public interface TestController {
* No description
*
*/
public ResponseEntity<?> deleteBy(BigDecimal test1, String test2, BigDecimal xTest1, String xTest2,
public ResponseEntity<?> deleteTest(BigDecimal test1, String test2, BigDecimal xTest1, String xTest2,
@Valid
DeleteObject deleteObject);

Expand Down Expand Up @@ -162,7 +162,7 @@ public class TestControllerDecorator
*
*/
@RequestMapping(value = "", method = RequestMethod.DELETE)
public ResponseEntity<?> deleteBy(
public ResponseEntity<?> deleteTest(
@RequestParam
BigDecimal test1,
@RequestParam
Expand All @@ -174,7 +174,7 @@ public class TestControllerDecorator
@Valid
@RequestBody
DeleteObject deleteObject) {
return this.testControllerDelegate.deleteBy(test1, test2, xTest1, xTest2, deleteObject);
return this.testControllerDelegate.deleteTest(test1, test2, xTest1, xTest2, deleteObject);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public interface TestController {
* No description
*
*/
public ResponseEntity<?> deleteByTestId(Long testId);
public ResponseEntity<?> deleteTestByTestId(Long testId);

}
-----------------------------------com.gen.test.TestControllerDecorator.java-----------------------------------
Expand Down Expand Up @@ -199,10 +199,10 @@ public class TestControllerDecorator
*
*/
@RequestMapping(value = "/{testId}", method = RequestMethod.DELETE)
public ResponseEntity<?> deleteByTestId(
public ResponseEntity<?> deleteTestByTestId(
@PathVariable
Long testId) {
return this.testControllerDelegate.deleteByTestId(testId);
return this.testControllerDelegate.deleteTestByTestId(testId);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
-----------------------------------com.gen.test.DeletingController.java-----------------------------------

package com.gen.test;

import org.springframework.http.ResponseEntity;


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


/**
* No description
*
*/
public ResponseEntity<?> deleteTestObjectByTestId(Long testId);

/**
* No description
*
*/
public ResponseEntity<?> deleteSubresource(Long testId, String name);

/**
* No description
*
*/
public ResponseEntity<?> deleteSomeOtherObject(Long id, Long number);

/**
* No description
*
*/
public ResponseEntity<?> deleteAnotherobject(Long objectId, String headerParam);

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

}
-----------------------------------com.gen.test.DeletingControllerDecorator.java-----------------------------------

package com.gen.test;

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.RequestHeader;
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/deleting")
@Validated
public class DeletingControllerDecorator
implements DeletingController
{

@Autowired
private DeletingController deletingControllerDelegate;

/**
* No description
*
*/
@RequestMapping(value = "/test-object/{testId}", method = RequestMethod.DELETE)
public ResponseEntity<?> deleteTestObjectByTestId(
@PathVariable
Long testId) {
return this.deletingControllerDelegate.deleteTestObjectByTestId(testId);
}

/**
* No description
*
*/
@RequestMapping(value = "/test-object/{testId}/subresource", method = RequestMethod.DELETE)
public ResponseEntity<?> deleteSubresource(
@PathVariable
Long testId,
@RequestParam
String name) {
return this.deletingControllerDelegate.deleteSubresource(testId, name);
}

/**
* No description
*
*/
@RequestMapping(value = "/some-other-object/", method = RequestMethod.DELETE)
public ResponseEntity<?> deleteSomeOtherObject(
@RequestParam
Long id,
@RequestParam
Long number) {
return this.deletingControllerDelegate.deleteSomeOtherObject(id, number);
}

/**
* No description
*
*/
@RequestMapping(value = "/anotherobject/{objectId}", method = RequestMethod.DELETE)
public ResponseEntity<?> deleteAnotherobject(
@PathVariable
Long objectId,
@RequestHeader
String headerParam) {
return this.deletingControllerDelegate.deleteAnotherobject(objectId, headerParam);
}

/**
* No description
*
*/
@RequestMapping(value = "/singleobject", method = RequestMethod.DELETE)
public ResponseEntity<?> deleteSingleobject() {
return this.deletingControllerDelegate.deleteSingleobject();
}

}

0 comments on commit 4c1bbaa

Please sign in to comment.