swagger-api / swagger-api/swagger-codegen

[Codegen] Spec not adhered to wrt. relative Server URLs

Open
#9,215 0 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

Using a relative server url in the swagger file produces a java.net.MalformedURLException: no protocol

Swagger-codegen version

io.swagger.codegen.v3:swagger-codegen-maven-plugin:3.0.5
io.swagger.codegen.v3:swagger-codegen:3.0.5

Swagger declaration file content or url
openapi: "3.0.0"
info:
  version: 1.0.0
  title: 'Some title'
  description: Some description
  termsOfService: 'http://www.terms.com'
  contact:
    email: contact@email.com
  license:
    name: The License
    url: 'http://www.license.com'
servers:
  - url: '/api/v1'
paths:
..........

-->

Command line used for generation

mvn clean package

Steps to reproduce
  1. add an openapi swagger file with a relative path url
  2. use the io.swagger.codegen.v3:swagger-codegen-maven-plugin:3.0.5 plugin
  3. run mvn clean package

The output is:

[WARNING] Not valid URL: /api/v1
java.net.MalformedURLException: no protocol: /api/v1
	at java.net.URL.<init>(URL.java:606)
	at java.net.URL.<init>(URL.java:498)
	at java.net.URL.<init>(URL.java:447)
	at io.swagger.codegen.v3.utils.URLPathUtil.getServerURL(URLPathUtil.java:31)
	at io.swagger.codegen.v3.utils.URLPathUtil.getScheme(URLPathUtil.java:77)
	at io.swagger.codegen.v3.DefaultGenerator.buildSupportFileBundle(DefaultGenerator.java:673)
	at io.swagger.codegen.v3.DefaultGenerator.generate(DefaultGenerator.java:725)
	at io.swagger.codegen.v3.maven.plugin.CodeGenMojo.execute(CodeGenMojo.java:545)
	at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:134)
	at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:207)
	at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
	at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
	at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116)
	at org.apache.maven.lifecycle.internal.builder.multithreaded.MultiThreadedBuilder$1.call(MultiThreadedBuilder.java:185)
	at org.apache.maven.lifecycle.internal.builder.multithreaded.MultiThreadedBuilder$1.call(MultiThreadedBuilder.java:181)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at java.lang.Thread.run(Thread.java:836)
Related issues/PRs

https://github.com/swagger-api/swagger-codegen/issues/7960

Suggest a fix/enhancement

#7960 fixed the problem in io.swagger.codegen.v3.DefaultGenerator#configureGeneratorProperties (line 202) and in io.swagger.codegen.v3.DefaultGenerator#buildSupportFileBundle (line 664). Now the same issue appears because of the code in io.swagger.codegen.v3.DefaultGenerator#buildSupportFileBundle (line 673), as io.swagger.codegen.v3.utils.URLPathUtil#getScheme calls the io.swagger.codegen.v3.utils.URLPathUtil#getServerURL(io.swagger.v3.oas.models.OpenAPI), instead of io.swagger.codegen.v3.utils.URLPathUtil#getServerURL(io.swagger.v3.oas.models.OpenAPI, io.swagger.codegen.v3.CodegenConfig).

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with URLPathUtil.getServerURL and getScheme, then inspect DefaultGenerator.buildSupportFileBundle and the related overloads referenced in the report. Reproduce the failure with the supplied OpenAPI YAML and mvn clean package; done means a relative server URL is handled without the reported MalformedURLException.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi
Domain
api, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.