Skip to content

Commit

Permalink
[java-jaxrs] Fix paths when useTags=true is used (OpenAPITools#437)
Browse files Browse the repository at this point in the history
* Add test case for the existing implementation
* Introduce {{commonPath}}
* Update samples
  • Loading branch information
jmini authored Jul 3, 2018
1 parent b110eee commit e77b744
Show file tree
Hide file tree
Showing 16 changed files with 397 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.URL;
import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
Expand Down Expand Up @@ -154,6 +154,8 @@ static Map<String, Object> jaxrsPostProcessOperations(Map<String, Object> objs)
@SuppressWarnings("unchecked")
Map<String, Object> operations = (Map<String, Object>) objs.get("operations");
if (operations != null) {
String commonBaseName = null;
boolean baseNameEquals = true;
@SuppressWarnings("unchecked")
List<CodegenOperation> ops = (List<CodegenOperation>) operations.get("operation");
for (CodegenOperation operation : ops) {
Expand Down Expand Up @@ -219,6 +221,23 @@ static Map<String, Object> jaxrsPostProcessOperations(Map<String, Object> objs)
} else if ("map".equals(operation.returnContainer)) {
operation.returnContainer = "Map";
}

if(commonBaseName == null) {
commonBaseName = operation.baseName;
} else if(!commonBaseName.equals(operation.baseName)) {
baseNameEquals = false;
}
}
if(baseNameEquals) {
objs.put("commonPath", commonBaseName);
} else {
for (CodegenOperation operation : ops) {
if(operation.baseName != null) {
operation.path = "/" + operation.baseName + operation.path;
operation.baseName = null;
}
}
objs.put("commonPath", null);
}
}
return objs;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public Map<String, Object> postProcessModelsEnum(Map<String, Object> objs) {
@Override
public void addOperationToGroup(String tag, String resourcePath, Operation operation, CodegenOperation co, Map<String, List<CodegenOperation>> operations) {
if (useTags) {
String basePath = resourcePath;
String basePath = tag.toLowerCase();
if (basePath.startsWith("/")) {
basePath = basePath.substring(1);
}
Expand All @@ -168,11 +168,17 @@ public void addOperationToGroup(String tag, String resourcePath, Operation opera
basePath = basePath.substring(0, pos);
}

if (co.path.startsWith("/" + basePath)) {
boolean pathStartsWithBasePath = co.path.startsWith("/" + basePath);
if (pathStartsWithBasePath) {
co.path = co.path.substring(("/" + basePath).length());
}
co.subresourceOperation = !co.path.isEmpty();
super.addOperationToGroup(tag, resourcePath, operation, co, operations);
if (pathStartsWithBasePath) {
co.baseName = basePath;
} else {
co.baseName = null;
}
} else {
String basePath = resourcePath;
if (basePath.startsWith("/")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import javax.ws.rs.*;
import javax.validation.constraints.*;
{{/useBeanValidation}}

@Path("/{{{baseName}}}")
{{#commonPath}}@Path("/{{{commonPath}}}"){{/commonPath}}{{^commonPath}}@Path(""){{/commonPath}}
{{#hasConsumes}}@Consumes({ {{#consumes}}"{{{mediaType}}}"{{#hasMore}}, {{/hasMore}}{{/consumes}} }){{/hasConsumes}}
{{#hasProduces}}@Produces({ {{#produces}}"{{{mediaType}}}"{{#hasMore}}, {{/hasMore}}{{/produces}} }){{/hasProduces}}
@io.swagger.annotations.Api(description = "the {{{baseName}}} API")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.servers.Server;

import org.openapitools.codegen.MockDefaultGenerator.WrittenTemplateBasedFile;
import org.testng.Assert;

import java.io.File;
import java.util.Collections;
import java.util.Optional;

public class TestUtils {

Expand All @@ -24,4 +29,11 @@ public static OpenAPI createOpenAPI() {
openAPI.setServers(Collections.singletonList(server));
return openAPI;
}

public static WrittenTemplateBasedFile getTemplateBasedFile(MockDefaultGenerator generator, File root, String filename) {
String defaultApiFilename = new File(root, filename).getAbsolutePath().replace("\\", "/");
Optional<WrittenTemplateBasedFile> optional = generator.getTemplateBasedFiles().stream().filter(f -> defaultApiFilename.equals(f.getOutputFilename())).findFirst();
Assert.assertTrue(optional.isPresent());
return optional.get();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.openapitools.codegen.CodegenProperty;
import org.openapitools.codegen.MockDefaultGenerator;
import org.openapitools.codegen.MockDefaultGenerator.WrittenTemplateBasedFile;
import org.openapitools.codegen.TestUtils;
import org.openapitools.codegen.config.CodegenConfigurator;
import org.openapitools.codegen.languages.JavaClientCodegen;
import org.openapitools.codegen.utils.ModelUtils;
Expand All @@ -59,7 +60,6 @@
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

public class JavaClientCodegenTest {
Expand Down Expand Up @@ -406,9 +406,8 @@ public void testGeneratePing() throws Exception {
String defaultApiConent = generatedFiles.get(defaultApiFilename);
Assert.assertTrue(defaultApiConent.contains("public class DefaultApi"));

Optional<WrittenTemplateBasedFile> optional = generator.getTemplateBasedFiles().stream().filter(f -> defaultApiFilename.equals(f.getOutputFilename())).findFirst();
Assert.assertTrue(optional.isPresent());
Assert.assertEquals(optional.get().getTemplateData().get("classname"), "DefaultApi");
WrittenTemplateBasedFile templateBasedFile = TestUtils.getTemplateBasedFile(generator, output, "src/main/java/xyz/abcdef/api/DefaultApi.java");
Assert.assertEquals(templateBasedFile.getTemplateData().get("classname"), "DefaultApi");

output.deleteOnExit();
}
Expand Down
Loading

0 comments on commit e77b744

Please sign in to comment.