private static boolean append(final List<Violation> violations, final ProcessingMessage message, final Optional<String> requiredLevel) { if (requiredLevel.isPresent()) { final Optional<String> level = ofNullable(message.asJson()).flatMap(node -> ofNullable(node.get("level"))) .flatMap(node -> ofNullable(node.textValue())); if (!level.equals(requiredLevel)) { return false; // skip } } final Optional<String> property = ofNullable(message.asJson()).flatMap(node -> ofNullable(node.get("instance"))) .flatMap(node -> ofNullable(node.get("pointer"))).flatMap(node -> ofNullable(node.textValue())); final Optional<String> error = ofNullable(message.asJson()).flatMap(node -> ofNullable(node.get("domain"))) .flatMap(node -> ofNullable(node.textValue())); violations.add(new Violation.Builder().error(error.orElse("")).message(message.getMessage()).property(property.orElse("")).build()); return true; }
private static boolean append(final List<Violation> violations, final ProcessingMessage message, final Optional<String> requiredLevel) { if (requiredLevel.isPresent()) { final Optional<String> level = ofNullable(message.asJson()).flatMap(node -> ofNullable(node.get("level"))) .flatMap(node -> ofNullable(node.textValue())); if (!level.equals(requiredLevel)) { return false; // skip } } final Optional<String> property = ofNullable(message.asJson()).flatMap(node -> ofNullable(node.get("instance"))) .flatMap(node -> ofNullable(node.get("pointer"))).flatMap(node -> ofNullable(node.textValue())); final Optional<String> error = ofNullable(message.asJson()).flatMap(node -> ofNullable(node.get("domain"))) .flatMap(node -> ofNullable(node.textValue())); violations.add(new Violation.Builder().error(error.orElse("")).message(message.getMessage()).property(property.orElse("")).build()); return true; }
+ " does not provide a schema for the body parameter"; withWarnings.addWarning(new Violation.Builder()// .property("")// .error("missing-parameter-schema")// + " does not provide a response schema for code " + responseEntry.getKey(); withWarnings.addWarning(new Violation.Builder()// .property("")// .error("missing-response-schema")//
+ " does not provide a schema for the body parameter"; withWarnings.addWarning(new Violation.Builder()// .property("")// .error("missing-parameter-schema")// + " does not provide a response schema for code " + responseEntry.getKey(); withWarnings.addWarning(new Violation.Builder()// .property("")// .error("missing-response-schema")//
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(); }
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 (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(); }
/** * Check if all operations contains valid authentication types */ private SwaggerModelInfo validateAuthTypes(final SwaggerModelInfo swaggerModelInfo) { if (swaggerModelInfo.getModel() == null) { return swaggerModelInfo; } final SwaggerModelInfo.Builder withWarnings = new SwaggerModelInfo.Builder().createFrom(swaggerModelInfo); for (final Map.Entry<String, SecuritySchemeDefinition> definitionEntry : notNull( swaggerModelInfo.getModel().getSecurityDefinitions()).entrySet()) { final String authType = definitionEntry.getValue().getType(); if (!SUPPORTED_AUTH_TYPES.contains(authType)) { withWarnings.addWarning(new Violation.Builder()// .property("")// .error("unsupported-auth")// .message("Authentication type " + authType + " is currently not supported")// .build()); } } return withWarnings.build(); }
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(); } }
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(); } } }
/** * Check if all operations contains valid authentication types */ private SwaggerModelInfo validateAuthTypes(final SwaggerModelInfo swaggerModelInfo) { if (swaggerModelInfo.getModel() == null) { return swaggerModelInfo; } final SwaggerModelInfo.Builder withWarnings = new SwaggerModelInfo.Builder().createFrom(swaggerModelInfo); for (final Map.Entry<String, SecuritySchemeDefinition> definitionEntry : notNull( swaggerModelInfo.getModel().getSecurityDefinitions()).entrySet()) { final String authType = definitionEntry.getValue().getType(); if (!SUPPORTED_AUTH_TYPES.contains(authType)) { withWarnings.addWarning(new Violation.Builder()// .property("")// .error("unsupported-auth")// .message("Authentication type " + authType + " is currently not supported")// .build()); } } return withWarnings.build(); }