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

fix for issue invalid remote server url #1412

Merged
merged 1 commit into from
Aug 1, 2020
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 @@ -406,6 +406,14 @@ public Server getServer(ObjectNode obj, String location, ParseResult result, Str

Server server = new Server();

if (obj.get("variables") != null) {
ObjectNode variables = getObject("variables", obj, false, location, result);
ServerVariables serverVariables = getServerVariables(variables, String.format("%s.%s", location, "variables"), result);
if (serverVariables != null && serverVariables.size() > 0) {
server.setVariables(serverVariables);
}
}

String value = getString("url", obj, true, location, result);
if(StringUtils.isNotBlank(value)) {
if(!isValidURL(value) && path != null){
Expand All @@ -415,9 +423,13 @@ public Server getServer(ObjectNode obj, String location, ParseResult result, Str
value = absURI.resolve(new URI(value)).toString();
}
} catch (URISyntaxException e) {
result.warning(location,"invalid url : "+value);
String variable = value.substring(value.indexOf("{")+1,value.indexOf("}"));
if (server.getVariables() != null) {
if (!server.getVariables().containsKey(variable)) {
result.warning(location, "invalid url : " + value);
}
}
}

}
server.setUrl(value);
}
Expand All @@ -426,14 +438,6 @@ public Server getServer(ObjectNode obj, String location, ParseResult result, Str
if(StringUtils.isNotBlank(value)) {
server.setDescription(value);
}
if (obj.get("variables") != null) {
ObjectNode variables = getObject("variables", obj, false, location, result);
ServerVariables serverVariables = getServerVariables(variables, String.format("%s.%s", location, "variables"), result);
if (serverVariables != null && serverVariables.size() > 0) {
server.setVariables(serverVariables);
}
}


Map <String,Object> extensions = getExtensions(obj);
if(extensions != null && extensions.size() > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import io.swagger.v3.oas.models.parameters.RequestBody;
import io.swagger.v3.parser.OpenAPIV3Parser;
import io.swagger.v3.parser.core.models.AuthorizationValue;
import io.swagger.v3.parser.core.models.ParseOptions;
import io.swagger.v3.parser.core.models.SwaggerParseResult;
import io.swagger.v3.parser.util.RemoteUrl;
import mockit.Expectations;
import mockit.Mocked;
Expand All @@ -15,6 +17,7 @@
import java.util.Arrays;

import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertTrue;


public class RelativeReferenceTest {
Expand All @@ -25,6 +28,14 @@ public class RelativeReferenceTest {
"openapi: 3.0.0\n" +
"servers:\n" +
" - url: /\n" +
" - url: https://localhost:8080/{version}\n" +
" description: The local server\n" +
" variables:\n" +
" version:\n" +
" default: v2\n" +
" enum:\n" +
" - v1\n" +
" - v2\n"+
"info:\n" +
" description: It works.\n" +
" version: 1.0.0\n" +
Expand All @@ -44,6 +55,19 @@ public class RelativeReferenceTest {
" type: object\n" +
" required: true";

@Test
public void testIssueServerUrlValidation() throws Exception {
new Expectations() {{
RemoteUrl.urlToString("http://foo.bar.com/swagger.json", Arrays.asList(new AuthorizationValue[]{}));
times = 1;
result = spec;
}};

SwaggerParseResult swaggerParseResult = new OpenAPIV3Parser().readLocation("http://foo.bar.com/swagger.json", null, new ParseOptions());
assertNotNull(swaggerParseResult.getOpenAPI());
assertTrue(swaggerParseResult.getMessages().isEmpty());
}

@Test
public void testIssue213() throws Exception {
new Expectations() {{
Expand Down