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