Search in sources :

Example 1 with SwaggerDescriptorException

use of io.gravitee.rest.api.service.exceptions.SwaggerDescriptorException in project gravitee-management-rest-api by gravitee-io.

the class SwaggerServiceImpl method createAPI.

@Override
public SwaggerApiEntity createAPI(ImportSwaggerDescriptorEntity swaggerDescriptor, DefinitionVersion definitionVersion) {
    ParseOptions options = new ParseOptions();
    // In the context of an import, we need to fully resolve $ref which are required for OAIDescriptorVisitors (ie. policies).
    options.setResolveFully(true);
    boolean wsdlImport = Format.WSDL.equals(swaggerDescriptor.getFormat());
    SwaggerDescriptor descriptor = parse(swaggerDescriptor.getPayload(), wsdlImport, options);
    if (wsdlImport) {
        overridePayload(swaggerDescriptor, descriptor);
        populateXmlToJsonPolicy(swaggerDescriptor);
    }
    if (descriptor != null) {
        if (definitionVersion.equals(DefinitionVersion.V2)) {
            return new OAIToAPIV2Converter(swaggerDescriptor, policyOperationVisitorManager, groupService, tagService).convert((OAIDescriptor) descriptor);
        } else {
            return new OAIToAPIConverter(swaggerDescriptor, policyOperationVisitorManager, groupService, tagService).convert((OAIDescriptor) descriptor);
        }
    }
    throw new SwaggerDescriptorException();
}
Also used : OAIToAPIConverter(io.gravitee.rest.api.service.impl.swagger.converter.api.OAIToAPIConverter) OAIToAPIV2Converter(io.gravitee.rest.api.service.impl.swagger.converter.api.OAIToAPIV2Converter) SwaggerDescriptorException(io.gravitee.rest.api.service.exceptions.SwaggerDescriptorException) SwaggerDescriptor(io.gravitee.rest.api.service.swagger.SwaggerDescriptor) ParseOptions(io.swagger.v3.parser.core.models.ParseOptions)

Example 2 with SwaggerDescriptorException

use of io.gravitee.rest.api.service.exceptions.SwaggerDescriptorException in project gravitee-management-rest-api by gravitee-io.

the class OAIParser method parse.

public OAIDescriptor parse(String content, ParseOptions options) {
    OpenAPIParser parser = new OpenAPIParser();
    SwaggerParseResult parseResult;
    String path = content;
    File temp = null;
    if (!isLocationUrl(content)) {
        // Swagger v1 supports only a URL to read swagger: create temporary file for Swagger parser
        temp = createTempFile(content);
        path = temp.getAbsolutePath();
    }
    parseResult = parser.readLocation(path, null, options);
    if (temp != null) {
        temp.delete();
    }
    /* Hack due to swagger v1 converting issue
         * See https://github.com/swagger-api/swagger-parser/issues/1451
         */
    if (parseResult.getMessages() != null) {
        final List<String> filteredMessages = parseResult.getMessages().stream().filter(message -> message != null && !message.matches("^attribute info.contact.*")).collect(Collectors.toList());
        parseResult.setMessages(filteredMessages);
    }
    if (parseResult.getOpenAPI() == null) {
        throw new SwaggerDescriptorException("Malformed descriptor");
    }
    OAIDescriptor descriptor = new OAIDescriptor(parseResult.getOpenAPI());
    descriptor.setMessages(parseResult.getMessages());
    return descriptor;
}
Also used : SwaggerDescriptorException(io.gravitee.rest.api.service.exceptions.SwaggerDescriptorException) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) OpenAPIParser(io.swagger.parser.OpenAPIParser) Logger(org.slf4j.Logger) BufferedWriter(java.io.BufferedWriter) FileWriter(java.io.FileWriter) LoggerFactory(org.slf4j.LoggerFactory) OAIDescriptor(io.gravitee.rest.api.service.swagger.OAIDescriptor) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) File(java.io.File) ParseOptions(io.swagger.v3.parser.core.models.ParseOptions) List(java.util.List) OAIDescriptor(io.gravitee.rest.api.service.swagger.OAIDescriptor) SwaggerDescriptorException(io.gravitee.rest.api.service.exceptions.SwaggerDescriptorException) OpenAPIParser(io.swagger.parser.OpenAPIParser) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) File(java.io.File)

Example 3 with SwaggerDescriptorException

use of io.gravitee.rest.api.service.exceptions.SwaggerDescriptorException in project gravitee-management-rest-api by gravitee-io.

the class SwaggerServiceImpl method parse.

public SwaggerDescriptor parse(String content, boolean wsdl, ParseOptions options) {
    OpenAPI descriptor;
    if (isUrl(content)) {
        UrlSanitizerUtils.checkAllowed(content, importConfiguration.getImportWhitelist(), importConfiguration.isAllowImportFromPrivate());
    }
    if (wsdl) {
        // try to read wsdl
        logger.debug("Trying to load a Wsdl descriptor");
        descriptor = new WsdlParser().parse(content);
        if (descriptor != null) {
            return new OAIDescriptor(descriptor);
        }
    } else {
        OAIDescriptor oaiDescriptor = new OAIParser().parse(content, options);
        if (oaiDescriptor == null || oaiDescriptor.getSpecification() == null) {
            throw new SwaggerDescriptorException();
        }
        return oaiDescriptor;
    }
    throw new SwaggerDescriptorException();
}
Also used : OAIDescriptor(io.gravitee.rest.api.service.swagger.OAIDescriptor) SwaggerDescriptorException(io.gravitee.rest.api.service.exceptions.SwaggerDescriptorException) OAIParser(io.gravitee.rest.api.service.impl.swagger.parser.OAIParser) WsdlParser(io.gravitee.rest.api.service.impl.swagger.parser.WsdlParser) OpenAPI(io.swagger.v3.oas.models.OpenAPI)

Aggregations

SwaggerDescriptorException (io.gravitee.rest.api.service.exceptions.SwaggerDescriptorException)3 OAIDescriptor (io.gravitee.rest.api.service.swagger.OAIDescriptor)2 ParseOptions (io.swagger.v3.parser.core.models.ParseOptions)2 OAIToAPIConverter (io.gravitee.rest.api.service.impl.swagger.converter.api.OAIToAPIConverter)1 OAIToAPIV2Converter (io.gravitee.rest.api.service.impl.swagger.converter.api.OAIToAPIV2Converter)1 OAIParser (io.gravitee.rest.api.service.impl.swagger.parser.OAIParser)1 WsdlParser (io.gravitee.rest.api.service.impl.swagger.parser.WsdlParser)1 SwaggerDescriptor (io.gravitee.rest.api.service.swagger.SwaggerDescriptor)1 OpenAPIParser (io.swagger.parser.OpenAPIParser)1 OpenAPI (io.swagger.v3.oas.models.OpenAPI)1 SwaggerParseResult (io.swagger.v3.parser.core.models.SwaggerParseResult)1 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1