Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
ahri opened this issue Apr 3, 2018 · 7 comments
Closed

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

ahri opened this issue Apr 3, 2018 · 7 comments
Assignees

Comments

@ahri
Copy link

ahri commented Apr 3, 2018

Description

The spec for a Server Object states that the URL is "A URL to the target host. This URL supports Server Variables and MAY be relative, to indicate that the host location is relative to the location where the OpenAPI document is being served" (emphasis mine).

The generator does not support relative URLs:

[Thread-0] WARN io.swagger.codegen.utils.URLPathUtil - Not valid URL: api
java.net.MalformedURLException: no protocol: api
	at java.base/java.net.URL.<init>(URL.java:627)
	at java.base/java.net.URL.<init>(URL.java:523)
	at java.base/java.net.URL.<init>(URL.java:470)
	at io.swagger.codegen.utils.URLPathUtil.getServerURL(URLPathUtil.java:30)
	at io.swagger.codegen.DefaultGenerator.buildSupportFileBundle(DefaultGenerator.java:673)
	at io.swagger.codegen.DefaultGenerator.generate(DefaultGenerator.java:734)
	at io.swagger.codegen.cmd.Generate.run(Generate.java:331)
	at java.base/java.lang.Thread.run(Thread.java:844)
Swagger-codegen version

3.0.0-SNAPSHOT (swagger-codegen-cli-3.0.0-20180328.231509-53.jar)

Swagger declaration file content or url
"servers":[{"url":"api"}], ...
Command line used for generation
$ java -jar ~/swagger-codegen-cli-3.0.0-20180328.231509-53.jar generate -i http://localhost:48781/v1/openapi.json -l java -o /tmp/test/
Steps to reproduce

Specify a relative URL for a server object definition and provide the codegen tool with a URL to the OpenAPI JSON file, e.g. http://foo.com/openapi.json specifying

"servers":[{"url":"api"}], ...

and expecting the URL to be expanded to http://foo.com/api.

Related issues/PRs

Did not find related issues.

Suggest a fix/enhancement

My reading of the spec is that since a relative URL is a valid value for the url field, it should be used, along with the input file provided to the codegen (i.e. via the -i switch) to derive a new URL.

This will allow OpenAPI files to be served from behind proxies/load balancers where the actual server cannot know its public address with certainty (X-Forwarded-Host can be wrong).

@HugoMario HugoMario self-assigned this Apr 4, 2018
@ahri
Copy link
Author

ahri commented Apr 4, 2018

At first glance it seems like this line could be altered to:

            return new URL(config.getInputSpec())
                    .toURI()
                    .resolve(url)
                    .toURL();

if getScheme() passes along its CodegenConfig object. This does assume that the spec is a URL, too though; I don't know if local filenames are supposed to be supported here?

@tkingsbury42
Copy link

I'm running into this issue as well with the latest 3.0.0 snapshot. If I switch back to the latest 2.4.0 snapshot, the problem goes away.

@dementati
Copy link

This issue still appears to be present in 3.0.0.

@deg0nz
Copy link

deg0nz commented Dec 5, 2018

I'm running into the same error.
My API server is behind an NGINX and for https://myserver.example.com/api/v1 I get the error for a MalformedURLException for ///api/v1.

Swagger API on the server is version 2.3.0 and the relative URl on the swagger.json is /api/v1
I'm using 3.0.3 of the codegen.

I worked around it by hard coding the entire server URL into line 32 of URLPathUtil.java as @ahri's fix mentioned above didn't work for me.

@mrjoe7
Copy link

mrjoe7 commented Dec 18, 2018

I noticed that when host and schemes are omitted in swagger: '2.0' definition then basePathWithoutHost in templates is blank string. It's present in all codegen versions 3.0.x (tested up to 3.0.3).

@HugoMario
Copy link
Contributor

this issue has been fixed with PR #9047, so going to close this issue but please let me know if you find something wrong and i'll work on it.

@OldIMP
Copy link

OldIMP commented Dec 2, 2019

R u guys planning to merge this fix to the 2.x master branch any time soon? Thx for the great work!

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants