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

JObjectArray Stringify error #1005

Closed
hyzx86 opened this issue Nov 8, 2021 · 10 comments · Fixed by #1006
Closed

JObjectArray Stringify error #1005

hyzx86 opened this issue Nov 8, 2021 · 10 comments · Fixed by #1006

Comments

@hyzx86
Copy link
Contributor

hyzx86 commented Nov 8, 2021

OrchardCMS/OrchardCore#10648

      [Fact]
       public void EngineShouldStringifyJObjectArrayWithValuesCorrectly()
        {
           //https://github.com/OrchardCMS/OrchardCore/issues/10648
            var engine = new Engine();
            var source = new JObject[] {
                 JObject.FromObject(new { Text = "Text1", Value = 1 }),
                 JObject.FromObject(new { Text = "Text2", Value = 2 }) }; 

            engine.SetValue("testSubject", source);
            var fromEngine = engine.Evaluate("return JSON.stringify(testSubject);");
            var result = fromEngine.ToString();
            Assert.Equal(result, "[{\"Text\":\"Text1\",\"Value\":1},{\"Text\":\"Text2\",\"Value\":2}]");
        }

out put :

[{"Type":null,"HasValues":null,"First":null,"Last":null,"Count":null,"Parent":null,"Root":null,"Next":null,"Previous":null,"Path":null},{"Type":null,"HasValues":null,"First":null,"Last":null,"Count":null,"Parent":null,"Root":null,"Next":null,"Previous":null,"Path":null}]
@hyzx86
Copy link
Contributor Author

hyzx86 commented Nov 8, 2021

This test can be run and passed,

        [Fact]
        public void EngineShouldStringifyDynamicObjectArrayWithValuesCorrectly()
        {
            var engine = new Engine();
            var source = new dynamic[] { new { Text = "Text1", Value = 1 }, new { Text = "Text2", Value = 2 } };

            engine.SetValue("testSubject", source.AsEnumerable());
            var fromEngine = engine.Evaluate("return JSON.stringify(testSubject);");
            var result = fromEngine.ToString();
            Assert.Equal(result, "[{\"Text\":\"Text1\",\"Value\":1},{\"Text\":\"Text2\",\"Value\":2}]");

        }

but not work in OrchardCore ,Even though I commented out these lines of code
https://github.com/OrchardCMS/OrchardCore/blob/afcc979897f0d1a0e26581066bce53f2c319e166/src/OrchardCore.Modules/OrchardCore.Queries/Sql/SqlQuerySource.cs#L91

IEnumerable<dynamic> queryResults;

                using (connection)
                {
                    await connection.OpenAsync();

                    using (var transaction = connection.BeginTransaction(_session.Store.Configuration.IsolationLevel))
                    {
                        queryResults = await connection.QueryAsync(rawQuery, parameters, transaction);
                    }
                }

        /*        var results = new List<JObject>();
                foreach (var document in queryResults)
                {
                    results.Add(JObject.FromObject(document));
                }*/

                sqlQueryResults.Items = queryResults;
                return sqlQueryResults;

@hyzx86
Copy link
Contributor Author

hyzx86 commented Nov 8, 2021

Part of the reason I've found is that if the input object is of type List, it will fail, but if the ToArray() method is called before Jint is passed in, the program will work fine

image

Here is my test code:

  //https://github.com/OrchardCMS/OrchardCore/issues/10648
        [Fact]
        public void EngineShouldStringifyAnJObjectListWithValuesCorrectly()
        {
            
            var engine = new Engine();
            List<dynamic> queryResults = new List<dynamic>();
            queryResults.Add(new { Text = "Text1", Value = 1 });
            queryResults.Add(new { Text = "Text2", Value = 2 });

            engine.SetValue("testSubject", queryResults.Select(x => JObject.FromObject(x)));
            var fromEngine = engine.Evaluate("return JSON.stringify(testSubject);");
            var result = fromEngine.ToString();
            Assert.Equal(result, "[{\"Text\":\"Text1\",\"Value\":1},{\"Text\":\"Text2\",\"Value\":2}]");
       
        }

        [Fact]
        public void EngineShouldStringifyAnJObjectArrayWithValuesCorrectly()
        {
            //https://github.com/OrchardCMS/OrchardCore/issues/10648
            var engine = new Engine();
            List<dynamic> queryResults = new List<dynamic>();
            queryResults.Add(new { Text = "Text1", Value = 1 });
            queryResults.Add(new { Text = "Text2", Value = 2 }); 

            engine.SetValue("testSubject", queryResults.ToArray());
            var fromEngine2 = engine.Evaluate("return JSON.stringify(testSubject);");
            var result2 = fromEngine2.ToString();
            Assert.Equal(result2, "[{\"Text\":\"Text1\",\"Value\":1},{\"Text\":\"Text2\",\"Value\":2}]");
        }

        [Fact]
        public void EngineShouldStringifyDynamicObjectListWithValuesCorrectly()
        {
            var engine = new Engine();
            var source = new dynamic[] { new { Text = "Text1", Value = 1 }, new { Text = "Text2", Value = 2 } };

            engine.SetValue("testSubject", source.ToList());
            var fromEngine = engine.Evaluate("return JSON.stringify(testSubject);");
            var result = fromEngine.ToString();
            Assert.Equal(result, "[{\"Text\":\"Text1\",\"Value\":1},{\"Text\":\"Text2\",\"Value\":2}]");

        }

        [Fact]
        public void EngineShouldStringifyDynamicObjectArrayWithValuesCorrectly()
        {
            var engine = new Engine();
            var source = new dynamic[] { new { Text = "Text1", Value = 1 }, new { Text = "Text2", Value = 2 } };

            engine.SetValue("testSubject", source.AsEnumerable());
            var fromEngine = engine.Evaluate("return JSON.stringify(testSubject);");
            var result = fromEngine.ToString();
            Assert.Equal(result, "[{\"Text\":\"Text1\",\"Value\":1},{\"Text\":\"Text2\",\"Value\":2}]");

        }

@lahma
Copy link
Collaborator

lahma commented Nov 8, 2021

I can certainly fix the List stringify problem, but enumerators are a different thing (.Select()), Jint probably shouldn't materialize enumerators/non-concrete collections as it could be surprising behavior is enumerator causes some DB connections or whatnot.

@lahma
Copy link
Collaborator

lahma commented Nov 8, 2021

@hyzx86 we can help with the concrete list but the enumerator is a bit hard like stated. I see you've created a PR to address this on Orchard's side so maybe not something we can improve further.

@hyzx86
Copy link
Contributor Author

hyzx86 commented Nov 10, 2021

Ok, GOT it. Thank you

@hyzx86
Copy link
Contributor Author

hyzx86 commented Nov 10, 2021

Hi @lahma , have you update the nuget version?

@lahma
Copy link
Collaborator

lahma commented Nov 11, 2021

@hyzx86 You can use the MyGet feed for each main release, described in the main README.

@hyzx86
Copy link
Contributor Author

hyzx86 commented Nov 15, 2021

Hi @lahma ,That test will be faild

image

  [Fact]
        public void EngineShouldStringifyJObjectFromObjectListWithValuesCorrectly()
        {
            var engine = new Engine();
            var source = new dynamic[] { new { Text = "Text1", Value = 1 }, new { Text = "Text2", Value = 2 } };

            engine.SetValue("testSubject", source.Select(x => JObject.FromObject(x)).ToList());
            var fromEngine = engine.Evaluate("return JSON.stringify(testSubject);");
            var result = fromEngine.ToString();
            Assert.Equal(result, "[{\"Text\":\"Text1\",\"Value\":1},{\"Text\":\"Text2\",\"Value\":2}]");

        }

@hyzx86
Copy link
Contributor Author

hyzx86 commented Nov 15, 2021

Even if I use the ToArray():

image

@lahma
Copy link
Collaborator

lahma commented Nov 16, 2021

@hyzx86 this is probably as good as I can make it #1009 , note that still requires you to have custom object converter to handle mapping of primitive types

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

Successfully merging a pull request may close this issue.

2 participants