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);
}
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;
}
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));
}
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);
}
Aggregations