Search in sources :

Example 1 with Swagger

use of io.swagger.models.Swagger in project camel by apache.

the class RestSwaggerReaderApiDocsOverrideTest method testReaderRead.

@Test
public void testReaderRead() throws Exception {
    BeanConfig config = new BeanConfig();
    config.setHost("localhost:8080");
    config.setSchemes(new String[] { "http" });
    config.setBasePath("/api");
    RestSwaggerReader reader = new RestSwaggerReader();
    Swagger swagger = reader.read(context.getRestDefinitions(), null, config, context.getName(), new DefaultClassResolver());
    assertNotNull(swagger);
    ObjectMapper mapper = new ObjectMapper();
    mapper.enable(SerializationFeature.INDENT_OUTPUT);
    mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    String json = mapper.writeValueAsString(swagger);
    log.info(json);
    assertFalse(json.contains("\"/hello/bye\""));
    assertFalse(json.contains("\"summary\" : \"To update the greeting message\""));
    assertTrue(json.contains("\"/hello/bye/{name}\""));
    assertFalse(json.contains("\"/hello/hi/{name}\""));
    context.stop();
}
Also used : BeanConfig(io.swagger.jaxrs.config.BeanConfig) DefaultClassResolver(org.apache.camel.impl.DefaultClassResolver) Swagger(io.swagger.models.Swagger) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 2 with Swagger

use of io.swagger.models.Swagger in project camel by apache.

the class RestSwaggerReaderModelTest method testReaderRead.

@Test
public void testReaderRead() throws Exception {
    BeanConfig config = new BeanConfig();
    config.setHost("localhost:8080");
    config.setSchemes(new String[] { "http" });
    config.setBasePath("/api");
    config.setTitle("Camel User store");
    config.setLicense("Apache 2.0");
    config.setLicenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html");
    RestSwaggerReader reader = new RestSwaggerReader();
    Swagger swagger = reader.read(context.getRestDefinitions(), null, config, context.getName(), new DefaultClassResolver());
    assertNotNull(swagger);
    ObjectMapper mapper = new ObjectMapper();
    mapper.enable(SerializationFeature.INDENT_OUTPUT);
    mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    String json = mapper.writeValueAsString(swagger);
    log.info(json);
    assertTrue(json.contains("\"host\" : \"localhost:8080\""));
    assertTrue(json.contains("\"description\" : \"The user returned\""));
    assertTrue(json.contains("\"$ref\" : \"#/definitions/User\""));
    assertTrue(json.contains("\"x-className\""));
    assertTrue(json.contains("\"format\" : \"org.apache.camel.swagger.User\""));
    assertFalse(json.contains("\"enum\""));
    context.stop();
}
Also used : BeanConfig(io.swagger.jaxrs.config.BeanConfig) DefaultClassResolver(org.apache.camel.impl.DefaultClassResolver) Swagger(io.swagger.models.Swagger) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 3 with Swagger

use of io.swagger.models.Swagger in project camel by apache.

the class RestSwaggerReaderTest method testReaderRead.

@Test
public void testReaderRead() throws Exception {
    BeanConfig config = new BeanConfig();
    config.setHost("localhost:8080");
    config.setSchemes(new String[] { "http" });
    config.setBasePath("/api");
    RestSwaggerReader reader = new RestSwaggerReader();
    Swagger swagger = reader.read(context.getRestDefinitions(), null, config, context.getName(), new DefaultClassResolver());
    assertNotNull(swagger);
    ObjectMapper mapper = new ObjectMapper();
    mapper.enable(SerializationFeature.INDENT_OUTPUT);
    mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    String json = mapper.writeValueAsString(swagger);
    log.info(json);
    assertTrue(json.contains("\"host\" : \"localhost:8080\""));
    assertTrue(json.contains("\"basePath\" : \"/api\""));
    assertTrue(json.contains("\"/hello/bye\""));
    assertTrue(json.contains("\"summary\" : \"To update the greeting message\""));
    assertTrue(json.contains("\"/hello/bye/{name}\""));
    assertTrue(json.contains("\"/hello/hi/{name}\""));
    context.stop();
}
Also used : BeanConfig(io.swagger.jaxrs.config.BeanConfig) DefaultClassResolver(org.apache.camel.impl.DefaultClassResolver) Swagger(io.swagger.models.Swagger) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 4 with Swagger

use of io.swagger.models.Swagger in project camel by apache.

the class RestSwaggerEndpoint method createProducer.

@Override
public Producer createProducer() throws Exception {
    final CamelContext camelContext = getCamelContext();
    final Swagger swagger = loadSpecificationFrom(camelContext, specificationUri);
    final Map<String, Path> paths = swagger.getPaths();
    for (final Entry<String, Path> pathEntry : paths.entrySet()) {
        final Path path = pathEntry.getValue();
        final Optional<Entry<HttpMethod, Operation>> maybeOperationEntry = path.getOperationMap().entrySet().stream().filter(operationEntry -> operationId.equals(operationEntry.getValue().getOperationId())).findAny();
        if (maybeOperationEntry.isPresent()) {
            final Entry<HttpMethod, Operation> operationEntry = maybeOperationEntry.get();
            final String uriTemplate = pathEntry.getKey();
            final HttpMethod httpMethod = operationEntry.getKey();
            final String method = httpMethod.name();
            final Operation operation = operationEntry.getValue();
            return createProducerFor(swagger, operation, method, uriTemplate);
        }
    }
    final String supportedOperations = paths.values().stream().flatMap(p -> p.getOperations().stream()).map(Operation::getOperationId).collect(Collectors.joining(", "));
    throw new IllegalArgumentException("The specified operation with ID: `" + operationId + "` cannot be found in the Swagger specification loaded from `" + specificationUri + "`. Operations defined in the specification are: " + supportedOperations);
}
Also used : CamelContext(org.apache.camel.CamelContext) UriPath(org.apache.camel.spi.UriPath) Path(io.swagger.models.Path) Scheme(io.swagger.models.Scheme) SwaggerParser(io.swagger.parser.SwaggerParser) Metadata(org.apache.camel.spi.Metadata) UriPath(org.apache.camel.spi.UriPath) URISyntaxException(java.net.URISyntaxException) Swagger(io.swagger.models.Swagger) Json(io.swagger.util.Json) StringHelper.before(org.apache.camel.util.StringHelper.before) HashMap(java.util.HashMap) Endpoint(org.apache.camel.Endpoint) Consumer(org.apache.camel.Consumer) ObjectHelper.notNull(org.apache.camel.util.ObjectHelper.notNull) StringHelper.notEmpty(org.apache.camel.util.StringHelper.notEmpty) UriEndpoint(org.apache.camel.spi.UriEndpoint) Processor(org.apache.camel.Processor) UriParam(org.apache.camel.spi.UriParam) ObjectHelper.isNotEmpty(org.apache.camel.util.ObjectHelper.isNotEmpty) Path(io.swagger.models.Path) RestSwaggerHelper.isHostParam(org.apache.camel.component.rest.swagger.RestSwaggerHelper.isHostParam) Map(java.util.Map) ResourceHelper(org.apache.camel.util.ResourceHelper) JsonNode(com.fasterxml.jackson.databind.JsonNode) URI(java.net.URI) Operation(io.swagger.models.Operation) RestSwaggerHelper.isMediaRange(org.apache.camel.component.rest.swagger.RestSwaggerHelper.isMediaRange) ExchangePattern(org.apache.camel.ExchangePattern) CamelContext(org.apache.camel.CamelContext) StringHelper.after(org.apache.camel.util.StringHelper.after) DefaultEndpoint(org.apache.camel.impl.DefaultEndpoint) Optional.ofNullable(java.util.Optional.ofNullable) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) Producer(org.apache.camel.Producer) List(java.util.List) Entry(java.util.Map.Entry) Optional(java.util.Optional) HttpMethod(io.swagger.models.HttpMethod) InputStream(java.io.InputStream) RestConfiguration(org.apache.camel.spi.RestConfiguration) Operation(io.swagger.models.Operation) Entry(java.util.Map.Entry) Swagger(io.swagger.models.Swagger) HttpMethod(io.swagger.models.HttpMethod)

Example 5 with Swagger

use of io.swagger.models.Swagger 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);
    }
}
Also used : SwaggerParser(io.swagger.parser.SwaggerParser) InputStream(java.io.InputStream) Swagger(io.swagger.models.Swagger) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

Swagger (io.swagger.models.Swagger)164 Test (org.testng.annotations.Test)114 Operation (io.swagger.models.Operation)39 Parameter (io.swagger.models.parameters.Parameter)30 QueryParameter (io.swagger.models.parameters.QueryParameter)30 BodyParameter (io.swagger.models.parameters.BodyParameter)26 Reader (io.swagger.jaxrs.Reader)23 PathParameter (io.swagger.models.parameters.PathParameter)22 SpecFilter (io.swagger.core.filter.SpecFilter)21 Test (org.junit.Test)19 Path (io.swagger.models.Path)18 Response (io.swagger.models.Response)14 SerializableParameter (io.swagger.models.parameters.SerializableParameter)14 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)10 Model (io.swagger.models.Model)10 NicknamedOperation (io.swagger.resources.NicknamedOperation)10 HeaderParameter (io.swagger.models.parameters.HeaderParameter)9 BeanConfig (io.swagger.jaxrs.config.BeanConfig)8 Map (java.util.Map)8 Info (io.swagger.models.Info)7