Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[cli] Enable recommendations on validate command #292

Merged
merged 1 commit into from
Jun 17, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
import io.airlift.airline.Option;

import io.swagger.parser.OpenAPIParser;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.parser.core.models.SwaggerParseResult;
import org.openapitools.codegen.utils.ModelUtils;

import java.util.HashSet;
import java.util.List;
Expand All @@ -34,26 +36,58 @@ public class Validate implements Runnable {
description = "location of the OpenAPI spec, as URL or file (required)")
private String spec;

@Option(name = { "--recommend"}, title = "recommend spec improvements")
private Boolean recommend;

@Override
public void run() {
System.out.println("Validating spec (" + spec + ")");

SwaggerParseResult result = new OpenAPIParser().readLocation(spec, null, null);
List<String> messageList = result.getMessages();
Set<String> messages = new HashSet<String>(messageList);
Set<String> errors = new HashSet<String>(messageList);
Set<String> warnings = new HashSet<String>();

if (messages.size() > 0) {
StringBuilder sb = new StringBuilder();
sb.append(System.lineSeparator());
for (String message : messages) {
sb.append(String.format("\t- %s%s", message, System.lineSeparator()));
StringBuilder sb = new StringBuilder();
OpenAPI specification = result.getOpenAPI();

if (Boolean.TRUE.equals(recommend)) {
if (specification != null) {
// Add information about unused models to the warnings set.
List<String> unusedModels = ModelUtils.getUnusedSchemas(specification);
if (unusedModels != null) {
unusedModels.forEach(name -> warnings.add("Unused model: " + name));
}
}
}

if (errors.size() > 0) {
sb.append("Errors:").append(System.lineSeparator());
errors.forEach(msg ->
sb.append("\t-").append(msg).append(System.lineSeparator())
);
}

if (!warnings.isEmpty()) {
sb.append("Warnings: ").append(System.lineSeparator());
warnings.forEach(msg ->
sb.append("\t-").append(msg).append(System.lineSeparator())
);
}

if (!errors.isEmpty()) {
sb.append(System.lineSeparator());
sb.append("[error] Spec is invalid.");
sb.append("[error] Spec has ").append(errors.size()).append(" errors.");
System.err.println(sb.toString());
System.exit(1);
} else if (!warnings.isEmpty()) {
sb.append(System.lineSeparator());
sb.append("[info] Spec has ").append(warnings.size()).append(" recommendation(s).");
} else {
System.out.println("No validation errors detected.");
// we say "issues" here rather than "errors" to account for both errors and issues.
sb.append("No validation issues detected.");
}

System.out.println(sb.toString());
}
}