Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[jaxrs-spec] fix incorrect @Path-Generation for resource-path "/" #771

Merged
merged 4 commits into from
Aug 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package org.openapitools.codegen.languages;

import io.swagger.v3.oas.models.Operation;

import org.apache.commons.lang3.StringUtils;
import org.openapitools.codegen.CliOption;
import org.openapitools.codegen.CodegenConstants;
Expand All @@ -42,7 +41,9 @@ public class JavaJAXRSSpecServerCodegen extends AbstractJavaJAXRSServerCodegen {
private boolean interfaceOnly = false;
private boolean returnResponse = false;
private boolean generatePom = true;


private String primaryResourceName;

public JavaJAXRSSpecServerCodegen() {
super();
invokerPackage = "org.openapitools.api";
Expand Down Expand Up @@ -147,19 +148,26 @@ public void addOperationToGroup(String tag, String resourcePath, Operation opera
if (pos > 0) {
basePath = basePath.substring(0, pos);
}


String operationKey = basePath;
if (StringUtils.isEmpty(basePath)) {
basePath = "default";
basePath = tag;
operationKey = "";
primaryResourceName = tag;
} else if (basePath.matches("\\{.*\\}")) {
basePath = tag;
operationKey = "";
co.subresourceOperation = true;
} else {
if (co.path.startsWith("/" + basePath)) {
co.path = co.path.substring(("/" + basePath).length());
}
co.subresourceOperation = !co.path.isEmpty();
}
List<CodegenOperation> opList = operations.get(basePath);
List<CodegenOperation> opList = operations.get(operationKey);
if (opList == null || opList.isEmpty()) {
opList = new ArrayList<CodegenOperation>();
operations.put(basePath, opList);
operations.put(operationKey, opList);
}
opList.add(co);
co.baseName = basePath;
Expand All @@ -186,4 +194,14 @@ public Map<String, Object> postProcessSupportingFileData(Map<String, Object> obj
public String getHelp() {
return "Generates a Java JAXRS Server according to JAXRS 2.0 specification.";
}

@Override
public String toApiName(final String name) {
String computed = name;
if (computed.length() == 0) {
return primaryResourceName + "Api";
}
computed = sanitizeName(computed);
return camelize(computed) + "Api";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package org.openapitools.codegen.java.jaxrs;

import io.swagger.v3.oas.models.Operation;
import org.junit.Before;
import org.junit.Test;
import org.openapitools.codegen.CodegenOperation;
import org.openapitools.codegen.languages.JavaJAXRSSpecServerCodegen;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

/**
* Unit-Test for {@link org.openapitools.codegen.languages.JavaJAXRSSpecServerCodegen}.
*
* @author attrobit
*/
public class JavaJAXRSSpecServerCodegenTest {

private JavaJAXRSSpecServerCodegen instance;

@Before
public void before() {
instance = new JavaJAXRSSpecServerCodegen();
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could remove a line here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JFCote Whats next? I cant merge the PR. Do you merge the PR?

/**
* Test
* {@link JavaJAXRSSpecServerCodegen#addOperationToGroup(String, String, Operation, CodegenOperation, Map)} for Resource with path "/" and set tag.
*/
@Test
public void testAddOperationToGroupForRootResource() {
CodegenOperation codegenOperation = new CodegenOperation();
codegenOperation.operationId = "findPrimaryresource";
Operation operation = new Operation();
Map<String, List<CodegenOperation>> operationList = new HashMap<>();

instance.addOperationToGroup("Primaryresource", "/", operation, codegenOperation, operationList);

assertThat(operationList.size(), is(1));
assertThat(operationList.containsKey(""), is(true));
assertThat(codegenOperation.baseName, is("Primaryresource"));
}

/**
* Test
* {@link JavaJAXRSSpecServerCodegen#addOperationToGroup(String, String, Operation, CodegenOperation, Map)} for Resource with path param.
*/
@Test
public void testAddOperationToGroupForRootResourcePathParam() {
CodegenOperation codegenOperation = new CodegenOperation();
codegenOperation.operationId = "getPrimaryresource";
Operation operation = new Operation();
Map<String, List<CodegenOperation>> operationList = new HashMap<>();

instance.addOperationToGroup("Primaryresource", "/{uuid}", operation, codegenOperation, operationList);

assertThat(operationList.size(), is(1));
assertThat(operationList.containsKey(""), is(true));
assertThat(codegenOperation.baseName, is("Primaryresource"));
}

/**
* Test
* {@link JavaJAXRSSpecServerCodegen#addOperationToGroup(String, String,
* Operation, CodegenOperation, Map)} for Resource with path "/subresource".
*/
@Test
public void testAddOperationToGroupForSubresource() {
CodegenOperation codegenOperation = new CodegenOperation();
codegenOperation.path = "/subresource";
Operation operation = new Operation();
Map<String, List<CodegenOperation>> operationList = new HashMap<>();

instance.addOperationToGroup("Default", "/subresource", operation, codegenOperation, operationList);

assertThat(codegenOperation.baseName, is("subresource"));
assertThat(operationList.size(), is(1));
assertThat(operationList.containsKey("subresource"), is(true));
}

/**
* Test {@link JavaJAXRSSpecServerCodegen#toApiName(String)} with subresource.
*/
@Test
public void testToApiNameForSubresource() {
final String subresource = instance.toApiName("subresource");
assertThat(subresource, is("SubresourceApi"));
}

/**
* Test {@link JavaJAXRSSpecServerCodegen#toApiName(String)} with primary resource.
*/
@Test
public void testToApiNameForPrimaryResource() {
CodegenOperation codegenOperation = new CodegenOperation();
codegenOperation.operationId = "findPrimaryresource";
Operation operation = new Operation();
Map<String, List<CodegenOperation>> operationList = new HashMap<>();
instance.addOperationToGroup("Primaryresource", "/", operation, codegenOperation, operationList);

final String subresource = instance.toApiName("");
assertThat(subresource, is("PrimaryresourceApi"));
}
}