Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,11 @@ void configureGeneratorProperties() {
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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,4 +232,12 @@ private static URL getDefaultUrl() {
return null;
}
}

public static boolean isRelativeUrl(List<Server> servers) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

replace tabs with spaces

if (servers != null && servers.size() > 0) {
final Server firstServer = servers.get(0);
return Pattern.matches("^(\\/[\\w\\d]+)+", firstServer.getUrl());
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,30 @@ public void testHandlesTrailingSlashInServers() {
Assert.assertEquals(servers.get(1).url, "http://trailingshlash.io:80/v1");
Assert.assertEquals(servers.get(2).url, "http://notrailingslash.io:80/v2");
}

@Test
public void testHandlesRelativeUrlsInServers() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

method configureGeneratorProperties is package protected, you didn't need to write this whole test with setup of new yaml file

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 {
Expand Down
15 changes: 15 additions & 0 deletions modules/openapi-generator/src/test/resources/3_0/issue_10056.yaml
Original file line number Diff line number Diff line change
@@ -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: {}