use of io.swagger.v3.parser.core.models.AuthorizationValue in project swagger-parser by swagger-api.
the class RemoteUrl method urlToString.
public static String urlToString(String url, List<AuthorizationValue> auths) throws Exception {
InputStream is = null;
BufferedReader br = null;
try {
URLConnection conn;
do {
final URL inUrl = new URL(cleanUrl(url));
final List<AuthorizationValue> query = new ArrayList<>();
final List<AuthorizationValue> header = new ArrayList<>();
if (auths != null && auths.size() > 0) {
for (AuthorizationValue auth : auths) {
if (auth.getUrlMatcher() != null) {
if (auth.getUrlMatcher().test(inUrl)) {
if ("query".equals(auth.getType())) {
appendValue(inUrl, auth, query);
} else if ("header".equals(auth.getType())) {
appendValue(inUrl, auth, header);
}
}
}
}
}
if (!query.isEmpty()) {
final URI inUri = inUrl.toURI();
final StringBuilder newQuery = new StringBuilder(inUri.getQuery() == null ? "" : inUri.getQuery());
for (AuthorizationValue item : query) {
if (newQuery.length() > 0) {
newQuery.append("&");
}
newQuery.append(URLEncoder.encode(item.getKeyName(), UTF_8.name())).append("=").append(URLEncoder.encode(item.getValue(), UTF_8.name()));
}
conn = new URI(inUri.getScheme(), inUri.getAuthority(), inUri.getPath(), newQuery.toString(), inUri.getFragment()).toURL().openConnection();
} else {
conn = inUrl.openConnection();
}
CONNECTION_CONFIGURATOR.process(conn);
for (AuthorizationValue item : header) {
conn.setRequestProperty(item.getKeyName(), item.getValue());
}
conn.setRequestProperty("Accept", ACCEPT_HEADER_VALUE);
conn.setRequestProperty("User-Agent", USER_AGENT_HEADER_VALUE);
conn.connect();
url = ((HttpURLConnection) conn).getHeaderField("Location");
} while ((301 == ((HttpURLConnection) conn).getResponseCode()) || (302 == ((HttpURLConnection) conn).getResponseCode()) || (307 == ((HttpURLConnection) conn).getResponseCode()) || (308 == ((HttpURLConnection) conn).getResponseCode()));
InputStream in = conn.getInputStream();
StringBuilder contents = new StringBuilder();
BufferedReader input = new BufferedReader(new InputStreamReader(in, UTF_8));
for (int i = 0; i != -1; i = input.read()) {
char c = (char) i;
if (!Character.isISOControl(c)) {
contents.append((char) i);
}
if (c == '\n') {
contents.append('\n');
}
}
in.close();
return contents.toString();
} catch (javax.net.ssl.SSLProtocolException e) {
LOGGER.warn("there is a problem with the target SSL certificate");
LOGGER.warn("**** you may want to run with -Djsse.enableSNIExtension=false\n\n");
LOGGER.error("unable to read", e);
throw e;
} catch (Exception e) {
LOGGER.error("unable to read", e);
throw e;
} finally {
if (is != null) {
is.close();
}
if (br != null) {
br.close();
}
}
}
use of io.swagger.v3.parser.core.models.AuthorizationValue in project swagger-parser by swagger-api.
the class NetworkReferenceTest method testIssue411.
@Test
public void testIssue411() throws Exception {
final List<AuthorizationValue> auths = new ArrayList<>();
AuthorizationValue auth = new AuthorizationValue("Authorization", "OMG_SO_SEKR3T", "header");
auths.add(auth);
new Expectations() {
{
remoteUrl.urlToString("http://remote1/resources/swagger.yaml", auths);
result = issue_411_server;
remoteUrl.urlToString("http://remote2/resources/foo", auths);
result = issue_411_components;
}
};
OpenAPIV3Parser parser = new OpenAPIV3Parser();
ParseOptions options = new ParseOptions();
options.setResolve(true);
SwaggerParseResult result = parser.readLocation("http://remote1/resources/swagger.yaml", auths, options);
OpenAPI swagger = result.getOpenAPI();
assertNotNull(swagger.getPaths().get("/health"));
PathItem health = swagger.getPaths().get("/health");
assertTrue(health.getGet().getParameters().size() == 0);
Schema responseRef = health.getGet().getResponses().get("200").getContent().get("*/*").getSchema();
assertTrue(responseRef.get$ref() != null);
assertEquals(responseRef.get$ref(), "#/components/schemas/Success");
assertNotNull(swagger.getComponents().getSchemas().get("Success"));
Parameter param = swagger.getPaths().get("/stuff").getGet().getParameters().get(0);
assertEquals(param.getIn(), "query");
assertEquals(param.getName(), "skip");
ApiResponse response = swagger.getPaths().get("/stuff").getGet().getResponses().get("200");
assertNotNull(response);
assertTrue(response.getContent().get("*/*").getSchema() instanceof StringSchema);
ApiResponse error = swagger.getPaths().get("/stuff").getGet().getResponses().get("400");
assertNotNull(error);
Schema errorProp = error.getContent().get("*/*").getSchema();
assertNotNull(errorProp);
assertTrue(errorProp.get$ref() != null);
assertEquals(errorProp.get$ref(), "#/components/schemas/Error");
assertTrue(swagger.getComponents().getSchemas().get("Error") instanceof Schema);
}
use of io.swagger.v3.parser.core.models.AuthorizationValue in project swagger-parser by swagger-api.
the class OpenAPIResolverTest method testIssue1352.
@Test
public void testIssue1352(@Injectable final List<AuthorizationValue> auths) {
ParseOptions options = new ParseOptions();
options.setResolve(true);
options.setResolveFully(true);
OpenAPI openAPI = new OpenAPIV3Parser().readLocation("issue-1352.json", auths, options).getOpenAPI();
assertNull(openAPI.getPaths().get("/responses").getPatch().getResponses().get("200").getHeaders().get("x-my-secret-header").getSchema().get$ref());
}
use of io.swagger.v3.parser.core.models.AuthorizationValue in project swagger-parser by swagger-api.
the class OpenAPIResolverTest method resolveComposedSchema.
@Test
public void resolveComposedSchema(@Injectable final List<AuthorizationValue> auths) {
ParseOptions options = new ParseOptions();
options.setResolveCombinators(false);
options.setResolveFully(true);
OpenAPI openAPI = new OpenAPIV3Parser().readLocation("src/test/resources/oneof-anyof.yaml", auths, options).getOpenAPI();
assertTrue(openAPI.getPaths().get("/mixed-array").getGet().getResponses().get("200").getContent().get("application/json").getSchema() instanceof ArraySchema);
ArraySchema arraySchema = (ArraySchema) openAPI.getPaths().get("/mixed-array").getGet().getResponses().get("200").getContent().get("application/json").getSchema();
assertTrue(arraySchema.getItems() instanceof ComposedSchema);
ComposedSchema oneOf = (ComposedSchema) arraySchema.getItems();
assertEquals(oneOf.getOneOf().get(0).getType(), "string");
// System.out.println(openAPI.getPaths().get("/oneOf").getGet().getResponses().get("200").getContent().get("application/json").getSchema() );
assertTrue(openAPI.getPaths().get("/oneOf").getGet().getResponses().get("200").getContent().get("application/json").getSchema() instanceof ComposedSchema);
ComposedSchema oneOfSchema = (ComposedSchema) openAPI.getPaths().get("/oneOf").getGet().getResponses().get("200").getContent().get("application/json").getSchema();
assertEquals(oneOfSchema.getOneOf().get(0).getType(), "object");
}
use of io.swagger.v3.parser.core.models.AuthorizationValue in project swagger-parser by swagger-api.
the class OpenAPIResolverTest method testIssue1157.
@Test
public void testIssue1157(@Injectable final List<AuthorizationValue> auths) {
ParseOptions options = new ParseOptions();
options.setResolve(true);
options.setResolveFully(true);
OpenAPI openAPIAnyOf = new OpenAPIV3Parser().readLocation("/issue-1157/anyOf-example.yaml", auths, options).getOpenAPI();
Schema petSchemaAnyOf = openAPIAnyOf.getComponents().getSchemas().get("Pet");
assertTrue(petSchemaAnyOf instanceof ComposedSchema);
assertTrue(((ComposedSchema) petSchemaAnyOf).getAnyOf() != null);
OpenAPI openAPIOneOf = new OpenAPIV3Parser().readLocation("/issue-1157/oneOf-example.yaml", auths, options).getOpenAPI();
Schema petSchemaOneOf = openAPIOneOf.getComponents().getSchemas().get("Pet");
assertTrue(petSchemaOneOf instanceof ComposedSchema);
assertTrue(((ComposedSchema) petSchemaOneOf).getOneOf() != null);
OpenAPI openAPIAllOf = new OpenAPIV3Parser().readLocation("/issue-1157/allOf-example.yaml", auths, options).getOpenAPI();
Schema petSchemaAllOf = openAPIAllOf.getComponents().getSchemas().get("Pet");
assertFalse(petSchemaAllOf instanceof ComposedSchema);
assertTrue(petSchemaAllOf.getProperties() != null);
}
Aggregations