use of io.swagger.parser.SwaggerParser in project camel by apache.
the class RestSwaggerEndpoint method loadSpecificationFrom.
/**
* Loads the Swagger definition model from the given path. Tries to resolve
* the resource using Camel's resource loading support, if it fails uses
* Swagger's resource loading support instead.
*
* @param uri URI of the specification
* @param camelContext context to use
* @return the specification
* @throws IOException
*/
static Swagger loadSpecificationFrom(final CamelContext camelContext, final URI uri) throws IOException {
final ObjectMapper mapper = Json.mapper();
final SwaggerParser swaggerParser = new SwaggerParser();
final String uriAsString = uri.toString();
try (InputStream stream = ResourceHelper.resolveMandatoryResourceAsInputStream(camelContext, uriAsString)) {
final JsonNode node = mapper.readTree(stream);
return swaggerParser.read(node);
} catch (final IOException e) {
// try Swaggers loader
final Swagger swagger = swaggerParser.read(uriAsString);
if (swagger != null) {
return swagger;
}
throw new IllegalArgumentException("The given Swagger specification could not be loaded from `" + uri + "`. Tried loading using Camel's resource resolution and using Swagger's own resource resolution." + " Swagger tends to swallow exceptions while parsing, try specifying Java system property `debugParser`" + " (e.g. `-DdebugParser=true`), the exception that occured when loading using Camel's resource" + " loader follows", e);
}
}
use of io.swagger.parser.SwaggerParser in project swagger-parser by swagger-api.
the class SwaggerDeserializerTest method testArrayModelDefinition.
@Test
public void testArrayModelDefinition() {
String json = "{\n" + " \"paths\": {\n" + " \"/store/inventory\": {\n" + " \"get\": {\n" + " \"responses\": {\n" + " \"200\": {\n" + " \"description\": \"successful operation\",\n" + " \"schema\": {\n" + " \"type\": \"object\",\n" + " \"additionalProperties\": {\n" + " \"type\": \"integer\",\n" + " \"format\": \"int32\"\n" + " }\n" + " }\n" + " }\n" + " }\n" + " }\n" + " }\n" + " }\n" + "}";
SwaggerParser parser = new SwaggerParser();
SwaggerDeserializationResult result = parser.readWithInfo(json);
List<String> messageList = result.getMessages();
Set<String> messages = new HashSet<String>(messageList);
Swagger swagger = result.getSwagger();
Property response = swagger.getPath("/store/inventory").getGet().getResponses().get("200").getSchema();
assertTrue(response instanceof MapProperty);
}
use of io.swagger.parser.SwaggerParser in project swagger-parser by swagger-api.
the class SwaggerDeserializerTest method testIssue386.
@Test
public void testIssue386() {
String yaml = "swagger: '2.0'\n" + "info:\n" + " description: 'bleh'\n" + " version: '2.0.0'\n" + " title: 'Test'\n" + "paths:\n" + " /foo:\n" + " post:\n" + " parameters:\n" + " - in: body\n" + " name: ugly\n" + " schema:\n" + " type: object\n" + " enum:\n" + " - id: fun\n" + " properties:\n" + " id:\n" + " type: string\n" + " responses:\n" + " 200:\n" + " description: 'OK'\n" + "definitions:\n" + " Fun:\n" + " type: object\n" + " properties:\n" + " complex:\n" + " enum:\n" + " - id: 110\n" + " type: object\n" + " properties:\n" + " id:\n" + " type: string\n" + " MyEnum:\n" + " type: integer\n" + " enum:\n" + " - value: 3\n" + " description: Value 1\n" + " - value: 10\n" + " description: Value 2";
SwaggerParser parser = new SwaggerParser();
SwaggerDeserializationResult result = parser.readWithInfo(yaml);
Swagger swagger = result.getSwagger();
assertNotNull(swagger);
}
use of io.swagger.parser.SwaggerParser in project swagger-parser by swagger-api.
the class SwaggerDeserializerTest method testPR246.
@Test
public void testPR246() throws Exception {
String yaml = "swagger: '2.0'\n" + "info:\n" + " description: 'Tests the allOf API for parent, interface and child models.'\n" + " version: '2.0.0'\n" + " title: 'Test allOf API'\n" + "paths:\n" + " /:\n" + " get:\n" + " parameters: []\n" + " responses:\n" + " 200:\n" + " description: 'OK'\n" + " parameters: []\n" + "definitions:\n" + " Pet:\n" + " type: 'object'\n" + " required:\n" + " - 'id'\n" + " properties:\n" + " id:\n" + " type: 'integer'\n" + " format: 'int64'\n" + " Furry:\n" + " type: 'object'\n" + " required:\n" + " - 'coatColour'\n" + " properties:\n" + " coatColour:\n" + " type: 'string'\n" + " Dog:\n" + " allOf:\n" + " - $ref: '#/definitions/Pet'\n" + " - $ref: '#/definitions/Furry'\n" + " - type: object\n" + " required:\n" + " - 'name'\n" + " properties:\n" + " name:\n" + " type: 'string'\n" + "";
SwaggerParser parser = new SwaggerParser();
SwaggerDeserializationResult result = parser.readWithInfo(yaml);
Swagger swagger = result.getSwagger();
Model dog = swagger.getDefinitions().get("Dog");
assertNotNull(dog);
assertTrue(dog instanceof ComposedModel);
ComposedModel composed = (ComposedModel) dog;
assertTrue(composed.getChild() instanceof ModelImpl);
assertTrue(composed.getInterfaces().size() == 2);
}
use of io.swagger.parser.SwaggerParser in project swagger-parser by swagger-api.
the class SwaggerDeserializerTest method testDefinitions.
@Test
public void testDefinitions() {
String json = "{\n" + " \"swagger\": \"2.0\",\n" + " \"definitions\": {\n" + " \"invalid\": true,\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" + " }\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(messages.contains("attribute definitions.invalid is not of type `object`"));
assertTrue(result.getSwagger().getDefinitions().get("Person") instanceof ModelImpl);
List<String> required = ((ModelImpl) result.getSwagger().getDefinitions().get("Person")).getRequired();
Set<String> requiredKeys = new HashSet<String>(required);
assertTrue(requiredKeys.contains("id"));
assertTrue(requiredKeys.contains("name"));
assertTrue(requiredKeys.size() == 2);
}
Aggregations