Search in sources :

Example 31 with ParseOptions

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

the class V2ConverterTest method testInlineDefinitionProperty.

@Test()
public void testInlineDefinitionProperty() throws Exception {
    SwaggerConverter converter = new SwaggerConverter();
    ParseOptions parseOptions = new ParseOptions();
    parseOptions.setResolve(true);
    parseOptions.setFlatten(true);
    SwaggerParseResult result = converter.readLocation("src/test/resources/issue-1359.yaml", null, parseOptions);
    OpenAPI oas = result.getOpenAPI();
    assertNotNull(oas);
    Schema pet = oas.getComponents().getSchemas().get("Pet");
    Schema property = (Schema) pet.getProperties().get("categoryInline");
    assertEquals("#/components/schemas/Pet_categoryInline", property.get$ref());
    Schema petCategoryInline = oas.getComponents().getSchemas().get("Pet_categoryInline");
    assertNotNull(petCategoryInline);
}
Also used : BooleanSchema(io.swagger.v3.oas.models.media.BooleanSchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) Schema(io.swagger.v3.oas.models.media.Schema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) ParseOptions(io.swagger.v3.parser.core.models.ParseOptions) SwaggerConverter(io.swagger.v3.parser.converter.SwaggerConverter) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 32 with ParseOptions

use of io.swagger.v3.parser.core.models.ParseOptions 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 33 with ParseOptions

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

the class OpenAPIV3Parser method readContents.

public SwaggerParseResult readContents(String yaml) {
    final ParseOptions options = new ParseOptions();
    options.setResolve(true);
    return readContents(yaml, null, options);
}
Also used : ParseOptions(io.swagger.v3.parser.core.models.ParseOptions)

Example 34 with ParseOptions

use of io.swagger.v3.parser.core.models.ParseOptions 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 35 with ParseOptions

use of io.swagger.v3.parser.core.models.ParseOptions 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)

Aggregations

ParseOptions (io.swagger.v3.parser.core.models.ParseOptions)202 OpenAPIV3Parser (io.swagger.v3.parser.OpenAPIV3Parser)172 Test (org.testng.annotations.Test)166 OpenAPI (io.swagger.v3.oas.models.OpenAPI)144 SwaggerParseResult (io.swagger.v3.parser.core.models.SwaggerParseResult)140 Schema (io.swagger.v3.oas.models.media.Schema)52 ArraySchema (io.swagger.v3.oas.models.media.ArraySchema)51 StringSchema (io.swagger.v3.oas.models.media.StringSchema)46 ObjectSchema (io.swagger.v3.oas.models.media.ObjectSchema)44 ComposedSchema (io.swagger.v3.oas.models.media.ComposedSchema)40 IntegerSchema (io.swagger.v3.oas.models.media.IntegerSchema)36 ByteArraySchema (io.swagger.v3.oas.models.media.ByteArraySchema)25 MapSchema (io.swagger.v3.oas.models.media.MapSchema)23 Test (org.junit.Test)20 File (java.io.File)12 Parameter (io.swagger.v3.oas.models.parameters.Parameter)11 Expectations (mockit.Expectations)11 HashSet (java.util.HashSet)10 QueryParameter (io.swagger.v3.oas.models.parameters.QueryParameter)9 PathItem (io.swagger.v3.oas.models.PathItem)8