Search in sources :

Example 21 with SecurityRequirement

use of io.swagger.v3.oas.models.security.SecurityRequirement in project swagger-parser by swagger-api.

the class OpenAPIDeserializerTest method testSecurityDeserialization.

@Test
public void testSecurityDeserialization() throws Exception {
    String yaml = "openapi: 3.0.0\n" + "security:\n" + "  - api_key1: []\n" + "    api_key2: []\n" + "  - api_key3: []\n";
    OpenAPIV3Parser parser = new OpenAPIV3Parser();
    SwaggerParseResult result = parser.readContents(yaml, null, null);
    OpenAPI openAPI = result.getOpenAPI();
    assertNotNull(openAPI);
    List<SecurityRequirement> security = openAPI.getSecurity();
    assertTrue(security.size() == 2);
}
Also used : SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) OpenAPI(io.swagger.v3.oas.models.OpenAPI) SecurityRequirement(io.swagger.v3.oas.models.security.SecurityRequirement) Test(org.testng.annotations.Test)

Example 22 with SecurityRequirement

use of io.swagger.v3.oas.models.security.SecurityRequirement in project swagger-parser by swagger-api.

the class OpenAPIDeserializer method parseRoot.

public OpenAPI parseRoot(JsonNode node, ParseResult result, String path) {
    String location = "";
    OpenAPI openAPI = new OpenAPI();
    if (node.getNodeType().equals(JsonNodeType.OBJECT)) {
        ObjectNode rootNode = (ObjectNode) node;
        // required
        String value = getString("openapi", rootNode, true, location, result);
        // we don't even try if the version isn't there
        if (value == null || !value.startsWith("3.0")) {
            return null;
        }
        openAPI.setOpenapi(value);
        ObjectNode obj = getObject("info", rootNode, true, location, result);
        if (obj != null) {
            Info info = getInfo(obj, "info", result);
            openAPI.setInfo(info);
        }
        obj = getObject("components", rootNode, false, location, result);
        if (obj != null) {
            Components components = getComponents(obj, "components", result);
            openAPI.setComponents(components);
            this.components = components;
        }
        obj = getObject("paths", rootNode, true, location, result);
        if (obj != null) {
            Paths paths = getPaths(obj, "paths", result);
            openAPI.setPaths(paths);
        }
        ArrayNode array = getArray("servers", rootNode, false, location, result);
        if (array != null && array.size() > 0) {
            openAPI.setServers(getServersList(array, String.format("%s.%s", location, "servers"), result, path));
        } else {
            Server defaultServer = new Server();
            defaultServer.setUrl("/");
            List<Server> servers = new ArrayList<>();
            servers.add(defaultServer);
            openAPI.setServers(servers);
        }
        obj = getObject("externalDocs", rootNode, false, location, result);
        if (obj != null) {
            ExternalDocumentation externalDocs = getExternalDocs(obj, "externalDocs", result);
            openAPI.setExternalDocs(externalDocs);
        }
        array = getArray("tags", rootNode, false, location, result);
        if (array != null && array.size() > 0) {
            openAPI.setTags(getTagList(array, "tags", result));
        }
        array = getArray("security", rootNode, false, location, result);
        if (array != null && array.size() > 0) {
            List<SecurityRequirement> securityRequirements = getSecurityRequirementsList(array, "security", result);
            if (securityRequirements != null && securityRequirements.size() > 0) {
                openAPI.setSecurity(securityRequirements);
            }
        }
        Map<String, Object> extensions = getExtensions(rootNode);
        if (extensions != null && extensions.size() > 0) {
            openAPI.setExtensions(extensions);
        }
        Set<String> keys = getKeys(rootNode);
        for (String key : keys) {
            if (!ROOT_KEYS.contains(key) && !key.startsWith("x-")) {
                result.extra(location, key, node.get(key));
            }
        }
    } else {
        result.invalidType(location, "openapi", "object", node);
        result.invalid();
        return null;
    }
    return openAPI;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Server(io.swagger.v3.oas.models.servers.Server) Info(io.swagger.v3.oas.models.info.Info) Components(io.swagger.v3.oas.models.Components) ExternalDocumentation(io.swagger.v3.oas.models.ExternalDocumentation) Paths(io.swagger.v3.oas.models.Paths) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) OpenAPI(io.swagger.v3.oas.models.OpenAPI) SecurityRequirement(io.swagger.v3.oas.models.security.SecurityRequirement)

Example 23 with SecurityRequirement

use of io.swagger.v3.oas.models.security.SecurityRequirement in project swagger-core by swagger-api.

the class ReaderTest method testSecurityRequirement.

@Test(description = "Security Requirement")
public void testSecurityRequirement() {
    Reader reader = new Reader(new OpenAPI());
    Method[] methods = SecurityResource.class.getDeclaredMethods();
    Operation securityOperation = reader.parseMethod(Arrays.stream(methods).filter((method -> method.getName().equals("getSecurity"))).findFirst().get(), null, null);
    assertNotNull(securityOperation);
    List<SecurityRequirement> securityRequirements = securityOperation.getSecurity();
    assertNotNull(securityRequirements);
    assertEquals(SECURITY_REQUIREMENT_NUMBER, securityRequirements.size());
    List<String> scopes = securityRequirements.get(0).get(SECURITY_KEY);
    assertNotNull(scopes);
    assertEquals(SCOPE_NUMBER, scopes.size());
    assertEquals(SCOPE_VALUE1, scopes.get(0));
    assertEquals(SCOPE_VALUE2, scopes.get(1));
}
Also used : Method(java.lang.reflect.Method) Operation(io.swagger.v3.oas.models.Operation) OpenAPI(io.swagger.v3.oas.models.OpenAPI) SecurityRequirement(io.swagger.v3.oas.models.security.SecurityRequirement) Test(org.testng.annotations.Test)

Example 24 with SecurityRequirement

use of io.swagger.v3.oas.models.security.SecurityRequirement in project swagger-core by swagger-api.

the class SecurityParser method getSecurityRequirements.

public static Optional<List<SecurityRequirement>> getSecurityRequirements(io.swagger.v3.oas.annotations.security.SecurityRequirement[] securityRequirementsApi) {
    if (securityRequirementsApi == null || securityRequirementsApi.length == 0) {
        return Optional.empty();
    }
    List<SecurityRequirement> securityRequirements = new ArrayList<>();
    for (io.swagger.v3.oas.annotations.security.SecurityRequirement securityRequirementApi : securityRequirementsApi) {
        if (StringUtils.isBlank(securityRequirementApi.name())) {
            continue;
        }
        SecurityRequirement securityRequirement = new SecurityRequirement();
        if (securityRequirementApi.scopes().length > 0) {
            securityRequirement.addList(securityRequirementApi.name(), Arrays.asList(securityRequirementApi.scopes()));
        } else {
            securityRequirement.addList(securityRequirementApi.name());
        }
        securityRequirements.add(securityRequirement);
    }
    if (securityRequirements.isEmpty()) {
        return Optional.empty();
    }
    return Optional.of(securityRequirements);
}
Also used : ArrayList(java.util.ArrayList) SecurityRequirement(io.swagger.v3.oas.models.security.SecurityRequirement)

Aggregations

SecurityRequirement (io.swagger.v3.oas.models.security.SecurityRequirement)23 OpenAPI (io.swagger.v3.oas.models.OpenAPI)14 Operation (io.swagger.v3.oas.models.Operation)11 ArrayList (java.util.ArrayList)10 PathItem (io.swagger.v3.oas.models.PathItem)8 SecurityScheme (io.swagger.v3.oas.models.security.SecurityScheme)8 HashMap (java.util.HashMap)8 LinkedHashMap (java.util.LinkedHashMap)8 List (java.util.List)8 Test (org.testng.annotations.Test)8 Components (io.swagger.v3.oas.models.Components)7 Map (java.util.Map)7 HashSet (java.util.HashSet)5 Paths (io.swagger.v3.oas.models.Paths)4 OAuthFlow (io.swagger.v3.oas.models.security.OAuthFlow)4 OAuthFlows (io.swagger.v3.oas.models.security.OAuthFlows)4 Scopes (io.swagger.v3.oas.models.security.Scopes)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)3 HttpMethod (io.swagger.models.HttpMethod)3 Json (io.swagger.v3.core.util.Json)3