Search in sources :

Example 11 with AuthorizationValue

use of io.swagger.v3.parser.core.models.AuthorizationValue in project swagger-parser by swagger-api.

the class RemoteUrlTest method testAuthorizationHeader.

@Test
public void testAuthorizationHeader() throws Exception {
    final String expectedBody = setupStub();
    final String headerName = "Authorization";
    final String headerValue = "foobar";
    final AuthorizationValue authorizationValue = new AuthorizationValue(headerName, headerValue, "header");
    final String actualBody = RemoteUrl.urlToString(getUrl(), Arrays.asList(authorizationValue));
    assertEquals(actualBody, expectedBody);
    verify(getRequestedFor(urlEqualTo("/v2/pet/1")).withHeader("Accept", equalTo(EXPECTED_ACCEPTS_HEADER)).withHeader(headerName, equalTo(headerValue)));
}
Also used : AuthorizationValue(io.swagger.v3.parser.core.models.AuthorizationValue) Test(org.testng.annotations.Test)

Example 12 with AuthorizationValue

use of io.swagger.v3.parser.core.models.AuthorizationValue in project swagger-parser by swagger-api.

the class OpenAPIV3Parser method readContents.

private SwaggerParseResult readContents(String swaggerAsString, List<AuthorizationValue> auth, ParseOptions options, String location) {
    if (swaggerAsString == null || swaggerAsString.trim().isEmpty()) {
        return SwaggerParseResult.ofError("Null or empty definition");
    }
    try {
        final ObjectMapper mapper = getRightMapper(swaggerAsString);
        JsonNode rootNode;
        final SwaggerParseResult deserializationUtilsResult = new SwaggerParseResult();
        if (options != null && options.isLegacyYamlDeserialization()) {
            rootNode = mapper.readTree(swaggerAsString);
        } else {
            try {
                rootNode = DeserializationUtils.deserializeIntoTree(swaggerAsString, location, options, deserializationUtilsResult);
            } catch (Exception e) {
                rootNode = mapper.readTree(swaggerAsString);
            }
        }
        SwaggerParseResult result;
        if (options != null) {
            result = parseJsonNode(location, rootNode, options);
        } else {
            result = parseJsonNode(location, rootNode);
        }
        if (result.getOpenAPI() != null) {
            result = resolve(result, auth, options, location);
        }
        if (deserializationUtilsResult.getMessages() != null) {
            for (String s : deserializationUtilsResult.getMessages()) {
                result.message(getParseErrorMessage(s, location));
            }
        }
        return result;
    } catch (JsonProcessingException e) {
        LOGGER.warn("Exception while parsing:", e);
        final String message = getParseErrorMessage(e.getOriginalMessage(), location);
        return SwaggerParseResult.ofError(message);
    } catch (Exception e) {
        LOGGER.warn("Exception while parsing:", e);
        final String message = getParseErrorMessage(e.getMessage(), location);
        return SwaggerParseResult.ofError(message);
    }
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ReadContentException(io.swagger.v3.parser.exception.ReadContentException) EncodingNotSupportedException(io.swagger.v3.parser.exception.EncodingNotSupportedException) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 13 with AuthorizationValue

use of io.swagger.v3.parser.core.models.AuthorizationValue in project swagger-parser by swagger-api.

the class OpenAPIV3Parser method resolve.

private SwaggerParseResult resolve(SwaggerParseResult result, List<AuthorizationValue> auth, ParseOptions options, String location) {
    try {
        if (options != null) {
            if (options.isResolve() || options.isResolveFully()) {
                OpenAPIResolver resolver = new OpenAPIResolver(result.getOpenAPI(), emptyListIfNull(auth), location, null, options);
                resolver.resolve(result);
                if (options.isResolveFully()) {
                    new ResolverFully(options.isResolveCombinators()).resolveFully(result.getOpenAPI());
                }
            }
            if (options.isFlatten()) {
                final InlineModelResolver inlineModelResolver = new InlineModelResolver(options.isFlattenComposedSchemas(), options.isCamelCaseFlattenNaming(), options.isSkipMatches());
                if (result.getOpenAPI() != null) {
                    inlineModelResolver.flatten(result.getOpenAPI());
                }
            }
        }
    } catch (Exception e) {
        LOGGER.warn("Exception while resolving:", e);
        // TODO verify if this change makes sense (adding resolve messages instead of replacing)
        result.getMessages().add(e.getMessage());
    // result.setMessages(Collections.singletonList(e.getMessage()));
    }
    return result;
}
Also used : InlineModelResolver(io.swagger.v3.parser.util.InlineModelResolver) ReadContentException(io.swagger.v3.parser.exception.ReadContentException) EncodingNotSupportedException(io.swagger.v3.parser.exception.EncodingNotSupportedException) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ResolverFully(io.swagger.v3.parser.util.ResolverFully)

Example 14 with AuthorizationValue

use of io.swagger.v3.parser.core.models.AuthorizationValue in project swagger-parser by swagger-api.

the class OpenAPIResolverTest method testIssue1170.

@Test
public void testIssue1170(@Injectable final List<AuthorizationValue> auths) {
    String path = "/issue-1170/swagger.yaml";
    ParseOptions options = new ParseOptions();
    options.setResolve(true);
    options.setResolveFully(true);
    OpenAPI openAPI = new OpenAPIV3Parser().readLocation(path, auths, options).getOpenAPI();
    // Array schema with items $ref
    Schema breedsListSchema = openAPI.getComponents().getSchemas().get("BreedsList");
    Schema breedSchema = openAPI.getComponents().getSchemas().get("Breed");
    assertNotNull(breedsListSchema);
    assertNotNull(breedSchema);
    assertTrue(breedsListSchema instanceof ArraySchema);
    Schema breedPropertySchema = ((ArraySchema) breedsListSchema).getItems().getProperties().get("breed");
    assertNotNull(breedPropertySchema);
    // Verify items resolved fully
    assertTrue(breedPropertySchema.get$ref() == null);
    assertTrue(breedPropertySchema == breedSchema);
    // Array schema with inline items object with $ref properties
    Schema petsListSchema = openAPI.getComponents().getSchemas().get("PetsList");
    Schema colouringsSchema = openAPI.getComponents().getSchemas().get("Colouring");
    Schema colourSchema = openAPI.getComponents().getSchemas().get("Colour");
    assertNotNull(petsListSchema);
    assertNotNull(colouringsSchema);
    assertNotNull(colourSchema);
    assertTrue(petsListSchema instanceof ArraySchema);
    Schema colouringPropertySchema = ((ArraySchema) petsListSchema).getItems().getProperties().get("colouring");
    assertNotNull(colouringPropertySchema);
    // Verify inline items resolved fully
    assertTrue(colouringPropertySchema.get$ref() == null);
    assertTrue(colouringPropertySchema == colouringsSchema);
}
Also used : ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) 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) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 15 with AuthorizationValue

use of io.swagger.v3.parser.core.models.AuthorizationValue in project swagger-parser by swagger-api.

the class OpenAPIV3ParserTest method testIssue1177.

@Test
public void testIssue1177(@Injectable final List<AuthorizationValue> auths) {
    String path = "/issue-1177/swagger.yaml";
    ParseOptions options = new ParseOptions();
    options.setResolve(true);
    options.setResolveFully(true);
    OpenAPI openAPI = new OpenAPIV3Parser().readLocation(path, auths, options).getOpenAPI();
    // $ref response with $ref header
    ApiResponse petsListApiResponse = openAPI.getPaths().get("/update-pets").getPost().getResponses().get("200");
    assertNotNull(petsListApiResponse);
    Header sessionIdHeader = petsListApiResponse.getHeaders().get("x-session-id");
    assertNotNull(sessionIdHeader);
    Schema petsListSchema = openAPI.getComponents().getSchemas().get("PetsList");
    assertNotNull(petsListSchema);
    assertNotNull(openAPI.getComponents().getHeaders());
    Header sessionIdHeaderComponent = openAPI.getComponents().getHeaders().get("x-session-id");
    assertTrue(sessionIdHeader == sessionIdHeaderComponent);
    assertTrue(petsListApiResponse.getContent().get("application/json").getSchema() == petsListSchema);
}
Also used : Header(io.swagger.v3.oas.models.headers.Header) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) ByteArraySchema(io.swagger.v3.oas.models.media.ByteArraySchema) 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) MapSchema(io.swagger.v3.oas.models.media.MapSchema) ParseOptions(io.swagger.v3.parser.core.models.ParseOptions) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) OpenAPI(io.swagger.v3.oas.models.OpenAPI) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) 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