Skip to content
GitLab
    • Explore Projects Groups Snippets
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • O openapi-generator
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 3,476
    • Issues 3,476
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 402
    • Merge requests 402
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • OpenAPI Tools
  • openapi-generator
  • Merge requests
  • !10057

Fix relative url as basePath

  • Review changes

  • Download
  • Email patches
  • Plain diff
Merged Administrator requested to merge github/fork/code1x1/fix-relative-url-as-base-url into master 3 years ago
  • Overview 0
  • Commits 6
  • Pipelines 0
  • Changes 4

Created by: code1x1

fixes #2731 (closed) fixes #8576 (closed) fixes #10697 (closed) fixes #10056 (closed)

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package 
    ./bin/generate-samples.sh
    ./bin/utils/export_docs_generators.sh
    Commit all changed files. This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master. These must match the expectations made by your contribution. You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*. For Windows users, please run the script in Git BASH.
  • File the PR against the correct branch: master, 5.3.x, 6.0.x
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.
Compare
  • master (base)

and
  • latest version
    05c6852f
    6 commits, 2 years ago

4 files
+ 52
- 1

    Preferences

    File browser
    Compare changes
modules/openap‎i-generator/src‎
main/java/org/ope‎napitools/codegen‎
ut‎ils‎
URLPathU‎tils.java‎ +8 -0
DefaultGen‎erator.java‎ +5 -1
te‎st‎
java/org/opena‎pitools/codegen‎
DefaultGener‎atorTest.java‎ +24 -0
resour‎ces/3_0‎
issue_10‎056.yaml‎ +15 -0
modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/URLPathUtils.java
+ 8
- 0
  • View file @ 05c6852f

  • Edit in single-file editor

  • Open in Web IDE


@@ -232,4 +232,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;
}
}
modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultGenerator.java
+ 5
- 1
  • View file @ 05c6852f

  • Edit in single-file editor

  • Open in Web IDE


@@ -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() {
modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultGeneratorTest.java
+ 24
- 0
  • View file @ 05c6852f

  • Edit in single-file editor

  • Open in Web IDE


@@ -662,6 +662,30 @@ public class DefaultGeneratorTest {
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() {
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 {
modules/openapi-generator/src/test/resources/3_0/issue_10056.yaml 0 → 100644
+ 15
- 0
  • View file @ 05c6852f

  • Edit in single-file editor

  • Open in Web IDE

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: {}
\ No newline at end of file
0 Assignees
None
Assign to
0 Reviewers
None
Request review from
Labels
2
Feature: Generator Issue: Bug
2
Feature: Generator Issue: Bug
    Assign labels
  • Manage project labels

Milestone
5.3.1
5.3.1 (expired)
None
Time tracking
No estimate or time spent
Lock merge request
Unlocked
2
2 participants
Administrator
William Cheng
Reference: OpenAPITools/openapi-generator!10057
Source branch: github/fork/code1x1/fix-relative-url-as-base-url

Menu

Explore Projects Groups Snippets