From 57227e510f8b0be99b2fa1ba7b23e9d5573f8c3a Mon Sep 17 00:00:00 2001 From: Kuzma <57258237+ksvirkou-hubspot@users.noreply.github.com> Date: Mon, 25 Jan 2021 08:32:39 +0300 Subject: [PATCH] Remove servers urls with trailing slash (#7940) * remove trailing slash * update sample app * added tests * bug fix * Adds test in DefaultGeneratorTest * Reverts python files * Does not modify a value of / * Stops skipping / use case * update samples Co-authored-by: Justin Black Co-authored-by: William Cheng --- .../codegen/DefaultGenerator.java | 15 ++++++---- .../codegen/DefaultGeneratorTest.java | 29 +++++++++++++++++++ .../src/test/resources/3_0/issue_7533.yaml | 17 +++++++++++ .../org/openapitools/client/ApiClient.java | 2 +- 4 files changed, 57 insertions(+), 6 deletions(-) create mode 100644 modules/openapi-generator/src/test/resources/3_0/issue_7533.yaml diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultGenerator.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultGenerator.java index 67bee20dbe0..e752c710c94 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultGenerator.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultGenerator.java @@ -266,9 +266,9 @@ public class DefaultGenerator implements Generator { // TODO: Allow user to define _which_ servers object in the array to target. // Configures contextPath/basePath according to api document's servers URL url = URLPathUtils.getServerURL(openAPI, config.serverVariableOverrides()); - contextPath = config.escapeText(url.getPath()).replaceAll("/$", ""); // for backward compatibility + contextPath = removeTrailingSlash(config.escapeText(url.getPath())); // for backward compatibility basePathWithoutHost = contextPath; - basePath = config.escapeText(URLPathUtils.getHost(openAPI, config.serverVariableOverrides())).replaceAll("/$", ""); + basePath = removeTrailingSlash(config.escapeText(URLPathUtils.getHost(openAPI, config.serverVariableOverrides()))); } private void configureOpenAPIInfo() { @@ -552,7 +552,7 @@ public class DefaultGenerator implements Generator { } @SuppressWarnings("unchecked") - private void generateApis(List files, List allOperations, List allModels) { + void generateApis(List files, List allOperations, List allModels) { if (!generateApis) { // TODO: Process these anyway and present info via dryRun? LOGGER.info("Skipping generation of APIs."); @@ -580,7 +580,7 @@ public class DefaultGenerator implements Generator { Map operation = processOperations(config, tag, ops, allModels); URL url = URLPathUtils.getServerURL(openAPI, config.serverVariableOverrides()); operation.put("basePath", basePath); - operation.put("basePathWithoutHost", config.encodePath(url.getPath()).replaceAll("/$", "")); + operation.put("basePathWithoutHost", removeTrailingSlash(config.encodePath(url.getPath()))); operation.put("contextPath", contextPath); operation.put("baseName", tag); operation.put("apiPackage", config.apiPackage()); @@ -756,7 +756,7 @@ public class DefaultGenerator implements Generator { } @SuppressWarnings("unchecked") - private Map buildSupportFileBundle(List allOperations, List allModels) { + Map buildSupportFileBundle(List allOperations, List allModels) { Map bundle = new HashMap<>(config.additionalProperties()); bundle.put("apiPackage", config.apiPackage()); @@ -806,6 +806,7 @@ public class DefaultGenerator implements Generator { List servers = config.fromServers(openAPI.getServers()); if (servers != null && !servers.isEmpty()) { + servers.forEach(server -> server.url = removeTrailingSlash(server.url)); bundle.put("servers", servers); bundle.put("hasServers", true); } @@ -1483,4 +1484,8 @@ public class DefaultGenerator implements Generator { } } + private String removeTrailingSlash(String value) { + return StringUtils.removeEnd(value, "/"); + } + } diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultGeneratorTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultGeneratorTest.java index ad9f062ee56..7a22776af35 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultGeneratorTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultGeneratorTest.java @@ -11,8 +11,11 @@ import io.swagger.v3.oas.models.parameters.QueryParameter; import io.swagger.v3.oas.models.parameters.RequestBody; import io.swagger.v3.oas.models.responses.ApiResponse; import io.swagger.v3.oas.models.responses.ApiResponses; +import org.apache.commons.lang3.StringUtils; import org.openapitools.codegen.config.CodegenConfigurator; import org.openapitools.codegen.config.GlobalSettings; +import org.openapitools.codegen.meta.GeneratorMetadata; +import org.openapitools.codegen.meta.Stability; import org.openapitools.codegen.utils.ModelUtils; import org.testng.Assert; import org.testng.annotations.Test; @@ -636,5 +639,31 @@ public class DefaultGeneratorTest { templates.toFile().delete(); } } + + @Test + public void testHandlesTrailingSlashInServers() { + OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/issue_7533.yaml"); + ClientOptInput opts = new ClientOptInput(); + opts.openAPI(openAPI); + DefaultCodegen config = new DefaultCodegen(); + config.setStrictSpecBehavior(false); + opts.config(config); + final DefaultGenerator generator = new DefaultGenerator(); + generator.opts(opts); + generator.configureGeneratorProperties(); + + List files = new ArrayList<>(); + List filteredSchemas = ModelUtils.getSchemasUsedOnlyInFormParam(openAPI); + List allModels = new ArrayList<>(); + generator.generateModels(files, allModels, filteredSchemas); + List allOperations = new ArrayList<>(); + generator.generateApis(files, allOperations, allModels); + + Map bundle = generator.buildSupportFileBundle(allOperations, allModels); + LinkedList servers = (LinkedList) bundle.get("servers"); + Assert.assertEquals(servers.get(0).url, ""); + Assert.assertEquals(servers.get(1).url, "http://trailingshlash.io:80/v1"); + Assert.assertEquals(servers.get(2).url, "http://notrailingslash.io:80/v2"); + } } diff --git a/modules/openapi-generator/src/test/resources/3_0/issue_7533.yaml b/modules/openapi-generator/src/test/resources/3_0/issue_7533.yaml new file mode 100644 index 00000000000..7d66422f558 --- /dev/null +++ b/modules/openapi-generator/src/test/resources/3_0/issue_7533.yaml @@ -0,0 +1,17 @@ +openapi: 3.0.1 +info: + title: OpenAPI Petstore + description: "sample spec" + license: + name: Apache-2.0 + url: https://www.apache.org/licenses/LICENSE-2.0.html + version: 1.0.0 +servers: + - url: / + - url: http://trailingshlash.io:80/v1/ + - url: http://notrailingslash.io:80/v2 +tags: [] +paths: {} +components: + schemas: {} + securitySchemes: {} \ No newline at end of file diff --git a/samples/openapi3/client/petstore/java/jersey2-java8-special-characters/src/main/java/org/openapitools/client/ApiClient.java b/samples/openapi3/client/petstore/java/jersey2-java8-special-characters/src/main/java/org/openapitools/client/ApiClient.java index 838e1910886..8b3dbdcebb0 100644 --- a/samples/openapi3/client/petstore/java/jersey2-java8-special-characters/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/openapi3/client/petstore/java/jersey2-java8-special-characters/src/main/java/org/openapitools/client/ApiClient.java @@ -72,7 +72,7 @@ public class ApiClient extends JavaTimeFormatter { protected List servers = new ArrayList(Arrays.asList( new ServerConfiguration( - "/", + "", "No description provided", new HashMap() )