Handlebars-Net/Handlebars.Net

Get data from concret class were there is a JObject attribute is not working in 2.x

Closed this issue · 2 comments

Describe the bug

First of all, I would like to say thanks for the maintainers. The HD.NET is excellent package.
This test below was working until ver 1.11.5. After tests using 2.03, the error is happening.

Expected behavior:

Using the version 1.11.5 we do not have any problems in our tests answers.

Test to reproduce

        public class ClassOfTest
        {
            public ClassOfTest()
            {
                Parameters = new HashSet<Parameter>();
            }

            public string NameTest { get; set; } = "Nome1";

            public string AdressTest { get; set; } = "Endereco1";

            public DateTime DateTest { get; set; } = DateTime.Now;

            public ICollection<Parameter> Parameters { get; set; }

            public Guid CodeTest { get; set; } = Guid.NewGuid();

            public int AuxCodeTest { get; set; } = 0;

            public JObject ContentTest { get; set; } = JObject.FromObject(new { Nested1 = new { Prop = "Prop" }, Nested2 = (object)null });

            public bool ValidTest { get; set; } = false;
        }

        public class Parameter
        {
            public int Code { get; set; }

            public string Name { get; set; }
        }

        [Fact]
        public void UsingTheClassTestAsInputCheckingContentTest()
        {
            var source = "{{conTentTest}}";

            var handlebars = Handlebars.Create();

            var template = handlebars.Compile(source);
            var data = new ClassOfTest();

            var result = template(data);
            Assert.Equal(@"{
  &quot;Nested1&quot;: {
    &quot;Prop&quot;: &quot;Prop&quot;
  },
  &quot;Nested2&quot;: null
}", result);
        }

        [Fact]
        public void UsingTheClassTestAsInputCheckingContentTestNested1()
        {
            var source = "{{conTentTest.Nested1}}";
            var template = Handlebars.Compile(source);
            var data = new ClassOfTest();

            var result = template(data);
            Assert.Equal(@"{
  &quot;Prop&quot;: &quot;Prop&quot;
}", result);
        }

Other related info

HB is getting the data, but not all data, it is returning the data of attribute Prop.
In this test we have a JObject attribute that we can change, but during the PROD hours, the do not access to class to change them. I believe that the error could be when the HB is building the attribute answer.

After submitting the issue

Please consider contributing to the project by submitting a PR with a fix to the issue.
This would help to solve your problem in a shorter time as well as help other users of the project.

In case you do not know where to start - feel free to ask for help in the issue thread.

Building an active community is essential for any project survival as time of maintainers is limited.

Hello @josercsouza
This looks like an expected breaking change introduced in #390.
In most cases JObject requires special handling. You should have a look at Handlebars.Net.Extension.NewtonsoftJson in case you work closely with Newtonsoft.Json

@zjklee thanks for your prompt response.

I did the changes from your last pull request and now the tests are working fine using Handlebars.Net.Extension.NewtonsoftJson.
Like a tip, in the readme.md of HB include the topics to Helpers and Extentions (for the lost like me kkkkk).

Good job sir and success in 2021.