Add validate option to swagger-codegen-cli (Issue #5466) (#5526)

This commit is contained in:
Takuro Wada 2017-05-02 16:11:10 +09:00 committed by wing328
parent b7df040b6e
commit afa8e56b4e
3 changed files with 46 additions and 0 deletions

View File

@ -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
);

View File

@ -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<String> messageList = result.getMessages();
Set<String> messages = new HashSet<String>(messageList);
for (String message: messages) {
System.out.println(message);
}
if (messages.size() > 0) {
throw new ValidateException();
}
}
}

View File

@ -0,0 +1,7 @@
package io.swagger.codegen.cmd;
/**
* Created by takuro on 2017/05/02.
*/
public class ValidateException extends RuntimeException {
}