use of io.swagger.parser.SwaggerParser in project swagger-parser by swagger-api.
the class SwaggerDeserializerTest method testNestedDefinitions.
@Test
public void testNestedDefinitions() {
String json = "{\n" + " \"swagger\": \"2.0\",\n" + " \"definitions\": {\n" + " \"Person\": {\n" + " \"required\": [\n" + " \"id\",\n" + " \"name\"\n" + " ],\n" + " \"properties\": {\n" + " \"id\": {\n" + " \"type\": \"integer\",\n" + " \"format\": \"int64\"\n" + " },\n" + " \"name\": {\n" + " \"type\": \"string\"\n" + " },\n" + " \"address\": {\n" + " \t\"$ref\": \"#/definitions/Address\"\n" + " }\n" + " }\n" + " },\n" + " \"Address\": {\n" + " \t\"required\": [\"zip\"],\n" + " \t\"properties\": {\n" + " \t\t\"street\": {\n" + " \t\t\t\"type\": \"string\"\n" + " \t\t},\n" + " \t\t\"zip\": {\n" + " \t\t\t\"type\": \"integer\",\n" + " \t\t\t\"format\": \"int32\",\n" + " \t\t\t\"minimum\": 0,\n" + " \t\t\t\"exclusiveMinimum\": true,\n" + " \t\t\t\"maximum\": 99999,\n" + " \t\t\t\"exclusiveMaximum\": true\n" + " \t\t}\n" + " \t}\n" + " }\n" + " }\n" + "}";
SwaggerParser parser = new SwaggerParser();
SwaggerDeserializationResult result = parser.readWithInfo(json);
List<String> messageList = result.getMessages();
Set<String> messages = new HashSet<String>(messageList);
assertTrue(result.getSwagger().getDefinitions().get("Person") instanceof ModelImpl);
assertTrue(result.getSwagger().getDefinitions().get("Address") instanceof ModelImpl);
ModelImpl person = (ModelImpl) result.getSwagger().getDefinitions().get("Person");
Property property = person.getProperties().get("address");
assertTrue(property instanceof RefProperty);
Property zip = ((ModelImpl) result.getSwagger().getDefinitions().get("Address")).getProperties().get("zip");
assertTrue(zip instanceof IntegerProperty);
IntegerProperty zipProperty = (IntegerProperty) zip;
assertEquals(zipProperty.getMinimum(), new BigDecimal("0"));
assertTrue(zipProperty.getExclusiveMinimum());
assertEquals(zipProperty.getMaximum(), new BigDecimal("99999"));
assertTrue(zipProperty.getExclusiveMaximum());
}
use of io.swagger.parser.SwaggerParser in project swagger-parser by swagger-api.
the class SwaggerDeserializerTest method testSecurityDefinitionWithMissingAttribute.
@Test
public void testSecurityDefinitionWithMissingAttribute() {
String json = "{\n" + " \"swagger\": \"2.0\",\n" + " \"securityDefinitions\": {\n" + " \"api_key\": {\n" + " \"description\": \"api key description\"\n" + " }\n" + " }\n" + "}";
SwaggerParser parser = new SwaggerParser();
SwaggerDeserializationResult result = parser.readWithInfo(json);
List<String> messageList = result.getMessages();
Set<String> messages = new HashSet<>(messageList);
assertTrue(messages.contains("attribute securityDefinitions.api_key.type is missing"));
}
use of io.swagger.parser.SwaggerParser in project swagger-parser by swagger-api.
the class SwaggerDeserializerTest method testIssue247.
@Test
public void testIssue247() {
String yaml = "swagger: '2.0'\n" + "info:\n" + " description: 'bleh'\n" + " version: '2.0.0'\n" + " title: 'Test'\n" + "paths:\n" + " /:\n" + " get:\n" + " parameters: []\n" + " responses:\n" + " 200:\n" + " description: 'OK'\n" + " parameters: []\n" + "definitions:\n" + " Pet:\n" + " allOf:\n" + " - type: 'object'\n" + " required:\n" + " - 'id'\n" + " properties:\n" + " id:\n" + " type: 'integer'\n" + " format: 'int64'";
SwaggerParser parser = new SwaggerParser();
SwaggerDeserializationResult result = parser.readWithInfo(yaml);
Swagger swagger = result.getSwagger();
assertNotNull(swagger.getDefinitions().get("Pet"));
}
use of io.swagger.parser.SwaggerParser in project records-management by Alfresco.
the class BaseYamlUnitTest method validateYamlFiles.
/**
* Helper method to validate that all given yaml files are valid readable Swagger format
*/
protected void validateYamlFiles(final Set<String> yamlFileNames) throws ProcessingException, IOException {
assertFalse("Expected at least 1 yaml file to validate", yamlFileNames.isEmpty());
final JsonSchema swaggerSchema = getSwaggerSchema(SWAGGER_2_SCHEMA_LOCATION);
assertNotNull("Failed to obtain the Swagger schema", swaggerSchema);
for (String yamlFilePath : yamlFileNames) {
try {
// check the yaml file is valid against Swagger JSON schema
assertTrue("Yaml file is not valid Swagger " + OPEN_API_SPECIFICATION + ": " + yamlFilePath, validateYamlFile(yamlFilePath, swaggerSchema));
// check can read the swagger object to obtain the swagger version
Swagger swagger = new SwaggerParser().read(yamlFilePath);
assertEquals("Failed to obtain Swagger version from yaml file " + yamlFilePath, swagger.getSwagger(), OPEN_API_SPECIFICATION);
} catch (ParserException ex) {
// ensure the yaml filename is included in the message
String context = String.format(yamlFilePath + ": %n" + ex.getContext());
throw new ParserException(context, ex.getContextMark(), ex.getProblem(), ex.getProblemMark());
}
}
}
use of io.swagger.parser.SwaggerParser in project syndesis by syndesisio.
the class UnifiedXmlDataShapeGeneratorShapeValidityTest method specifications.
@Parameters
public static Iterable<Object[]> specifications() {
final List<String> specifications = Collections.singletonList("/swagger/petstore.swagger.json");
final List<Object[]> parameters = new ArrayList<>();
specifications.forEach(specification -> {
String specificationContent;
try (InputStream in = UnifiedXmlDataShapeGenerator.class.getResourceAsStream(specification)) {
specificationContent = IOUtils.toString(in, StandardCharsets.UTF_8);
} catch (final IOException e) {
throw new AssertionError("Unable to load swagger specification in path: " + specification, e);
}
final SwaggerParser parser = new SwaggerParser();
final Swagger swagger = parser.parse(specificationContent);
swagger.getPaths().forEach((path, operations) -> {
operations.getOperationMap().forEach((method, operation) -> {
final Optional<BodyParameter> bodyParameter = BaseDataShapeGenerator.findBodyParameter(operation);
if (!bodyParameter.isPresent()) {
// only parameters
return;
}
parameters.add(new Object[] { specificationContent, swagger, operation, specification });
});
});
});
return parameters;
}
Aggregations