weixin_39803552
weixin_39803552
2020-12-09 05:13

crashes if there is a YAML syntax error

I'm trying to use this as a raml-lint tool to do syntax checking on files (since the RAML API-Console does not help when there are syntax errors).

I wrote a main parser class


import org.raml.parser.visitor.RamlValidationService;
import org.raml.parser.rule.ValidationResult;
import java.util.List;

public class Parser {
  public static void main(String args[]) {
    for (String location : args) {
      List<validationresult> results = RamlValidationService.createDefault().validate(location);
      if (results != null) {
        for (ValidationResult result : results)
          System.err.println(result);
      }
    }
  }
}
</validationresult>

but when I run it on a resource with a yaml syntax error, I get an exception instead of a diagnostic that shows me where the error is:


Exception in thread "main" java.lang.NullPointerException
    at org.raml.parser.rule.ValidationResult.toString(ValidationResult.java:142)
    at java.lang.String.valueOf(String.java:2854)
    at java.io.PrintStream.println(PrintStream.java:821)
    at org.raml.parser.Parser.main(Parser.java:13)

because the result has a null contextPath

该提问来源于开源项目:raml-org/raml-java-parser

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

6条回答

  • weixin_39528467 weixin_39528467 5月前

    Maybe we should be running a YAML parser first. https://code.google.com/p/snakeyaml/

    Any opinions?

    点赞 评论 复制链接分享
  • weixin_39803552 weixin_39803552 5月前

    At the heart, this is already calling a yaml parser Node root = yamlParser.compose(content); where yamlParser is a org.yaml.snakeyaml.Yaml; It's just that if that throws an error, the contextPath is null in the ValidationResult.

    点赞 评论 复制链接分享
  • weixin_39860636 weixin_39860636 5月前

    Hi David, could you provide a yaml snippet to reproduce the error? I've tried with a couple of invalid yaml files with no success.

    点赞 评论 复制链接分享
  • weixin_39803552 weixin_39803552 5月前

    Use the main Parser class above on the below RAML source (which I deployed on a web server and invoked with

    
    RP=./raml-java-parser
    java -classpath $RP/target/dependency/\*:$RP/target/classes${CP} org.raml.parser.Parser "$inFile"
    

    36.raml:

    
    #%RAML 0.8
    ---
    title: TestCase 36
    version: 1
    protocols: [ HTTP ]
    baseUri: http://www.example.com/test/case/36
    mediaType: application/json
    /:
      get
        description: Return a collection of links to this APIs resources.
    

    (missing : after get)

    点赞 评论 复制链接分享
  • weixin_39860636 weixin_39860636 5月前

    I'm getting this error message generated by the snakeyaml parser:

    ERROR mapping values are not allowed here

    点赞 评论 复制链接分享
  • weixin_39803552 weixin_39803552 5月前

    Yes, this appears to be fixed in the most recent code - I no longer get an exception, juts a message.

    点赞 评论 复制链接分享

相关推荐