From afa8e56b4edcbf920a780219ee08d341ef35d26c Mon Sep 17 00:00:00 2001 From: Takuro Wada Date: Tue, 2 May 2017 16:11:10 +0900 Subject: [PATCH] Add validate option to swagger-codegen-cli (Issue #5466) (#5526) --- .../io/swagger/codegen/SwaggerCodegen.java | 2 + .../java/io/swagger/codegen/cmd/Validate.java | 37 +++++++++++++++++++ .../codegen/cmd/ValidateException.java | 7 ++++ 3 files changed, 46 insertions(+) create mode 100644 modules/swagger-codegen-cli/src/main/java/io/swagger/codegen/cmd/Validate.java create mode 100644 modules/swagger-codegen-cli/src/main/java/io/swagger/codegen/cmd/ValidateException.java diff --git a/modules/swagger-codegen-cli/src/main/java/io/swagger/codegen/SwaggerCodegen.java b/modules/swagger-codegen-cli/src/main/java/io/swagger/codegen/SwaggerCodegen.java index 94b64be8d02..1b73ec6e98c 100644 --- a/modules/swagger-codegen-cli/src/main/java/io/swagger/codegen/SwaggerCodegen.java +++ b/modules/swagger-codegen-cli/src/main/java/io/swagger/codegen/SwaggerCodegen.java @@ -6,6 +6,7 @@ import io.swagger.codegen.cmd.ConfigHelp; import io.swagger.codegen.cmd.Generate; import io.swagger.codegen.cmd.Langs; import io.swagger.codegen.cmd.Meta; +import io.swagger.codegen.cmd.Validate; import io.swagger.codegen.cmd.Version; /** @@ -35,6 +36,7 @@ public class SwaggerCodegen { Langs.class, Help.class, ConfigHelp.class, + Validate.class, Version.class ); diff --git a/modules/swagger-codegen-cli/src/main/java/io/swagger/codegen/cmd/Validate.java b/modules/swagger-codegen-cli/src/main/java/io/swagger/codegen/cmd/Validate.java new file mode 100644 index 00000000000..832e008f282 --- /dev/null +++ b/modules/swagger-codegen-cli/src/main/java/io/swagger/codegen/cmd/Validate.java @@ -0,0 +1,37 @@ +package io.swagger.codegen.cmd; + +import io.airlift.airline.Command; +import io.airlift.airline.Option; + +import io.swagger.parser.SwaggerParser; +import io.swagger.parser.util.SwaggerDeserializationResult; + +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +@Command(name = "validate", description = "Validate specification") +public class Validate implements Runnable { + + @Option(name = {"-i", "--input-spec"}, title = "spec file", required = true, + description = "location of the swagger spec, as URL or file (required)") + private String spec; + + @Override + public void run() { + System.out.println("Validating spec file (" + spec + ")"); + + SwaggerParser parser = new SwaggerParser(); + SwaggerDeserializationResult result = parser.readWithInfo(spec, null, true); + List messageList = result.getMessages(); + Set messages = new HashSet(messageList); + + for (String message: messages) { + System.out.println(message); + } + + if (messages.size() > 0) { + throw new ValidateException(); + } + } +} \ No newline at end of file diff --git a/modules/swagger-codegen-cli/src/main/java/io/swagger/codegen/cmd/ValidateException.java b/modules/swagger-codegen-cli/src/main/java/io/swagger/codegen/cmd/ValidateException.java new file mode 100644 index 00000000000..f1715827ba9 --- /dev/null +++ b/modules/swagger-codegen-cli/src/main/java/io/swagger/codegen/cmd/ValidateException.java @@ -0,0 +1,7 @@ +package io.swagger.codegen.cmd; + +/** + * Created by takuro on 2017/05/02. + */ +public class ValidateException extends RuntimeException { +}