Skip to content

Fix for arrays with empty elements - #3811

Merged
amaitland merged 2 commits into
cefsharp:masterfrom
Romout:master
Sep 16, 2021
Merged

Fix for arrays with empty elements#3811
amaitland merged 2 commits into
cefsharp:masterfrom
Romout:master

Conversation

@Romout

@Romout Romout commented Sep 14, 2021

Copy link
Copy Markdown
Contributor

Fixes:
Resolves #3808

Summary: [summary of the change and which issue is fixed here]

  • Array serialization now correctly handles empty elements within the source array

Changes: [specify the structures changed]
-No structural changes - use HasValue to check if an element is empty

How Has This Been Tested?
Used WinForms example with several inputs to try and break it but - it works.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Updated documentation

Checklist:

  • Tested the code(if applicable)
  • Commented my code
  • Changed the documentation(if applicable)
  • New files have a license disclaimer
  • The formatting is consistent with the project (project supports .editorconfig)

@AppVeyorBot

Copy link
Copy Markdown

@amaitland

Copy link
Copy Markdown
Member

Used WinForms example with several inputs to try and break it but - it works

Unit test will need to be added, https://github.com/cefsharp/CefSharp/blob/master/CefSharp.Test/OffScreen/OffScreenBrowserBasicFacts.cs#L139 Can be used as a rough guide.

@amaitland

Copy link
Copy Markdown
Member

Looking at the actual code now and the history would suggest the section is basically unchanged since 2015. So the questions I have are was there a reason we used keys to begin with? If yes is that reason still valid? A straight index would have made more sense (I didn't write the code, though I likely reviewed it all those years ago).

I'd be inclined to simplify the code as below:

else if (obj->IsArray())
{
	int arrLength = obj->GetArrayLength();
	auto array = CefListValue::Create();

	for (int i = 0; i < arrLength; i++)
	{
		SerializeV8Object(obj->GetValue(i), array, i, callbackRegistry, seen);
	}

	list->SetList(index, array);
}

I did test your code and it differs slightly in it's output when I compare to JSON.stringify() for one edge case (at least that's all I found in the 2 mins of testing).

[1, 2, ,] is returned as "[1,2,null]" for JSON.stringify where it's "[1, 2]" for the updated code.

As above test cases will be required before any chance can be merged.

@Romout

Romout commented Sep 15, 2021

Copy link
Copy Markdown
Contributor Author

Hm, I like your version much more. You are right that keys are not even required if GetValue returns null if there isn't a value.

@AppVeyorBot

Copy link
Copy Markdown

@amaitland
amaitland merged commit 1847c89 into cefsharp:master Sep 16, 2021
@amaitland

Copy link
Copy Markdown
Member

Test case rewritten to use Theory with Inline Data in fa2b6e2

@amaitland amaitland added this to the 94.0.x milestone Sep 16, 2021
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 this pull request may close these issues.

EvaluateScriptAsync crash when serializing an partially empty array

3 participants