Search in sources :

Example 1 with ParseOptions

use of io.swagger.v3.parser.core.models.ParseOptions in project vertx-web by vert-x3.

the class OpenAPI3ParametersUnitTest method loadSwagger.

private OpenAPI loadSwagger(String filename) throws IOException {
    ParseOptions options = new ParseOptions();
    options.setResolve(true);
    options.setResolveCombinators(false);
    options.setResolveFully(true);
    return new OpenAPIV3Parser().readContents(String.join("\n", Files.readAllLines(Paths.get(filename), StandardCharsets.UTF_8)), null, options).getOpenAPI();
}
Also used : ParseOptions(io.swagger.v3.parser.core.models.ParseOptions) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser)

Example 2 with ParseOptions

use of io.swagger.v3.parser.core.models.ParseOptions in project vertx-web by vert-x3.

the class OpenApi3Utils method getParseOptions.

public static ParseOptions getParseOptions() {
    ParseOptions options = new ParseOptions();
    options.setResolve(true);
    options.setResolveCombinators(false);
    options.setResolveFully(true);
    return options;
}
Also used : ParseOptions(io.swagger.v3.parser.core.models.ParseOptions)

Example 3 with ParseOptions

use of io.swagger.v3.parser.core.models.ParseOptions in project hippo by NHS-digital-website.

the class SwaggerCodeGenOpenApiSpecificationJsonToHtmlConverter method openApiModelFrom.

private OpenAPI openApiModelFrom(final String openApiSpecificationJson) {
    final ParseOptions parseOptions = new ParseOptions();
    parseOptions.setResolve(true);
    final SwaggerParseResult swaggerParseResult = new OpenAPIV3Parser().readContents(openApiSpecificationJson, null, parseOptions);
    Optional.ofNullable(swaggerParseResult.getMessages()).orElse(Collections.emptyList()).forEach(log::warn);
    return swaggerParseResult.getOpenAPI();
}
Also used : ParseOptions(io.swagger.v3.parser.core.models.ParseOptions) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser)

Example 4 with ParseOptions

use of io.swagger.v3.parser.core.models.ParseOptions in project carbon-apimgt by wso2.

the class SchemaValidator method getOpenAPIValidator.

/**
 * Method to generate OpenApiInteractionValidator when the swagger is provided.
 *
 * @param swagger Swagger definition.
 * @return OpenApiInteractionValidator object for the provided swagger.
 */
private static OpenApiInteractionValidator getOpenAPIValidator(String swagger) {
    OpenAPIParser openAPIParser = new OpenAPIParser();
    ParseOptions options = new ParseOptions();
    options.setResolveFully(true);
    SwaggerParseResult swaggerParseResult = openAPIParser.readContents(swagger, new ArrayList<>(), options);
    OpenAPI openAPI = swaggerParseResult.getOpenAPI();
    return OpenApiInteractionValidator.createFor(openAPI).withLevelResolver(LevelResolver.create().withLevel("validation.schema.required", ValidationReport.Level.INFO).withLevel("validation.response.body.missing", ValidationReport.Level.INFO).build()).build();
}
Also used : ParseOptions(io.swagger.v3.parser.core.models.ParseOptions) OpenAPIParser(io.swagger.parser.OpenAPIParser) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) OpenAPI(io.swagger.v3.oas.models.OpenAPI)

Example 5 with ParseOptions

use of io.swagger.v3.parser.core.models.ParseOptions in project carbon-apimgt by wso2.

the class OAS3Parser method validateAPIDefinition.

/**
 * This method validates the given OpenAPI definition by content
 *
 * @param apiDefinition     OpenAPI Definition content
 * @param host OpenAPI Definition url
 * @param returnJsonContent whether to return the converted json form of the OpenAPI definition
 * @return APIDefinitionValidationResponse object with validation information
 */
@Override
public APIDefinitionValidationResponse validateAPIDefinition(String apiDefinition, String host, boolean returnJsonContent) throws APIManagementException {
    APIDefinitionValidationResponse validationResponse = new APIDefinitionValidationResponse();
    OpenAPIV3Parser openAPIV3Parser = new OpenAPIV3Parser();
    ParseOptions options = new ParseOptions();
    options.setResolve(true);
    SwaggerParseResult parseAttemptForV3 = openAPIV3Parser.readContents(apiDefinition, null, options);
    if (CollectionUtils.isNotEmpty(parseAttemptForV3.getMessages())) {
        validationResponse.setValid(false);
        for (String message : parseAttemptForV3.getMessages()) {
            OASParserUtil.addErrorToValidationResponse(validationResponse, message);
            if (message.contains(APIConstants.OPENAPI_IS_MISSING_MSG)) {
                ErrorItem errorItem = new ErrorItem();
                errorItem.setErrorCode(ExceptionCodes.INVALID_OAS3_FOUND.getErrorCode());
                errorItem.setMessage(ExceptionCodes.INVALID_OAS3_FOUND.getErrorMessage());
                errorItem.setDescription(ExceptionCodes.INVALID_OAS3_FOUND.getErrorMessage());
                validationResponse.getErrorItems().add(errorItem);
            }
        }
    } else {
        OpenAPI openAPI = parseAttemptForV3.getOpenAPI();
        io.swagger.v3.oas.models.info.Info info = openAPI.getInfo();
        List<String> endpoints;
        String endpointWithHost = "";
        if (openAPI.getServers() == null || openAPI.getServers().isEmpty()) {
            endpoints = null;
        } else {
            endpoints = openAPI.getServers().stream().map(url -> url.getUrl()).collect(Collectors.toList());
            for (String endpoint : endpoints) {
                if (endpoint.startsWith("/")) {
                    if (StringUtils.isEmpty(host)) {
                        endpointWithHost = "http://api.yourdomain.com" + endpoint;
                    } else {
                        endpointWithHost = host + endpoint;
                    }
                    endpoints.set(endpoints.indexOf(endpoint), endpointWithHost);
                }
            }
        }
        String title = null;
        String context = null;
        if (!StringUtils.isBlank(info.getTitle())) {
            title = info.getTitle();
            context = info.getTitle().replaceAll("\\s", "").toLowerCase();
        }
        OASParserUtil.updateValidationResponseAsSuccess(validationResponse, apiDefinition, openAPI.getOpenapi(), title, info.getVersion(), context, info.getDescription(), endpoints);
        validationResponse.setParser(this);
        if (returnJsonContent) {
            if (!apiDefinition.trim().startsWith("{")) {
                // not a json (it is yaml)
                JsonNode jsonNode = DeserializationUtils.readYamlTree(apiDefinition);
                validationResponse.setJsonContent(jsonNode.toString());
            } else {
                validationResponse.setJsonContent(apiDefinition);
            }
        }
    }
    return validationResponse;
}
Also used : Info(io.swagger.v3.oas.models.info.Info) ErrorItem(org.wso2.carbon.apimgt.api.ErrorItem) JsonNode(com.fasterxml.jackson.databind.JsonNode) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) APIDefinitionValidationResponse(org.wso2.carbon.apimgt.api.APIDefinitionValidationResponse) ParseOptions(io.swagger.v3.parser.core.models.ParseOptions) OpenAPI(io.swagger.v3.oas.models.OpenAPI)

Aggregations

ParseOptions (io.swagger.v3.parser.core.models.ParseOptions)198 OpenAPIV3Parser (io.swagger.v3.parser.OpenAPIV3Parser)170 Test (org.testng.annotations.Test)166 OpenAPI (io.swagger.v3.oas.models.OpenAPI)143 SwaggerParseResult (io.swagger.v3.parser.core.models.SwaggerParseResult)138 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)18 Parameter (io.swagger.v3.oas.models.parameters.Parameter)11 File (java.io.File)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)7