From 0c0c32f98220ebd36f557a6d7a175b5ff550c798 Mon Sep 17 00:00:00 2001 From: Jim Schubert Date: Tue, 12 Jun 2018 22:20:31 -0400 Subject: [PATCH] [cli] Enable recommendations on validate command --- .../openapitools/codegen/cmd/Validate.java | 50 ++++++++++++++++--- 1 file changed, 42 insertions(+), 8 deletions(-) diff --git a/modules/openapi-generator-cli/src/main/java/org/openapitools/codegen/cmd/Validate.java b/modules/openapi-generator-cli/src/main/java/org/openapitools/codegen/cmd/Validate.java index 4da5b318c6a1..fc8624586d6a 100644 --- a/modules/openapi-generator-cli/src/main/java/org/openapitools/codegen/cmd/Validate.java +++ b/modules/openapi-generator-cli/src/main/java/org/openapitools/codegen/cmd/Validate.java @@ -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; @@ -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 messageList = result.getMessages(); - Set messages = new HashSet(messageList); + Set errors = new HashSet(messageList); + Set warnings = new HashSet(); - 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 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()); } }