Search in sources :

Example 1 with AuthorizationValue

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();
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) KeyManagementException(java.security.KeyManagementException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) AuthorizationValue(io.swagger.v3.parser.core.models.AuthorizationValue) BufferedReader(java.io.BufferedReader)

Example 2 with AuthorizationValue

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);
}
Also used : Expectations(mockit.Expectations) StringSchema(io.swagger.v3.oas.models.media.StringSchema) Schema(io.swagger.v3.oas.models.media.Schema) ArrayList(java.util.ArrayList) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) AuthorizationValue(io.swagger.v3.parser.core.models.AuthorizationValue) PathItem(io.swagger.v3.oas.models.PathItem) ParseOptions(io.swagger.v3.parser.core.models.ParseOptions) Parameter(io.swagger.v3.oas.models.parameters.Parameter) StringSchema(io.swagger.v3.oas.models.media.StringSchema) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 3 with AuthorizationValue

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());
}
Also used : ParseOptions(io.swagger.v3.parser.core.models.ParseOptions) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 4 with AuthorizationValue

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");
}
Also used : ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) ParseOptions(io.swagger.v3.parser.core.models.ParseOptions) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 5 with AuthorizationValue

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);
}
Also used : ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) Schema(io.swagger.v3.oas.models.media.Schema) ParseOptions(io.swagger.v3.parser.core.models.ParseOptions) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Aggregations

Test (org.testng.annotations.Test)44 OpenAPIV3Parser (io.swagger.v3.parser.OpenAPIV3Parser)40 ParseOptions (io.swagger.v3.parser.core.models.ParseOptions)34 OpenAPI (io.swagger.v3.oas.models.OpenAPI)31 SwaggerParseResult (io.swagger.v3.parser.core.models.SwaggerParseResult)21 ArraySchema (io.swagger.v3.oas.models.media.ArraySchema)18 ComposedSchema (io.swagger.v3.oas.models.media.ComposedSchema)18 ObjectSchema (io.swagger.v3.oas.models.media.ObjectSchema)17 Schema (io.swagger.v3.oas.models.media.Schema)17 StringSchema (io.swagger.v3.oas.models.media.StringSchema)17 IntegerSchema (io.swagger.v3.oas.models.media.IntegerSchema)16 AuthorizationValue (io.swagger.v3.parser.core.models.AuthorizationValue)13 ByteArraySchema (io.swagger.v3.oas.models.media.ByteArraySchema)8 MapSchema (io.swagger.v3.oas.models.media.MapSchema)8 HashSet (java.util.HashSet)7 BinarySchema (io.swagger.v3.oas.models.media.BinarySchema)5 DateSchema (io.swagger.v3.oas.models.media.DateSchema)5 DateTimeSchema (io.swagger.v3.oas.models.media.DateTimeSchema)5 ArrayList (java.util.ArrayList)5 Expectations (mockit.Expectations)5