Skip to content

Commit 57227e5

Browse files
ksvirkou-hubspotspacetherwing328
authored
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 <[email protected]> Co-authored-by: William Cheng <[email protected]>
1 parent c5d4dc6 commit 57227e5

File tree

4 files changed

+57
-6
lines changed

4 files changed

+57
-6
lines changed

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -266,9 +266,9 @@ void configureGeneratorProperties() {
266266
// TODO: Allow user to define _which_ servers object in the array to target.
267267
// Configures contextPath/basePath according to api document's servers
268268
URL url = URLPathUtils.getServerURL(openAPI, config.serverVariableOverrides());
269-
contextPath = config.escapeText(url.getPath()).replaceAll("/$", ""); // for backward compatibility
269+
contextPath = removeTrailingSlash(config.escapeText(url.getPath())); // for backward compatibility
270270
basePathWithoutHost = contextPath;
271-
basePath = config.escapeText(URLPathUtils.getHost(openAPI, config.serverVariableOverrides())).replaceAll("/$", "");
271+
basePath = removeTrailingSlash(config.escapeText(URLPathUtils.getHost(openAPI, config.serverVariableOverrides())));
272272
}
273273

274274
private void configureOpenAPIInfo() {
@@ -552,7 +552,7 @@ void generateModels(List<File> files, List<Object> allModels, List<String> unuse
552552
}
553553

554554
@SuppressWarnings("unchecked")
555-
private void generateApis(List<File> files, List<Object> allOperations, List<Object> allModels) {
555+
void generateApis(List<File> files, List<Object> allOperations, List<Object> allModels) {
556556
if (!generateApis) {
557557
// TODO: Process these anyway and present info via dryRun?
558558
LOGGER.info("Skipping generation of APIs.");
@@ -580,7 +580,7 @@ private void generateApis(List<File> files, List<Object> allOperations, List<Obj
580580
Map<String, Object> operation = processOperations(config, tag, ops, allModels);
581581
URL url = URLPathUtils.getServerURL(openAPI, config.serverVariableOverrides());
582582
operation.put("basePath", basePath);
583-
operation.put("basePathWithoutHost", config.encodePath(url.getPath()).replaceAll("/$", ""));
583+
operation.put("basePathWithoutHost", removeTrailingSlash(config.encodePath(url.getPath())));
584584
operation.put("contextPath", contextPath);
585585
operation.put("baseName", tag);
586586
operation.put("apiPackage", config.apiPackage());
@@ -756,7 +756,7 @@ private void generateSupportingFiles(List<File> files, Map<String, Object> bundl
756756
}
757757

758758
@SuppressWarnings("unchecked")
759-
private Map<String, Object> buildSupportFileBundle(List<Object> allOperations, List<Object> allModels) {
759+
Map<String, Object> buildSupportFileBundle(List<Object> allOperations, List<Object> allModels) {
760760

761761
Map<String, Object> bundle = new HashMap<>(config.additionalProperties());
762762
bundle.put("apiPackage", config.apiPackage());
@@ -806,6 +806,7 @@ private Map<String, Object> buildSupportFileBundle(List<Object> allOperations, L
806806

807807
List<CodegenServer> servers = config.fromServers(openAPI.getServers());
808808
if (servers != null && !servers.isEmpty()) {
809+
servers.forEach(server -> server.url = removeTrailingSlash(server.url));
809810
bundle.put("servers", servers);
810811
bundle.put("hasServers", true);
811812
}
@@ -1483,4 +1484,8 @@ private void generateFilesMetadata(List<File> files) {
14831484
}
14841485
}
14851486

1487+
private String removeTrailingSlash(String value) {
1488+
return StringUtils.removeEnd(value, "/");
1489+
}
1490+
14861491
}

modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultGeneratorTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@
1111
import io.swagger.v3.oas.models.parameters.RequestBody;
1212
import io.swagger.v3.oas.models.responses.ApiResponse;
1313
import io.swagger.v3.oas.models.responses.ApiResponses;
14+
import org.apache.commons.lang3.StringUtils;
1415
import org.openapitools.codegen.config.CodegenConfigurator;
1516
import org.openapitools.codegen.config.GlobalSettings;
17+
import org.openapitools.codegen.meta.GeneratorMetadata;
18+
import org.openapitools.codegen.meta.Stability;
1619
import org.openapitools.codegen.utils.ModelUtils;
1720
import org.testng.Assert;
1821
import org.testng.annotations.Test;
@@ -636,5 +639,31 @@ public void testCustomNonLibraryTemplates() throws IOException {
636639
templates.toFile().delete();
637640
}
638641
}
642+
643+
@Test
644+
public void testHandlesTrailingSlashInServers() {
645+
OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/issue_7533.yaml");
646+
ClientOptInput opts = new ClientOptInput();
647+
opts.openAPI(openAPI);
648+
DefaultCodegen config = new DefaultCodegen();
649+
config.setStrictSpecBehavior(false);
650+
opts.config(config);
651+
final DefaultGenerator generator = new DefaultGenerator();
652+
generator.opts(opts);
653+
generator.configureGeneratorProperties();
654+
655+
List<File> files = new ArrayList<>();
656+
List<String> filteredSchemas = ModelUtils.getSchemasUsedOnlyInFormParam(openAPI);
657+
List<Object> allModels = new ArrayList<>();
658+
generator.generateModels(files, allModels, filteredSchemas);
659+
List<Object> allOperations = new ArrayList<>();
660+
generator.generateApis(files, allOperations, allModels);
661+
662+
Map<String, Object> bundle = generator.buildSupportFileBundle(allOperations, allModels);
663+
LinkedList<CodegenServer> servers = (LinkedList<CodegenServer>) bundle.get("servers");
664+
Assert.assertEquals(servers.get(0).url, "");
665+
Assert.assertEquals(servers.get(1).url, "http://trailingshlash.io:80/v1");
666+
Assert.assertEquals(servers.get(2).url, "http://notrailingslash.io:80/v2");
667+
}
639668
}
640669

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
openapi: 3.0.1
2+
info:
3+
title: OpenAPI Petstore
4+
description: "sample spec"
5+
license:
6+
name: Apache-2.0
7+
url: https://www.apache.org/licenses/LICENSE-2.0.html
8+
version: 1.0.0
9+
servers:
10+
- url: /
11+
- url: http://trailingshlash.io:80/v1/
12+
- url: http://notrailingslash.io:80/v2
13+
tags: []
14+
paths: {}
15+
components:
16+
schemas: {}
17+
securitySchemes: {}

samples/openapi3/client/petstore/java/jersey2-java8-special-characters/src/main/java/org/openapitools/client/ApiClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public class ApiClient extends JavaTimeFormatter {
7272

7373
protected List<ServerConfiguration> servers = new ArrayList<ServerConfiguration>(Arrays.asList(
7474
new ServerConfiguration(
75-
"/",
75+
"",
7676
"No description provided",
7777
new HashMap<String, ServerVariable>()
7878
)

0 commit comments

Comments
 (0)