aaubry / YamlDotNet

YamlDotNet is a .NET library for YAML
MIT License
2.53k stars 471 forks source link

I am getting property not found error #944

Closed akshaysarode31 closed 1 month ago

akshaysarode31 commented 1 month ago

Below is my Selectors.yaml

LoginForm:
  UsernameInput:
    Type: css
    Value: '#username'
  PasswordInput:
    Type: css
    Value: '#password'
  SubmitButton:
    Type: css
    Value: '#submit'
   public static class SelectorsLoader
{
    public static IDictionary<string, IDictionary<string, Selector>> LoadSelectors(string path)
    {
        var deserializer = new DeserializerBuilder()
            .WithNamingConvention(CamelCaseNamingConvention.Instance)
            .Build();

        using var reader = new StreamReader(path);
        var selectors = deserializer.Deserialize<Dictionary<string, Dictionary<string, Selector>>>(reader);

        // Explicitly convert to IDictionary
        IDictionary<string, IDictionary<string, Selector>> result = new Dictionary<string, IDictionary<string, Selector>>();
        foreach (var kvp in selectors)
        {
            result[kvp.Key] = new Dictionary<string, Selector>(kvp.Value);
        }

        return result;
    }
}

public class Selector
{
    public string Type { get; set; }
    public string Value { get; set; }
}
}

While running this code at line var selectors = deserializer.Deserialize<Dictionary<string, Dictionary<string, Selector>>>(reader); I am getting error

YamlDotNet.Core.YamlException : Property 'Type' not found on type 'PlaywrightFramework.Helpers.Selector'.

Could you please help me with this and suggest what wrong I am doing here?

EdwardCooke commented 1 month ago

Try removing the WithNamingConvention from your DeserializerBuilder

akshaysarode31 commented 1 month ago

@EdwardCooke This helped Thank you. Just out of curiosity how was WithNamingConventioncausing the issue?

EdwardCooke commented 1 month ago

The naming conventions aren’t the most reliable when deserializing. It is a bit surprising that it didn’t work because it looked like it should have based on the yaml and property names.

EdwardCooke commented 1 month ago

I realized why it didn’t work and why the error was confusing. When the error said Type with the capital T it was referring to the key in the yaml. Not the property on the class. With camel case naming convention it was expecting the key to be type in the yaml with a lowercase t.

I’m closing this since it’s complete.

akshaysarode31 commented 1 month ago

I realized why it didn’t work and why the error was confusing. When the error said Type with the capital T it was referring to the key in the yaml. Not the property on the class. With camel case naming convention it was expecting the key to be type in the yaml with a lowercase t.

I’m closing this since it’s complete.

This makes sense. Thank you looking back into this.