#10056 Fix relative url as basePath (#10057)

* #10056 Fix relative url as basePath

* Add tests handle relative urls in servers

* PR Task:
* replace tabs with spaces
* can `servers` be null or empty?

* PR Task: fix empty line tab

* Update DefaultGenerator.java

Fix lines change tabs to spaces

* Fix tabs spaces

Co-authored-by: Denis Behrends <d.behrends@hosting.de>
This commit is contained in:
Code1x1 2021-11-06 18:27:26 +01:00 committed by GitHub
parent 3284fc2f7e
commit 0bb4f186ec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 52 additions and 1 deletions

View File

@ -268,7 +268,11 @@ public class DefaultGenerator implements Generator {
URL url = URLPathUtils.getServerURL(openAPI, config.serverVariableOverrides());
contextPath = removeTrailingSlash(config.escapeText(url.getPath())); // for backward compatibility
basePathWithoutHost = contextPath;
basePath = removeTrailingSlash(config.escapeText(URLPathUtils.getHost(openAPI, config.serverVariableOverrides())));
if (URLPathUtils.isRelativeUrl(openAPI.getServers())) {
basePath = removeTrailingSlash(basePathWithoutHost);
} else {
basePath = removeTrailingSlash(config.escapeText(URLPathUtils.getHost(openAPI, config.serverVariableOverrides())));
}
}
private void configureOpenAPIInfo() {

View File

@ -231,4 +231,12 @@ public class URLPathUtils {
return null;
}
}
public static boolean isRelativeUrl(List<Server> servers) {
if (servers != null && servers.size() > 0) {
final Server firstServer = servers.get(0);
return Pattern.matches("^(\\/[\\w\\d]+)+", firstServer.getUrl());
}
return false;
}
}

View File

@ -663,6 +663,30 @@ public class DefaultGeneratorTest {
Assert.assertEquals(servers.get(2).url, "http://notrailingslash.io:80/v2");
}
@Test
public void testHandlesRelativeUrlsInServers() {
OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/issue_10056.yaml");
ClientOptInput opts = new ClientOptInput();
opts.openAPI(openAPI);
DefaultCodegen config = new DefaultCodegen();
config.setStrictSpecBehavior(true);
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, "/relative/url");
}
@Test
public void testProcessUserDefinedTemplatesWithConfig() throws IOException {
Path target = Files.createTempDirectory("test");

View File

@ -0,0 +1,15 @@
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: /relative/url
tags: []
paths: {}
components:
schemas: {}
securitySchemes: {}