forked from loafle/openapi-generator-original
Merge remote-tracking branch 'origin/3.1.x' into 4.0.x
# Conflicts: # samples/server/petstore/kotlin-server/ktor/.openapi-generator/VERSION # samples/server/petstore/kotlin-server/ktor/README.md # samples/server/petstore/php-silex/.openapi-generator/VERSION
This commit is contained in:
@@ -21,7 +21,9 @@ import io.airlift.airline.Command;
|
||||
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;
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user