Search in sources :

Example 21 with SwaggerParser

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

the class SwaggerDeserializerTest method testEmptyDefinitions.

@Test
public void testEmptyDefinitions() throws Exception {
    String yaml = "swagger: \"2.0\"\n" + "info:\n" + "  version: \"1.0\"\n" + "  title: \"dd\"\n" + "host: \"abc:5555\"\n" + "basePath: \"/mypath\"\n" + "schemes:\n" + "- \"http\"\n" + "consumes:\n" + "- \"application/json\"\n" + "produces:\n" + "- \"application/json\"\n" + "paths:\n" + "  /resource1/Id:\n" + "    post:\n" + "      description: \"\"\n" + "      operationId: \"postOp\"\n" + "      parameters:\n" + "      - in: \"body\"\n" + "        name: \"input3\"\n" + "        description: \"\"\n" + "        required: true\n" + "        schema:\n" + "          $ref: \"#/definitions/mydefinition\"\n" + "      responses:\n" + "        200:\n" + "          description: \"Successful\"\n" + "        401:\n" + "          description: \"Access Denied\"\n" + "definitions:\n" + "  mydefinition: {}";
    SwaggerParser parser = new SwaggerParser();
    SwaggerDeserializationResult result = parser.readWithInfo(yaml);
    List<String> messageList = result.getMessages();
    Set<String> messages = new HashSet<String>(messageList);
    Swagger swagger = result.getSwagger();
    assertNotNull(swagger);
    assertNotNull(swagger.getDefinitions().get("mydefinition"));
}
Also used : SwaggerParser(io.swagger.parser.SwaggerParser) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 22 with SwaggerParser

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

the class SwaggerDeserializerTest method testDeserializeWithBooleanEnumDiscriminator.

@Test
public void testDeserializeWithBooleanEnumDiscriminator() {
    String yaml = "swagger: '2.0'\n" + "definitions: \n" + "  Animal:\n" + "    type: object\n" + "    discriminator: petType\n" + "    description: |\n" + "      A basic `Animal` object which can extend to other animal types.\n" + "    required:\n" + "      - commonName\n" + "      - petType\n" + "    properties:\n" + "      commonName:\n" + "        description: the household name of the animal\n" + "        type: string\n" + "      petType:\n" + "        enum:\n" + "        - true\n" + "        - false";
    SwaggerParser parser = new SwaggerParser();
    SwaggerDeserializationResult result = parser.readWithInfo(yaml);
    Map<String, Property> properties = result.getSwagger().getDefinitions().get("Animal").getProperties();
    assertTrue(properties.containsKey("commonName"));
    assertTrue(properties.containsKey("petType"));
    assertEquals(properties.get("petType").getType(), "boolean");
}
Also used : SwaggerParser(io.swagger.parser.SwaggerParser) Test(org.testng.annotations.Test)

Example 23 with SwaggerParser

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

the class APIDefinitionFromSwagger20 method removeScopeFromSwaggerDefinition.

@Override
public String removeScopeFromSwaggerDefinition(String resourceConfigJSON, String name) {
    SwaggerParser swaggerParser = new SwaggerParser();
    Swagger swagger = swaggerParser.parse(resourceConfigJSON);
    Map<String, SecuritySchemeDefinition> securitySchemeDefinitionMap = swagger.getSecurityDefinitions();
    if (securitySchemeDefinitionMap != null && !securitySchemeDefinitionMap.isEmpty()) {
        OAuth2Definition oAuth2Definition = (OAuth2Definition) securitySchemeDefinitionMap.get(APIMgtConstants.OAUTH2SECURITY);
        if (oAuth2Definition != null) {
            // Removing Scope from Swagger SecurityDefinition
            oAuth2Definition.getScopes().remove(name);
            // Finding Security requirements at root level
            List<SecurityRequirement> securityRequirements = swagger.getSecurity();
            if (securityRequirements != null && !securityRequirements.isEmpty()) {
                // Get List of Security Requirements
                Iterator<SecurityRequirement> securityRequirementIterator = securityRequirements.iterator();
                while (securityRequirementIterator.hasNext()) {
                    SecurityRequirement securityRequirement = securityRequirementIterator.next();
                    Map<String, List<String>> secListMap = securityRequirement.getRequirements();
                    // get Oauth2Security scopes
                    List<String> scopesList = secListMap.get(APIMgtConstants.OAUTH2SECURITY);
                    if (scopesList != null) {
                        // Remove Scope from root level
                        scopesList.remove(name);
                    }
                    // Check root level security Requirements is empty
                    if (securityRequirement.getRequirements().isEmpty()) {
                        // Check root level security Requirements
                        securityRequirementIterator.remove();
                    }
                }
                if (securityRequirements.isEmpty()) {
                    // Remove root level security
                    swagger.setSecurity(null);
                }
            }
            Map<String, Path> pathMap = swagger.getPaths();
            if (pathMap != null && !pathMap.isEmpty()) {
                for (Map.Entry<String, Path> pathEntry : pathMap.entrySet()) {
                    Path path = pathEntry.getValue();
                    List<Operation> operationList = path.getOperations();
                    for (Operation operation : operationList) {
                        List<Map<String, List<String>>> operationSecurityList = operation.getSecurity();
                        if (operationSecurityList != null && !operationSecurityList.isEmpty()) {
                            Iterator<Map<String, List<String>>> securityMapIterator = operationSecurityList.iterator();
                            while (securityMapIterator.hasNext()) {
                                Map<String, List<String>> securityMap = securityMapIterator.next();
                                List<String> scopesList = securityMap.get(APIMgtConstants.OAUTH2SECURITY);
                                scopesList.remove(name);
                                if (scopesList.isEmpty()) {
                                    securityMapIterator.remove();
                                }
                            }
                            if (operationSecurityList.isEmpty()) {
                                operation.setSecurity(null);
                            }
                        }
                    }
                }
            }
        }
    }
    return Json.pretty(swagger);
}
Also used : Path(io.swagger.models.Path) OAuth2Definition(io.swagger.models.auth.OAuth2Definition) SecuritySchemeDefinition(io.swagger.models.auth.SecuritySchemeDefinition) Operation(io.swagger.models.Operation) SwaggerParser(io.swagger.parser.SwaggerParser) Swagger(io.swagger.models.Swagger) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) SecurityRequirement(io.swagger.models.SecurityRequirement)

Example 24 with SwaggerParser

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

the class APIDefinitionFromSwagger20 method generateCompositeApiFromSwaggerResource.

@Override
public CompositeAPI.Builder generateCompositeApiFromSwaggerResource(String provider, String apiDefinition) throws APIManagementException {
    SwaggerParser swaggerParser = new SwaggerParser();
    Swagger swagger = swaggerParser.parse(apiDefinition);
    if (swagger == null) {
        throw new APIManagementException("Swagger could not be generated from provided API definition");
    }
    Info apiInfo = swagger.getInfo();
    if (apiInfo == null) {
        throw new APIManagementException("Provided Swagger definition doesn't contain API information");
    } else {
        String apiName = apiInfo.getTitle();
        String apiVersion = apiInfo.getVersion();
        String apiDescription = apiInfo.getDescription();
        CompositeAPI.Builder apiBuilder = new CompositeAPI.Builder().provider(provider).name(apiName).version(apiVersion).description(apiDescription).context(swagger.getBasePath());
        List<APIResource> apiResourceList = parseSwaggerAPIResources(new StringBuilder(apiDefinition));
        Map<String, UriTemplate> uriTemplateMap = new HashMap();
        for (APIResource apiResource : apiResourceList) {
            uriTemplateMap.put(apiResource.getUriTemplate().getTemplateId(), apiResource.getUriTemplate());
        }
        apiBuilder.uriTemplates(uriTemplateMap);
        apiBuilder.id(UUID.randomUUID().toString());
        return apiBuilder;
    }
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) APIResource(org.wso2.carbon.apimgt.core.models.APIResource) Info(io.swagger.models.Info) ServiceMethodInfo(org.wso2.msf4j.ServiceMethodInfo) UriTemplate(org.wso2.carbon.apimgt.core.models.UriTemplate) SwaggerParser(io.swagger.parser.SwaggerParser) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) Swagger(io.swagger.models.Swagger) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI)

Example 25 with SwaggerParser

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

the class APIDefinitionFromSwagger20 method updateScopesOnSwaggerDefinition.

@Override
public String updateScopesOnSwaggerDefinition(String resourceConfigJSON, Scope scope) {
    SwaggerParser swaggerParser = new SwaggerParser();
    Swagger swagger = swaggerParser.parse(resourceConfigJSON);
    Map<String, SecuritySchemeDefinition> securitySchemeDefinitionMap = swagger.getSecurityDefinitions();
    if (securitySchemeDefinitionMap != null && !securitySchemeDefinitionMap.isEmpty()) {
        OAuth2Definition oAuth2Definition = (OAuth2Definition) securitySchemeDefinitionMap.get(APIMgtConstants.OAUTH2SECURITY);
        if (oAuth2Definition != null) {
            // Removing Scope from Swagger SecurityDefinition
            Map<String, String> scopeMap = oAuth2Definition.getScopes();
            if (scopeMap != null && scopeMap.containsKey(scope.getName())) {
                scopeMap.replace(scope.getName(), scope.getDescription());
            }
        }
    }
    return Json.pretty(swagger);
}
Also used : SwaggerParser(io.swagger.parser.SwaggerParser) Swagger(io.swagger.models.Swagger) OAuth2Definition(io.swagger.models.auth.OAuth2Definition) SecuritySchemeDefinition(io.swagger.models.auth.SecuritySchemeDefinition)

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