mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-12-11 09:52:44 +00:00
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 <justin.a.black@gmail.com> Co-authored-by: William Cheng <wing328hk@gmail.com>
This commit is contained in:
@@ -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<File> files, List<Object> allOperations, List<Object> allModels) {
|
||||
void generateApis(List<File> files, List<Object> allOperations, List<Object> 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<String, Object> 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<String, Object> buildSupportFileBundle(List<Object> allOperations, List<Object> allModels) {
|
||||
Map<String, Object> buildSupportFileBundle(List<Object> allOperations, List<Object> allModels) {
|
||||
|
||||
Map<String, Object> bundle = new HashMap<>(config.additionalProperties());
|
||||
bundle.put("apiPackage", config.apiPackage());
|
||||
@@ -806,6 +806,7 @@ public class DefaultGenerator implements Generator {
|
||||
|
||||
List<CodegenServer> 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, "/");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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<File> files = new ArrayList<>();
|
||||
List<String> filteredSchemas = ModelUtils.getSchemasUsedOnlyInFormParam(openAPI);
|
||||
List<Object> allModels = new ArrayList<>();
|
||||
generator.generateModels(files, allModels, filteredSchemas);
|
||||
List<Object> allOperations = new ArrayList<>();
|
||||
generator.generateApis(files, allOperations, allModels);
|
||||
|
||||
Map<String, Object> bundle = generator.buildSupportFileBundle(allOperations, allModels);
|
||||
LinkedList<CodegenServer> servers = (LinkedList<CodegenServer>) 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");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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: {}
|
||||
@@ -72,7 +72,7 @@ public class ApiClient extends JavaTimeFormatter {
|
||||
|
||||
protected List<ServerConfiguration> servers = new ArrayList<ServerConfiguration>(Arrays.asList(
|
||||
new ServerConfiguration(
|
||||
"/",
|
||||
"",
|
||||
"No description provided",
|
||||
new HashMap<String, ServerVariable>()
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user