Skip to content

Commit 1559c4f

Browse files
philipplangwing328
authored andcommitted
Issue 758 root resource (#771)
* Unit-Test for JavaJAXRSSpecServerCodegen. * Path generation for primary resource fixed. * Unit test for toApiName. * Review-Feedback: blank line removed.
1 parent 0cd6d8c commit 1559c4f

2 files changed

Lines changed: 132 additions & 6 deletions

File tree

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavaJAXRSSpecServerCodegen.java

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
package org.openapitools.codegen.languages;
1919

2020
import io.swagger.v3.oas.models.Operation;
21-
2221
import org.apache.commons.lang3.StringUtils;
2322
import org.openapitools.codegen.CliOption;
2423
import org.openapitools.codegen.CodegenConstants;
@@ -42,7 +41,9 @@ public class JavaJAXRSSpecServerCodegen extends AbstractJavaJAXRSServerCodegen {
4241
private boolean interfaceOnly = false;
4342
private boolean returnResponse = false;
4443
private boolean generatePom = true;
45-
44+
45+
private String primaryResourceName;
46+
4647
public JavaJAXRSSpecServerCodegen() {
4748
super();
4849
invokerPackage = "org.openapitools.api";
@@ -147,19 +148,26 @@ public void addOperationToGroup(String tag, String resourcePath, Operation opera
147148
if (pos > 0) {
148149
basePath = basePath.substring(0, pos);
149150
}
150-
151+
152+
String operationKey = basePath;
151153
if (StringUtils.isEmpty(basePath)) {
152-
basePath = "default";
154+
basePath = tag;
155+
operationKey = "";
156+
primaryResourceName = tag;
157+
} else if (basePath.matches("\\{.*\\}")) {
158+
basePath = tag;
159+
operationKey = "";
160+
co.subresourceOperation = true;
153161
} else {
154162
if (co.path.startsWith("/" + basePath)) {
155163
co.path = co.path.substring(("/" + basePath).length());
156164
}
157165
co.subresourceOperation = !co.path.isEmpty();
158166
}
159-
List<CodegenOperation> opList = operations.get(basePath);
167+
List<CodegenOperation> opList = operations.get(operationKey);
160168
if (opList == null || opList.isEmpty()) {
161169
opList = new ArrayList<CodegenOperation>();
162-
operations.put(basePath, opList);
170+
operations.put(operationKey, opList);
163171
}
164172
opList.add(co);
165173
co.baseName = basePath;
@@ -186,4 +194,14 @@ public Map<String, Object> postProcessSupportingFileData(Map<String, Object> obj
186194
public String getHelp() {
187195
return "Generates a Java JAXRS Server according to JAXRS 2.0 specification.";
188196
}
197+
198+
@Override
199+
public String toApiName(final String name) {
200+
String computed = name;
201+
if (computed.length() == 0) {
202+
return primaryResourceName + "Api";
203+
}
204+
computed = sanitizeName(computed);
205+
return camelize(computed) + "Api";
206+
}
189207
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package org.openapitools.codegen.java.jaxrs;
2+
3+
import io.swagger.v3.oas.models.Operation;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
import org.openapitools.codegen.CodegenOperation;
7+
import org.openapitools.codegen.languages.JavaJAXRSSpecServerCodegen;
8+
9+
import java.util.HashMap;
10+
import java.util.List;
11+
import java.util.Map;
12+
13+
import static org.hamcrest.CoreMatchers.is;
14+
import static org.junit.Assert.assertThat;
15+
16+
/**
17+
* Unit-Test for {@link org.openapitools.codegen.languages.JavaJAXRSSpecServerCodegen}.
18+
*
19+
* @author attrobit
20+
*/
21+
public class JavaJAXRSSpecServerCodegenTest {
22+
23+
private JavaJAXRSSpecServerCodegen instance;
24+
25+
@Before
26+
public void before() {
27+
instance = new JavaJAXRSSpecServerCodegen();
28+
}
29+
30+
/**
31+
* Test
32+
* {@link JavaJAXRSSpecServerCodegen#addOperationToGroup(String, String, Operation, CodegenOperation, Map)} for Resource with path "/" and set tag.
33+
*/
34+
@Test
35+
public void testAddOperationToGroupForRootResource() {
36+
CodegenOperation codegenOperation = new CodegenOperation();
37+
codegenOperation.operationId = "findPrimaryresource";
38+
Operation operation = new Operation();
39+
Map<String, List<CodegenOperation>> operationList = new HashMap<>();
40+
41+
instance.addOperationToGroup("Primaryresource", "/", operation, codegenOperation, operationList);
42+
43+
assertThat(operationList.size(), is(1));
44+
assertThat(operationList.containsKey(""), is(true));
45+
assertThat(codegenOperation.baseName, is("Primaryresource"));
46+
}
47+
48+
/**
49+
* Test
50+
* {@link JavaJAXRSSpecServerCodegen#addOperationToGroup(String, String, Operation, CodegenOperation, Map)} for Resource with path param.
51+
*/
52+
@Test
53+
public void testAddOperationToGroupForRootResourcePathParam() {
54+
CodegenOperation codegenOperation = new CodegenOperation();
55+
codegenOperation.operationId = "getPrimaryresource";
56+
Operation operation = new Operation();
57+
Map<String, List<CodegenOperation>> operationList = new HashMap<>();
58+
59+
instance.addOperationToGroup("Primaryresource", "/{uuid}", operation, codegenOperation, operationList);
60+
61+
assertThat(operationList.size(), is(1));
62+
assertThat(operationList.containsKey(""), is(true));
63+
assertThat(codegenOperation.baseName, is("Primaryresource"));
64+
}
65+
66+
/**
67+
* Test
68+
* {@link JavaJAXRSSpecServerCodegen#addOperationToGroup(String, String,
69+
* Operation, CodegenOperation, Map)} for Resource with path "/subresource".
70+
*/
71+
@Test
72+
public void testAddOperationToGroupForSubresource() {
73+
CodegenOperation codegenOperation = new CodegenOperation();
74+
codegenOperation.path = "/subresource";
75+
Operation operation = new Operation();
76+
Map<String, List<CodegenOperation>> operationList = new HashMap<>();
77+
78+
instance.addOperationToGroup("Default", "/subresource", operation, codegenOperation, operationList);
79+
80+
assertThat(codegenOperation.baseName, is("subresource"));
81+
assertThat(operationList.size(), is(1));
82+
assertThat(operationList.containsKey("subresource"), is(true));
83+
}
84+
85+
/**
86+
* Test {@link JavaJAXRSSpecServerCodegen#toApiName(String)} with subresource.
87+
*/
88+
@Test
89+
public void testToApiNameForSubresource() {
90+
final String subresource = instance.toApiName("subresource");
91+
assertThat(subresource, is("SubresourceApi"));
92+
}
93+
94+
/**
95+
* Test {@link JavaJAXRSSpecServerCodegen#toApiName(String)} with primary resource.
96+
*/
97+
@Test
98+
public void testToApiNameForPrimaryResource() {
99+
CodegenOperation codegenOperation = new CodegenOperation();
100+
codegenOperation.operationId = "findPrimaryresource";
101+
Operation operation = new Operation();
102+
Map<String, List<CodegenOperation>> operationList = new HashMap<>();
103+
instance.addOperationToGroup("Primaryresource", "/", operation, codegenOperation, operationList);
104+
105+
final String subresource = instance.toApiName("");
106+
assertThat(subresource, is("PrimaryresourceApi"));
107+
}
108+
}

0 commit comments

Comments
 (0)