@Test public void shouldDetermineOperationDescriptions() { assertThat(SwaggerHelper.operationDescriptionOf(swagger, operation, (m, p) -> "Send " + m + " request to " + p)).isEqualTo(expected); }
static SwaggerModelInfo parseSpecification(final ConnectorSettings connectorSettings, final APIValidationContext validationContext) { final String specification = requiredSpecification(connectorSettings); return SwaggerHelper.parse(specification, validationContext); }
@Test public void shouldSanitizeListOfTags() { assertThat(SwaggerHelper.sanitizeTags(Arrays.asList("tag", "wag ", " bag", ".]t%a$g#[/"))) .containsExactly("tag", "wag", "bag"); } }
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 OpenAPI schema file embedded in the artifact").build()).build(); } }
final OperationDescription description = SwaggerHelper.operationDescriptionOf(swagger, operation, (m, p) -> "Send " + m + " request to " + p); .descriptor(descriptor).tags(SwaggerHelper.sanitizeTags(operation.getTags()).distinct()::iterator)// .build(); builder.addAllActions(actions); builder.putConfiguredProperty("specification", SwaggerHelper.minimalSwaggerUsedByComponent(swagger));
@Override public APISummary info(String specification, APIValidationContext validation) { final SwaggerModelInfo swaggerInfo = SwaggerHelper.parse(specification, validation); try { .flatMap(o -> SwaggerHelper.sanitizeTags(o.getTags()))//
@Override @SuppressWarnings({"PMD.ExcessiveMethodLength"}) public APIIntegration generateIntegration(String specification, ProvidedApiTemplate template) { SwaggerModelInfo info = SwaggerHelper.parse(specification, APIValidationContext.NONE); Swagger swagger = info.getModel(); operationName = SwaggerHelper.operationDescriptionOf( swagger, operation,
public static SwaggerModelInfo parse(final String specification, final APIValidationContext validationContext) { 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 OpenAPI specification\n{}\n", specification, e); return resultBuilder .addError(new Violation.Builder().error("error").property("").message("Unable to resolve OpenAPI 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 OpenAPI specification\n{}\n", specification); return resultBuilder .addError(new Violation.Builder().error("error").property("").message("Unable to read OpenAPI specification from: " + ofNullable(specification).map(s -> StringUtils.abbreviate(s, 100)).orElse("")).build()) .build(); } if (validationContext != APIValidationContext.NONE) { final SwaggerModelInfo swaggerModelInfo = validateJSonSchema(resolvedSpecification, swagger); return SyndesisSwaggerValidationRules.get(validationContext).apply(swaggerModelInfo); } return resultBuilder.model(swagger).build(); }
bodySchemaToUse = SwaggerHelper.dereference((RefModel) bodySchema, swagger); } else if (bodySchema instanceof ArrayModel) { final Property items = ((ArrayModel) bodySchema).getItems(); bodySchemaToUse = SwaggerHelper.dereference((RefProperty) items, swagger); } else { bodySchemaToUse = new ModelImpl();
@SuppressWarnings("PMD.SignatureDeclareThrowsException") static String resolve(final String specification) throws Exception { final String specificationToUse; if (specification.toLowerCase(Locale.US).startsWith("http")) { specificationToUse = RemoteUrl.urlToString(specification, null); } else { specificationToUse = specification; } final JsonNode node = convertToJson(specificationToUse); return Json.writer().writeValueAsString(node); }
@Test public void shouldSanitizeTags() { assertThat(SwaggerHelper.sanitizeTag("tag")).isEqualTo("tag"); assertThat(SwaggerHelper.sanitizeTag(".]t%a$g#[/")).isEqualTo("tag"); final char[] str = new char[1024]; final String randomString = IntStream.range(0, str.length) .map(x -> (int) (Character.MAX_CODE_POINT * Math.random())).mapToObj(i -> new String(Character.toChars(i))) .collect(Collectors.joining("")); final String sanitized = SwaggerHelper.sanitizeTag(randomString); assertThatCode(() -> JsonRecordSupport.validateKey(sanitized)).doesNotThrowAnyException(); }
private static ConnectorSettings createSettingsFrom(final Swagger swagger) { return new ConnectorSettings.Builder()// .putConfiguredProperty("specification", SwaggerHelper.serialize(swagger))// .build(); }
private static Element defineComplexElement(final RefProperty property, final Element parent, final Swagger swagger, final Map<String, SchemaPrefixAndElement> moreSchemas) { final ModelImpl model = SwaggerHelper.dereference(property, swagger);
@Test public void testThatWarningPetstoreSwaggerContainsWarnings() throws IOException { final String specification = resource("/swagger/invalid/warning-petstore.swagger.json"); final SwaggerModelInfo info = SwaggerHelper.parse(specification, APIValidationContext.CONSUMED_API); assertThat(info.getErrors()).isEmpty(); assertThat(info.getWarnings()).hasSize(3); }
.flatMap(o -> SwaggerHelper.sanitizeTags(o.getTags()).distinct())//
@Test public void testThatInvalidFieldPetstoreSwaggerIsInvalid() throws IOException { final String specification = resource("/swagger/invalid/invalid-field.petstore.swagger.json"); final SwaggerModelInfo info = SwaggerHelper.parse(specification, APIValidationContext.CONSUMED_API); assertThat(info.getErrors()).hasSize(1); assertThat(info.getWarnings()).isEmpty(); assertThat(info.getErrors().get(0).message()) .startsWith("object instance has properties which are not allowed by the schema"); assertThat(info.getErrors().get(0).property()).contains("/paths/~1pet/put"); assertThat(info.getErrors().get(0).error()).contains("validation"); }
@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, APIValidationContext.CONSUMED_API); assertThat(info.getErrors()) .withFailMessage("Specification " + specificationFile + " has errors: " + info.getErrors()).isEmpty(); } }
@Test public void testThatInvalidTypePetstoreSwaggerIsInvalid() throws IOException { final String specification = resource("/swagger/invalid/invalid-type.petstore.swagger.json"); final SwaggerModelInfo info = SwaggerHelper.parse(specification, APIValidationContext.CONSUMED_API); 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"); }
@Test public void testThatInvalidSchemePetstoreSwaggerIsInvalid() throws IOException { final String specification = resource("/swagger/invalid/invalid-scheme.petstore.swagger.json"); final SwaggerModelInfo info = SwaggerHelper.parse(specification, APIValidationContext.CONSUMED_API); assertThat(info.getErrors()).hasSize(1); assertThat(info.getWarnings()).hasSize(1); assertThat(info.getErrors().get(0).message()).startsWith("instance value (\"httpz\") not found in enum"); assertThat(info.getErrors().get(0).property()).contains("/schemes/0"); assertThat(info.getErrors().get(0).error()).contains("validation"); assertThat(info.getWarnings().get(0).message()).startsWith("Unable to determine the scheme"); assertThat(info.getWarnings().get(0).property()).contains("/schemes"); assertThat(info.getWarnings().get(0).error()).contains("missing-schemes"); }