Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore invalid syntax #665

Closed
SinoAHpx opened this issue Jan 6, 2022 · 5 comments
Closed

Ignore invalid syntax #665

SinoAHpx opened this issue Jan 6, 2022 · 5 comments

Comments

@SinoAHpx
Copy link

SinoAHpx commented Jan 6, 2022

Is your feature request related to a problem? Please describe.
I have a yaml file:

adapters:
  - http
  - ws
enableVerify: true
verifyKey: 1145141919810
debug: false
singleMode: false
cacheSize: 4096
adapterSettings:
  http:
    host: localhost
    port: 8080
    cors: [*]
  ws:
    host: localhost
    port: 8080
    reservedSyncId: -1

And apparently this line cors: [*] is invalid, but I don't need to use this property so I deserialized them like this:

using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;

var deserializer = new DeserializerBuilder()
    .Build();
    
var yaml = deserializer.Deserialize<Dictionary<string, object>>("yaml content"));

Console.WriteLine(yaml);

When I deleted this invalid line, everything goes right, but I can't simply delete that line for my scenario requirements.

The exception will be thrown, stacktrace:

YamlDotNet.Core.SyntaxErrorException: (Line: 13, Col: 12, Idx: 188) - (Line: 13, Col: 13, Idx: 189): While scanning an anchor or alias, found value containing disallowed: []{},
   at YamlDotNet.Core.Scanner.ScanAnchor(Boolean isAlias)
   at YamlDotNet.Core.Scanner.FetchAnchor(Boolean isAlias)
   at YamlDotNet.Core.Scanner.FetchNextToken()
   at YamlDotNet.Core.Scanner.FetchMoreTokens()
   at YamlDotNet.Core.Scanner.MoveNextWithoutConsuming()
   at YamlDotNet.Core.Parser.GetCurrentToken()
   at YamlDotNet.Core.Parser.ParseFlowSequenceEntry(Boolean isFirst)
   at YamlDotNet.Core.Parser.StateMachine()
   at YamlDotNet.Core.Parser.MoveNext()
   at YamlDotNet.Core.ParserExtensions.Consume[T](IParser parser)
   at YamlDotNet.Serialization.NodeDeserializers.CollectionNodeDeserializer.DeserializeHelper(Type tItem, IParser parser, Func`3 nestedObjectDeserializer, IList result, Boolean canUpdate)
   at YamlDotNet.Serialization.NodeDeserializers.CollectionNodeDeserializer.YamlDotNet.Serialization.INodeDeserializer.Deserialize(IParser parser, Type expectedType, Func`3 nestedObjectDeserializer, Object& value)
   at YamlDotNet.Serialization.ValueDeserializers.NodeValueDeserializer.DeserializeValue(IParser parser, Type expectedType, SerializerState state, IValueDeserializer nestedObjectDeserializer)
   at YamlDotNet.Serialization.ValueDeserializers.AliasValueDeserializer.DeserializeValue(IParser parser, Type expectedType, SerializerState state, IValueDeserializer nestedObjectDeserializer)
   at YamlDotNet.Serialization.ValueDeserializers.NodeValueDeserializer.<>c__DisplayClass3_0.<DeserializeValue>b__0(IParser r, Type t)
   at YamlDotNet.Serialization.NodeDeserializers.DictionaryNodeDeserializer.DeserializeHelper(Type tKey, Type tValue, IParser parser, Func`3 nestedObjectDeserializer, IDictionary result)
   at YamlDotNet.Serialization.NodeDeserializers.DictionaryNodeDeserializer.YamlDotNet.Serialization.INodeDeserializer.Deserialize(IParser parser, Type expectedType, Func`3 nestedObjectDeserializer, Object& value)
   at YamlDotNet.Serialization.ValueDeserializers.NodeValueDeserializer.DeserializeValue(IParser parser, Type expectedType, SerializerState state, IValueDeserializer nestedObjectDeserializer)
   at YamlDotNet.Serialization.ValueDeserializers.AliasValueDeserializer.DeserializeValue(IParser parser, Type expectedType, SerializerState state, IValueDeserializer nestedObjectDeserializer)
   at YamlDotNet.Serialization.ValueDeserializers.NodeValueDeserializer.<>c__DisplayClass3_0.<DeserializeValue>b__0(IParser r, Type t)
   at YamlDotNet.Serialization.NodeDeserializers.DictionaryNodeDeserializer.DeserializeHelper(Type tKey, Type tValue, IParser parser, Func`3 nestedObjectDeserializer, IDictionary result)
   at YamlDotNet.Serialization.NodeDeserializers.DictionaryNodeDeserializer.YamlDotNet.Serialization.INodeDeserializer.Deserialize(IParser parser, Type expectedType, Func`3 nestedObjectDeserializer, Object& value)
   at YamlDotNet.Serialization.ValueDeserializers.NodeValueDeserializer.DeserializeValue(IParser parser, Type expectedType, SerializerState state, IValueDeserializer nestedObjectDeserializer)
   at YamlDotNet.Serialization.ValueDeserializers.AliasValueDeserializer.DeserializeValue(IParser parser, Type expectedType, SerializerState state, IValueDeserializer nestedObjectDeserializer)
   at YamlDotNet.Serialization.ValueDeserializers.NodeValueDeserializer.<>c__DisplayClass3_0.<DeserializeValue>b__0(IParser r, Type t)
   at YamlDotNet.Serialization.NodeDeserializers.DictionaryNodeDeserializer.DeserializeHelper(Type tKey, Type tValue, IParser parser, Func`3 nestedObjectDeserializer, IDictionary result)
   at YamlDotNet.Serialization.NodeDeserializers.DictionaryNodeDeserializer.YamlDotNet.Serialization.INodeDeserializer.Deserialize(IParser parser, Type expectedType, Func`3 nestedObjectDeserializer, Object& value)
   at YamlDotNet.Serialization.ValueDeserializers.NodeValueDeserializer.DeserializeValue(IParser parser, Type expectedType, SerializerState state, IValueDeserializer nestedObjectDeserializer)
   at YamlDotNet.Serialization.ValueDeserializers.AliasValueDeserializer.DeserializeValue(IParser parser, Type expectedType, SerializerState state, IValueDeserializer nestedObjectDeserializer)
   at YamlDotNet.Serialization.Deserializer.Deserialize(IParser parser, Type type)
   at YamlDotNet.Serialization.Deserializer.Deserialize[T](IParser parser)
   at YamlDotNet.Serialization.Deserializer.Deserialize[T](TextReader input)
   at YamlDotNet.Serialization.Deserializer.Deserialize[T](String input)
   at Program.<Main>$(String[] args) in E:\CSharp\Mirai.Net.Yaml\Mirai.Net.Yaml.Test\Program.cs:line 15

Describe the solution you'd like

So in my expectation, it would be like:

var deserializer = new DeserializerBuilder()
    .IgnoreInvalidSyntax() //or whatever name
    .Build();
@yamldotnet
Copy link

adapters:
  - http
  - ws
enableVerify: true
verifyKey: 1145141919810
debug: false
singleMode: false
cacheSize: 4096
adapterSettings:
  http:
    host: localhost
    port: 8080
    cors: [*]
  ws:
    host: localhost
    port: 8080
    reservedSyncId: -1

This YAML snippet appears to be invalid.
The following errors were identifier by the reference parser:

  • Line 13, char 12: Unexpected '*'

@krisrok
Copy link

krisrok commented Jan 6, 2022

As it's no valid YAML your best bet would be to strip the offending line before parsing.

@SinoAHpx
Copy link
Author

SinoAHpx commented Jan 6, 2022

As it's no valid YAML your best bet would be to strip the offending line before parsing.

yeah i have a dumb way to implement this, just parsing line by line and if cached the invalid line then remove it

@krisrok
Copy link

krisrok commented Jan 6, 2022

I've just had a look at the Parser code and found while the provided YAML is indeed invalid, the exception's message could still be clearer: It throws because the anchor (starting with *) is empty, not because of the following invalid char (]). In this particular case it's not much of a difference because it'd throw because of the ] anyway :) -- but in other scenarios it might be good to provide a more helpful message to the user.

@EdwardCooke
Copy link
Collaborator

We should not be parsing invalid YAML. There would be no guarantee that anything after the invalid set of characters would be accurate. I'm going to close this issue as it is not a bug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants