Search in sources :

Example 1 with SwaggerModelInfo

use of io.syndesis.server.connector.generator.swagger.SwaggerModelInfo in project syndesis by syndesisio.

the class SwaggerHelperTest method testThatAllSwaggerFilesAreValid.

@Test
public void testThatAllSwaggerFilesAreValid() throws IOException {
    final String[] specifications = { "/swagger/concur.swagger.json", "/swagger/petstore.swagger.json", "/swagger/todo.swagger.yaml" };
    for (final String specificationFile : specifications) {
        final String specification = resource(specificationFile);
        final SwaggerModelInfo info = SwaggerHelper.parse(specification, true);
        assertThat(info.getErrors()).withFailMessage("Specification " + specificationFile + " has errors: " + info.getErrors()).isEmpty();
    }
}
Also used : SwaggerModelInfo(io.syndesis.server.connector.generator.swagger.SwaggerModelInfo) AbstractSwaggerConnectorTest(io.syndesis.server.connector.generator.swagger.AbstractSwaggerConnectorTest) Test(org.junit.Test)

Example 2 with SwaggerModelInfo

use of io.syndesis.server.connector.generator.swagger.SwaggerModelInfo in project syndesis by syndesisio.

the class SwaggerHelperTest method testThatWarningPetstoreSwaggerContainsWarnings.

@Test
public void testThatWarningPetstoreSwaggerContainsWarnings() throws IOException {
    final String specification = resource("/swagger/invalid/warning-petstore.swagger.json");
    final SwaggerModelInfo info = SwaggerHelper.parse(specification, true);
    assertThat(info.getErrors()).isEmpty();
    assertThat(info.getWarnings()).hasSize(3);
}
Also used : SwaggerModelInfo(io.syndesis.server.connector.generator.swagger.SwaggerModelInfo) AbstractSwaggerConnectorTest(io.syndesis.server.connector.generator.swagger.AbstractSwaggerConnectorTest) Test(org.junit.Test)

Example 3 with SwaggerModelInfo

use of io.syndesis.server.connector.generator.swagger.SwaggerModelInfo in project syndesis by syndesisio.

the class SwaggerHelper method validateJSonSchema.

private static SwaggerModelInfo validateJSonSchema(final String specification, final Swagger model) {
    try {
        final JsonNode specRoot = convertToJson(specification);
        final ProcessingReport report = SWAGGER_2_0_SCHEMA.validate(specRoot);
        final List<Violation> errors = new ArrayList<>();
        final List<Violation> warnings = new ArrayList<>();
        for (final ProcessingMessage message : report) {
            final boolean added = append(errors, message, Optional.of("error"));
            if (!added) {
                append(warnings, message, Optional.empty());
            }
        }
        return new SwaggerModelInfo.Builder().errors(errors).warnings(warnings).model(model).resolvedSpecification(specification).build();
    } catch (IOException | ProcessingException ex) {
        LOG.error("Unable to load the schema file embedded in the artifact", ex);
        return new SwaggerModelInfo.Builder().addError(new Violation.Builder().error("error").property("").message("Unable to load the swagger schema file embedded in the artifact").build()).build();
    }
}
Also used : Violation(io.syndesis.common.model.Violation) ProcessingMessage(com.github.fge.jsonschema.core.report.ProcessingMessage) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) SwaggerModelInfo(io.syndesis.server.connector.generator.swagger.SwaggerModelInfo) IOException(java.io.IOException) ProcessingReport(com.github.fge.jsonschema.core.report.ProcessingReport) ProcessingException(com.github.fge.jsonschema.core.exceptions.ProcessingException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 4 with SwaggerModelInfo

use of io.syndesis.server.connector.generator.swagger.SwaggerModelInfo in project syndesis by syndesisio.

the class SwaggerHelper method parse.

public static SwaggerModelInfo parse(final String specification, final boolean validate) {
    final SwaggerModelInfo.Builder resultBuilder = new SwaggerModelInfo.Builder();
    final String resolvedSpecification;
    try {
        resolvedSpecification = resolve(specification);
        resultBuilder.resolvedSpecification(resolvedSpecification);
    } catch (@SuppressWarnings("PMD.AvoidCatchingGenericException") final Exception e) {
        LOG.debug("Unable to resolve Swagger specification\n{}\n", specification, e);
        return resultBuilder.addError(new Violation.Builder().error("error").property("").message("Unable to resolve Swagger specification from: " + ofNullable(specification).map(s -> StringUtils.abbreviate(s, 100)).orElse("")).build()).build();
    }
    final SwaggerParser parser = new SwaggerParser();
    final Swagger swagger = parser.parse(resolvedSpecification);
    if (swagger == null) {
        LOG.debug("Unable to read Swagger specification\n{}\n", specification);
        return resultBuilder.addError(new Violation.Builder().error("error").property("").message("Unable to read Swagger specification from: " + ofNullable(specification).map(s -> StringUtils.abbreviate(s, 100)).orElse("")).build()).build();
    }
    if (validate) {
        final SwaggerModelInfo swaggerModelInfo = validateJSonSchema(resolvedSpecification, swagger);
        return SyndesisSwaggerValidationRules.getInstance().apply(swaggerModelInfo);
    }
    return resultBuilder.model(swagger).build();
}
Also used : Violation(io.syndesis.common.model.Violation) Violation(io.syndesis.common.model.Violation) SwaggerParser(io.swagger.parser.SwaggerParser) Swagger(io.swagger.models.Swagger) LoggerFactory(org.slf4j.LoggerFactory) ProcessingException(com.github.fge.jsonschema.core.exceptions.ProcessingException) ModelImpl(io.swagger.models.ModelImpl) StringUtils(org.apache.commons.lang3.StringUtils) ProcessingReport(com.github.fge.jsonschema.core.report.ProcessingReport) ArrayList(java.util.ArrayList) Yaml(org.yaml.snakeyaml.Yaml) Locale(java.util.Locale) Path(io.swagger.models.Path) RefProperty(io.swagger.models.properties.RefProperty) JsonNode(com.fasterxml.jackson.databind.JsonNode) JsonSchemaFactory(com.github.fge.jsonschema.main.JsonSchemaFactory) Operation(io.swagger.models.Operation) RefModel(io.swagger.models.RefModel) StringUtils.trimToNull(org.apache.commons.lang3.StringUtils.trimToNull) Logger(org.slf4j.Logger) Optional.ofNullable(java.util.Optional.ofNullable) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) List(java.util.List) SwaggerModelInfo(io.syndesis.server.connector.generator.swagger.SwaggerModelInfo) SyndesisSwaggerValidationRules(io.syndesis.server.connector.generator.swagger.SyndesisSwaggerValidationRules) ProcessingMessage(com.github.fge.jsonschema.core.report.ProcessingMessage) Entry(java.util.Map.Entry) RemoteUrl(io.swagger.parser.util.RemoteUrl) Optional(java.util.Optional) HttpMethod(io.swagger.models.HttpMethod) Json(io.syndesis.common.util.Json) JsonSchema(com.github.fge.jsonschema.main.JsonSchema) SwaggerParser(io.swagger.parser.SwaggerParser) Swagger(io.swagger.models.Swagger) SwaggerModelInfo(io.syndesis.server.connector.generator.swagger.SwaggerModelInfo) ProcessingException(com.github.fge.jsonschema.core.exceptions.ProcessingException) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 5 with SwaggerModelInfo

use of io.syndesis.server.connector.generator.swagger.SwaggerModelInfo in project syndesis by syndesisio.

the class SwaggerHelperTest method testThatInvalidTypePetstoreSwaggerIsInvalid.

@Test
public void testThatInvalidTypePetstoreSwaggerIsInvalid() throws IOException {
    final String specification = resource("/swagger/invalid/invalid-type.petstore.swagger.json");
    final SwaggerModelInfo info = SwaggerHelper.parse(specification, true);
    assertThat(info.getErrors()).hasSize(1);
    assertThat(info.getWarnings()).isEmpty();
    assertThat(info.getErrors().get(0).message()).startsWith("instance failed to match exactly one schema");
    assertThat(info.getErrors().get(0).property()).contains("/paths/~1pet~1{petId}/post/parameters/2");
    assertThat(info.getErrors().get(0).error()).contains("validation");
}
Also used : SwaggerModelInfo(io.syndesis.server.connector.generator.swagger.SwaggerModelInfo) AbstractSwaggerConnectorTest(io.syndesis.server.connector.generator.swagger.AbstractSwaggerConnectorTest) Test(org.junit.Test)

Aggregations

SwaggerModelInfo (io.syndesis.server.connector.generator.swagger.SwaggerModelInfo)7 AbstractSwaggerConnectorTest (io.syndesis.server.connector.generator.swagger.AbstractSwaggerConnectorTest)5 Test (org.junit.Test)5 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 ProcessingException (com.github.fge.jsonschema.core.exceptions.ProcessingException)2 ProcessingMessage (com.github.fge.jsonschema.core.report.ProcessingMessage)2 ProcessingReport (com.github.fge.jsonschema.core.report.ProcessingReport)2 Violation (io.syndesis.common.model.Violation)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 JsonSchema (com.github.fge.jsonschema.main.JsonSchema)1 JsonSchemaFactory (com.github.fge.jsonschema.main.JsonSchemaFactory)1 HttpMethod (io.swagger.models.HttpMethod)1 ModelImpl (io.swagger.models.ModelImpl)1 Operation (io.swagger.models.Operation)1 Path (io.swagger.models.Path)1 RefModel (io.swagger.models.RefModel)1 Swagger (io.swagger.models.Swagger)1