Search in sources :

Example 36 with SwaggerParser

use of io.swagger.parser.SwaggerParser in project carbon-apimgt by wso2.

the class OAS2ParserTest method testUpdateAPIDefinition.

@Test
public void testUpdateAPIDefinition() throws Exception {
    String relativePath = "definitions" + File.separator + "oas2" + File.separator + "oas2Resources.json";
    String oas2Resources = IOUtils.toString(getClass().getClassLoader().getResourceAsStream(relativePath), "UTF-8");
    OASParserEvaluator evaluator = (definition -> {
        SwaggerParser swaggerParser = new SwaggerParser();
        Swagger swagger = swaggerParser.parse(definition);
        Assert.assertNotNull(swagger);
        Assert.assertEquals(1, swagger.getPaths().size());
        Assert.assertFalse(swagger.getPaths().containsKey("/noresource/{resid}"));
    });
    testGenerateAPIDefinition2(oas2Parser, oas2Resources, evaluator);
}
Also used : SwaggerParser(io.swagger.parser.SwaggerParser) Swagger(io.swagger.models.Swagger) Test(org.junit.Test)

Example 37 with SwaggerParser

use of io.swagger.parser.SwaggerParser in project carbon-apimgt by wso2.

the class SOAPOperationBindingTestCase method testGetSwaggerFromWSDLWithExternalXSDs.

@Test
public void testGetSwaggerFromWSDLWithExternalXSDs() throws Exception {
    String externalXSDElementName = "InvokeClaimGeniousDataIntoCordysElement";
    String externalXSDElementInputOperation = "invokeClaimGeniousDataIntoCordysBindingOperationInput";
    String externalXSDElementOutputOperation = "invokeClaimGeniousDataIntoCordysBindingOperationOutput";
    String externalXSDElementPropertyName = "LocalizableMessage";
    String swaggerStr = SOAPOperationBindingUtils.getSoapOperationMappingForUrl(Thread.currentThread().getContextClassLoader().getResource("wsdls/wsdl-with-external-xsds/sampleWSDLWithExternalXSDFiles.wsdl").toExternalForm());
    Swagger swagger = new SwaggerParser().parse(swaggerStr);
    Assert.assertTrue("Unable to parse the swagger from the given string", swagger != null);
    Assert.assertNotNull(swagger.getPaths());
    // Assert WSDL elements
    Assert.assertEquals(1, swagger.getPaths().size());
    Assert.assertEquals(5, swagger.getDefinitions().size());
    Assert.assertNotNull(swagger.getDefinitions().get(externalXSDElementOutputOperation));
    Assert.assertNotNull(swagger.getDefinitions().get(externalXSDElementInputOperation));
    Assert.assertNotNull(swagger.getDefinitions().get(externalXSDElementName));
    // Assert WSDL external XSD element
    Assert.assertTrue(swagger.getDefinitions().get(externalXSDElementName).getProperties().size() == 1);
    Assert.assertNotNull(swagger.getDefinitions().get(externalXSDElementName).getProperties().get(externalXSDElementPropertyName));
}
Also used : SwaggerParser(io.swagger.parser.SwaggerParser) Swagger(io.swagger.models.Swagger) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 38 with SwaggerParser

use of io.swagger.parser.SwaggerParser in project swagger-parser by swagger-api.

the class SwaggerConverter method readContents.

@Override
public SwaggerParseResult readContents(String swaggerAsString, List<AuthorizationValue> auth, ParseOptions options) {
    SwaggerDeserializationResult result = new SwaggerParser().readWithInfo(swaggerAsString, options == null ? true : options.isResolve());
    if (options != null) {
        if (options.isResolve()) {
            Swagger resolved = new SwaggerResolver(result.getSwagger(), convert(auth)).resolve();
            result.setSwagger(resolved);
        }
    }
    return readResult(result, auth, options);
}
Also used : SwaggerParser(io.swagger.parser.SwaggerParser) SwaggerResolver(io.swagger.parser.SwaggerResolver) SwaggerDeserializationResult(io.swagger.parser.util.SwaggerDeserializationResult) Swagger(io.swagger.models.Swagger)

Example 39 with SwaggerParser

use of io.swagger.parser.SwaggerParser in project swagger-parser by swagger-api.

the class SwaggerConverter method readLocation.

@Override
public SwaggerParseResult readLocation(String url, List<AuthorizationValue> auths, ParseOptions options) {
    boolean resolve = false;
    if (options != null) {
        resolve = options.isResolve();
    }
    SwaggerDeserializationResult result = new SwaggerParser().readWithInfo(url, convert(auths), resolve);
    return readResult(result, auths, options);
}
Also used : SwaggerParser(io.swagger.parser.SwaggerParser) SwaggerDeserializationResult(io.swagger.parser.util.SwaggerDeserializationResult)

Example 40 with SwaggerParser

use of io.swagger.parser.SwaggerParser in project gravitee-management-rest-api by gravitee-io.

the class SwaggerServiceImpl method prepare.

public NewApiEntity prepare(ImportSwaggerDescriptorEntity swaggerDescriptor) {
    Swagger swagger = null;
    switch(swaggerDescriptor.getVersion()) {
        case VERSION_1_0:
            try {
                logger.info("Loading an old Swagger descriptor from {}", swaggerDescriptor.getPayload());
                // For spec < 2.0, only read by url is possible
                swagger = new SwaggerCompatConverter().read(swaggerDescriptor.getPayload());
            } catch (IOException ioe) {
                logger.error("Can not read old Swagger specification", ioe);
                throw new SwaggerDescriptorException();
            }
        case VERSION_2_0:
            if (swaggerDescriptor.getType() == ImportSwaggerDescriptorEntity.Type.INLINE) {
                logger.info("Loading an inline Swagger descriptor");
                swagger = new SwaggerParser().parse(swaggerDescriptor.getPayload());
            } else if (swaggerDescriptor.getType() == ImportSwaggerDescriptorEntity.Type.URL) {
                logger.info("Loading a Swagger descriptor from URL: ", swaggerDescriptor.getPayload());
                swagger = new SwaggerParser().read(swaggerDescriptor.getPayload());
            }
    }
    if (swagger == null) {
        throw new SwaggerDescriptorException();
    }
    NewApiEntity apiEntity = new NewApiEntity();
    apiEntity.setName(swagger.getInfo().getTitle());
    apiEntity.setDescription(swagger.getInfo().getDescription());
    apiEntity.setVersion(swagger.getInfo().getVersion());
    String scheme = (swagger.getSchemes() == null || swagger.getSchemes().isEmpty()) ? defaultScheme : swagger.getSchemes().iterator().next().toValue();
    apiEntity.setEndpoint(scheme + "://" + swagger.getHost() + swagger.getBasePath());
    apiEntity.setPaths(new ArrayList<>(swagger.getPaths().keySet().stream().map(path -> path.replaceAll("\\{(.[^/]*)\\}", ":$1")).collect(Collectors.toList())));
    return apiEntity;
}
Also used : SwaggerParser(io.swagger.parser.SwaggerParser) Scheme(io.swagger.models.Scheme) Yaml(io.swagger.util.Yaml) SwaggerParser(io.swagger.parser.SwaggerParser) Swagger(io.swagger.models.Swagger) URISyntaxException(java.net.URISyntaxException) Json(io.swagger.util.Json) LoggerFactory(org.slf4j.LoggerFactory) ArrayList(java.util.ArrayList) Value(org.springframework.beans.factory.annotation.Value) PageEntity(io.gravitee.management.model.PageEntity) OpenAPI(io.swagger.v3.oas.models.OpenAPI) SwaggerCompatConverter(io.swagger.parser.SwaggerCompatConverter) URI(java.net.URI) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) Logger(org.slf4j.Logger) ImportSwaggerDescriptorEntity(io.gravitee.management.model.ImportSwaggerDescriptorEntity) BufferedWriter(java.io.BufferedWriter) FileWriter(java.io.FileWriter) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) NewApiEntity(io.gravitee.management.model.NewApiEntity) Collectors(java.util.stream.Collectors) File(java.io.File) Component(org.springframework.stereotype.Component) MediaType(io.gravitee.common.http.MediaType) PageConfigurationEntity(io.gravitee.management.model.PageConfigurationEntity) SwaggerDescriptorException(io.gravitee.management.service.exceptions.SwaggerDescriptorException) SwaggerService(io.gravitee.management.service.SwaggerService) Collections(java.util.Collections) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) SwaggerDescriptorException(io.gravitee.management.service.exceptions.SwaggerDescriptorException) SwaggerCompatConverter(io.swagger.parser.SwaggerCompatConverter) Swagger(io.swagger.models.Swagger) IOException(java.io.IOException) NewApiEntity(io.gravitee.management.model.NewApiEntity)

Aggregations

SwaggerParser (io.swagger.parser.SwaggerParser)91 Swagger (io.swagger.models.Swagger)44 Test (org.testng.annotations.Test)37 HashSet (java.util.HashSet)21 HashMap (java.util.HashMap)14 Path (io.swagger.models.Path)11 IOException (java.io.IOException)11 ArrayList (java.util.ArrayList)11 Map (java.util.Map)10 Operation (io.swagger.models.Operation)9 List (java.util.List)9 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)9 SwaggerDeserializationResult (io.swagger.parser.util.SwaggerDeserializationResult)8 Test (org.junit.Test)8 HttpMethod (io.swagger.models.HttpMethod)7 OpenAPIV3Parser (io.swagger.v3.parser.OpenAPIV3Parser)7 File (java.io.File)7 OAuth2Definition (io.swagger.models.auth.OAuth2Definition)6 SecuritySchemeDefinition (io.swagger.models.auth.SecuritySchemeDefinition)6 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)6