Skip to content

Commit c7d5275

Browse files
authored
[java] jaxrs (all) add usetags option (#6130)
* Adds "useTags" option to all jaxrs code generators (AbstractJAXRSServerCodegen): - jaxrs-jersey - jaxrs-spec - jaxrs-cxf-cdi - jaxrs-resteasy - jaxrs-cxf - jaxrs-cxf-extended - java-msf4j - jaxrs-resteasy-eap * jaxrs-spec - Changed handling of root paths: e.g "/:", "/{id}:" to simplify code and create a more consistent behaviour -- old: use tag for classname -- new: use tag only if useTags is enabled, use "DefaultApi" if not * @path class level annotation - for all generators above and the microprofile generator (uses same jaxrs postprocessing) - extended the "commonPath" to contain more than only the root path if possible - e.g. "/group1/subgroup1/op1" -> "/group1/subgroup1" is moved to class level annotation
1 parent 3ca6bc2 commit c7d5275

File tree

75 files changed

+753
-874
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+753
-874
lines changed

docs/generators/java-msf4j.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ sidebar_label: java-msf4j
5151
|sourceFolder|source folder for generated code| |src/main/java|
5252
|title|a title describing the application| |OpenAPI Server|
5353
|useBeanValidation|Use BeanValidation API annotations| |true|
54+
|useTags|use tags for creating interface and controller classnames| |false|
5455
|withXml|whether to include support for application/xml content type and include XML annotations in the model (works with libraries that provide support for JSON and XML)| |false|
5556

5657
## IMPORT MAPPING

docs/generators/jaxrs-cxf-cdi.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ sidebar_label: jaxrs-cxf-cdi
5757
|title|a title describing the application| |OpenAPI Server|
5858
|useBeanValidation|Use BeanValidation API annotations| |true|
5959
|useSwaggerAnnotations|Whether to generate Swagger annotations.| |true|
60+
|useTags|use tags for creating interface and controller classnames| |false|
6061
|withXml|whether to include support for application/xml content type and include XML annotations in the model (works with libraries that provide support for JSON and XML)| |false|
6162

6263
## IMPORT MAPPING

docs/generators/jaxrs-cxf-extended.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ sidebar_label: jaxrs-cxf-extended
7171
|useSpringAnnotationConfig|Use Spring Annotation Config| |false|
7272
|useSwaggerFeature|Use Swagger Feature| |false|
7373
|useSwaggerUI|Use Swagger UI| |false|
74+
|useTags|use tags for creating interface and controller classnames| |true|
7475
|useWadlFeature|Use WADL Feature| |false|
7576
|withXml|whether to include support for application/xml content type and include XML annotations in the model (works with libraries that provide support for JSON and XML)| |false|
7677

docs/generators/jaxrs-cxf.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ sidebar_label: jaxrs-cxf
6666
|useSpringAnnotationConfig|Use Spring Annotation Config| |false|
6767
|useSwaggerFeature|Use Swagger Feature| |false|
6868
|useSwaggerUI|Use Swagger UI| |false|
69+
|useTags|use tags for creating interface and controller classnames| |true|
6970
|useWadlFeature|Use WADL Feature| |false|
7071
|withXml|whether to include support for application/xml content type and include XML annotations in the model (works with libraries that provide support for JSON and XML)| |false|
7172

docs/generators/jaxrs-resteasy-eap.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ sidebar_label: jaxrs-resteasy-eap
5252
|title|a title describing the application| |OpenAPI Server|
5353
|useBeanValidation|Use BeanValidation API annotations| |true|
5454
|useSwaggerFeature|Use dynamic Swagger generator| |false|
55+
|useTags|use tags for creating interface and controller classnames| |false|
5556
|withXml|whether to include support for application/xml content type and include XML annotations in the model (works with libraries that provide support for JSON and XML)| |false|
5657

5758
## IMPORT MAPPING

docs/generators/jaxrs-resteasy.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ sidebar_label: jaxrs-resteasy
5151
|sourceFolder|source folder for generated code| |src/main/java|
5252
|title|a title describing the application| |OpenAPI Server|
5353
|useBeanValidation|Use BeanValidation API annotations| |true|
54+
|useTags|use tags for creating interface and controller classnames| |false|
5455
|withXml|whether to include support for application/xml content type and include XML annotations in the model (works with libraries that provide support for JSON and XML)| |false|
5556

5657
## IMPORT MAPPING

docs/generators/jaxrs-spec.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ sidebar_label: jaxrs-spec
5757
|title|a title describing the application| |OpenAPI Server|
5858
|useBeanValidation|Use BeanValidation API annotations| |true|
5959
|useSwaggerAnnotations|Whether to generate Swagger annotations.| |true|
60+
|useTags|use tags for creating interface and controller classnames| |false|
6061
|withXml|whether to include support for application/xml content type and include XML annotations in the model (works with libraries that provide support for JSON and XML)| |false|
6162

6263
## IMPORT MAPPING

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

Lines changed: 57 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717

1818
package org.openapitools.codegen.languages;
1919

20+
import com.google.common.annotations.VisibleForTesting;
2021
import io.swagger.v3.oas.models.OpenAPI;
2122
import io.swagger.v3.oas.models.Operation;
2223
import io.swagger.v3.oas.models.PathItem;
24+
import org.apache.commons.lang3.StringUtils;
2325
import org.openapitools.codegen.*;
2426
import org.openapitools.codegen.languages.features.BeanValidationFeatures;
2527
import org.openapitools.codegen.utils.URLPathUtils;
@@ -30,10 +32,10 @@
3032
import java.net.URL;
3133
import java.util.*;
3234

33-
import static org.openapitools.codegen.utils.StringUtils.camelize;
34-
3535
public abstract class AbstractJavaJAXRSServerCodegen extends AbstractJavaCodegen implements BeanValidationFeatures {
3636
public static final String SERVER_PORT = "serverPort";
37+
public static final String USE_TAGS = "useTags";
38+
3739
/**
3840
* Name of the sub-directory in "src/main/resource" where to find the
3941
* Mustache template for the JAX-RS Codegen.
@@ -43,7 +45,9 @@ public abstract class AbstractJavaJAXRSServerCodegen extends AbstractJavaCodegen
4345
protected String testResourcesFolder = "src/test/resources";
4446
protected String title = "OpenAPI Server";
4547
protected String serverPort = "8080";
48+
4649
protected boolean useBeanValidation = true;
50+
protected boolean useTags = false;
4751

4852
private static final Logger LOGGER = LoggerFactory.getLogger(AbstractJavaJAXRSServerCodegen.class);
4953

@@ -72,6 +76,7 @@ public AbstractJavaJAXRSServerCodegen() {
7276
cliOptions.add(new CliOption("title", "a title describing the application").defaultValue(title));
7377
cliOptions.add(CliOption.newBoolean(USE_BEANVALIDATION, "Use BeanValidation API annotations",useBeanValidation));
7478
cliOptions.add(new CliOption(SERVER_PORT, "The port on which the server should be started").defaultValue(serverPort));
79+
cliOptions.add(CliOption.newBoolean(USE_TAGS, "use tags for creating interface and controller classnames"));
7580
}
7681

7782

@@ -93,11 +98,32 @@ public void processOpts() {
9398
}
9499

95100
if (additionalProperties.containsKey(USE_BEANVALIDATION)) {
96-
this.setUseBeanValidation(convertPropertyToBoolean(USE_BEANVALIDATION));
101+
setUseBeanValidation(convertPropertyToBoolean(USE_BEANVALIDATION));
102+
}
103+
104+
if (additionalProperties.containsKey(USE_TAGS)) {
105+
setUseTags(convertPropertyToBoolean(USE_TAGS));
97106
}
98107

99108
writePropertyBack(USE_BEANVALIDATION, useBeanValidation);
109+
}
100110

111+
@Override
112+
public void addOperationToGroup(String tag, String resourcePath, Operation operation, CodegenOperation co, Map<String, List<CodegenOperation>> operations) {
113+
final String basePath = StringUtils.substringBefore(StringUtils.removeStart(resourcePath, "/"), "/");
114+
if (!StringUtils.isEmpty(basePath)) {
115+
co.subresourceOperation = !co.path.isEmpty();
116+
}
117+
if (useTags) {
118+
super.addOperationToGroup(tag, resourcePath, operation, co, operations);
119+
} else {
120+
co.baseName = basePath;
121+
if (StringUtils.isEmpty(co.baseName) || StringUtils.containsAny(co.baseName, "{", "}")) {
122+
co.baseName = "default";
123+
}
124+
final List<CodegenOperation> opList = operations.computeIfAbsent(co.baseName, k -> new ArrayList<>());
125+
opList.add(co);
126+
}
101127
}
102128

103129
@Override
@@ -151,9 +177,8 @@ public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> o
151177
static Map<String, Object> jaxrsPostProcessOperations(Map<String, Object> objs) {
152178
@SuppressWarnings("unchecked")
153179
Map<String, Object> operations = (Map<String, Object>) objs.get("operations");
180+
String commonPath = null;
154181
if (operations != null) {
155-
String commonBaseName = null;
156-
boolean baseNameEquals = true;
157182
@SuppressWarnings("unchecked")
158183
List<CodegenOperation> ops = (List<CodegenOperation>) operations.get("operation");
159184
for (CodegenOperation operation : ops) {
@@ -223,24 +248,18 @@ static Map<String, Object> jaxrsPostProcessOperations(Map<String, Object> objs)
223248
} else if ("map".equals(operation.returnContainer)) {
224249
operation.returnContainer = "Map";
225250
}
226-
227-
if(commonBaseName == null) {
228-
commonBaseName = operation.baseName;
229-
} else if(!commonBaseName.equals(operation.baseName)) {
230-
baseNameEquals = false;
251+
252+
if (commonPath == null) {
253+
commonPath = operation.path;
254+
} else {
255+
commonPath = getCommonPath(commonPath, operation.path);
231256
}
232257
}
233-
if(baseNameEquals) {
234-
objs.put("commonPath", commonBaseName);
235-
} else {
236-
for (CodegenOperation operation : ops) {
237-
if(operation.baseName != null) {
238-
operation.path = "/" + operation.baseName + operation.path;
239-
operation.baseName = null;
240-
}
241-
}
242-
objs.put("commonPath", null);
258+
for (CodegenOperation co : ops) {
259+
co.path = StringUtils.removeStart(co.path, commonPath);
260+
co.subresourceOperation = co.path.length() > 1;
243261
}
262+
objs.put("commonPath", "/".equals(commonPath) ? StringUtils.EMPTY : commonPath);
244263
}
245264
return objs;
246265
}
@@ -251,7 +270,7 @@ public String toApiName(final String name) {
251270
if (computed.length() > 0) {
252271
computed = sanitizeName(computed);
253272
}
254-
return super.toApiName(computed);
273+
return super.toApiName(computed);
255274
}
256275

257276
@Override
@@ -273,6 +292,19 @@ public String apiFilename(String templateName, String tag) {
273292
return result;
274293
}
275294

295+
private static String getCommonPath(String path1, String path2) {
296+
final String[] parts1 = StringUtils.split(path1, "/");
297+
final String[] parts2 = StringUtils.split(path2, "/");
298+
StringBuilder builder = new StringBuilder();
299+
for (int i = 0; i < Math.min(parts1.length, parts2.length); i++) {
300+
if (!parts1[i].equals(parts2[i])) {
301+
break;
302+
}
303+
builder.append("/").append(parts1[i]);
304+
}
305+
return builder.toString();
306+
}
307+
276308
private String implFileFolder(String output) {
277309
return outputFolder + "/" + output + "/" + apiPackage().replace('.', '/');
278310
}
@@ -281,5 +313,8 @@ public void setUseBeanValidation(boolean useBeanValidation) {
281313
this.useBeanValidation = useBeanValidation;
282314
}
283315

284-
316+
@VisibleForTesting
317+
public void setUseTags(boolean useTags) {
318+
this.useTags = useTags;
319+
}
285320
}

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,15 @@ public JavaCXFServerCodegen() {
7272
super();
7373

7474
supportsInheritance = true;
75+
useTags = true;
7576

7677
artifactId = "openapi-cxf-server";
7778

7879
outputFolder = "generated-code/JavaJaxRS-CXF";
7980

8081
// clioOptions default redifinition need to be updated
8182
updateOption(CodegenConstants.ARTIFACT_ID, this.getArtifactId());
83+
updateOption(USE_TAGS, String.valueOf(true));
8284

8385
apiTemplateFiles.put("apiServiceImpl.mustache", ".java");
8486

@@ -234,12 +236,6 @@ public String getName() {
234236
return "jaxrs-cxf";
235237
}
236238

237-
@Override
238-
public void addOperationToGroup(String tag, String resourcePath, Operation operation, CodegenOperation co, Map<String, List<CodegenOperation>> operations) {
239-
super.addOperationToGroup(tag, resourcePath, operation, co, operations);
240-
co.subresourceOperation = !co.path.isEmpty();
241-
}
242-
243239
@Override
244240
public void postProcessModelProperty(CodegenModel model, CodegenProperty property) {
245241
super.postProcessModelProperty(model, property);

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

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,14 @@
1717

1818
package org.openapitools.codegen.languages;
1919

20-
import io.swagger.v3.oas.models.Operation;
2120
import io.swagger.v3.oas.models.media.Schema;
2221
import org.apache.commons.lang3.StringUtils;
2322
import org.openapitools.codegen.*;
2423
import org.openapitools.codegen.meta.features.DocumentationFeature;
2524

2625
import java.io.File;
27-
import java.util.ArrayList;
28-
import java.util.List;
2926
import java.util.Map;
3027

31-
import static org.openapitools.codegen.utils.StringUtils.camelize;
32-
3328
public class JavaJAXRSSpecServerCodegen extends AbstractJavaJAXRSServerCodegen {
3429

3530
public static final String INTERFACE_ONLY = "interfaceOnly";
@@ -54,8 +49,6 @@ public class JavaJAXRSSpecServerCodegen extends AbstractJavaJAXRSServerCodegen {
5449
private boolean useJackson = false;
5550
private String openApiSpecFileLocation = "src/main/openapi/openapi.yaml";
5651

57-
private String primaryResourceName;
58-
5952
public JavaJAXRSSpecServerCodegen() {
6053
super();
6154

@@ -240,41 +233,6 @@ public void setOpenApiSpecFileLocation(String location) {
240233
this.openApiSpecFileLocation = location;
241234
}
242235

243-
@Override
244-
public void addOperationToGroup(String tag, String resourcePath, Operation operation, CodegenOperation co, Map<String, List<CodegenOperation>> operations) {
245-
String basePath = resourcePath;
246-
if (basePath.startsWith("/")) {
247-
basePath = basePath.substring(1);
248-
}
249-
int pos = basePath.indexOf("/");
250-
if (pos > 0) {
251-
basePath = basePath.substring(0, pos);
252-
}
253-
254-
String operationKey = basePath;
255-
if (StringUtils.isEmpty(basePath)) {
256-
basePath = tag;
257-
operationKey = "";
258-
primaryResourceName = tag;
259-
} else if (basePath.matches("\\{.*\\}")) {
260-
basePath = tag;
261-
operationKey = "";
262-
co.subresourceOperation = true;
263-
} else {
264-
if (co.path.startsWith("/" + basePath)) {
265-
co.path = co.path.substring(("/" + basePath).length());
266-
}
267-
co.subresourceOperation = !co.path.isEmpty();
268-
}
269-
List<CodegenOperation> opList = operations.get(operationKey);
270-
if (opList == null || opList.isEmpty()) {
271-
opList = new ArrayList<CodegenOperation>();
272-
operations.put(operationKey, opList);
273-
}
274-
opList.add(co);
275-
co.baseName = basePath;
276-
}
277-
278236
@Override
279237
public CodegenModel fromModel(String name, Schema model) {
280238
CodegenModel codegenModel = super.fromModel(name, model);
@@ -302,16 +260,4 @@ public String getHelp() {
302260
return "Generates a Java JAXRS Server according to JAXRS 2.0 specification.";
303261
}
304262

305-
@Override
306-
public String toApiName(final String name) {
307-
String computed = name;
308-
if (computed.length() == 0) {
309-
if (primaryResourceName == null) {
310-
return "DefaultApi";
311-
}
312-
return primaryResourceName + "Api";
313-
}
314-
computed = sanitizeName(computed);
315-
return camelize(computed) + "Api";
316-
}
317263
}

0 commit comments

Comments
 (0)