Fix for invalid state in JsonBlockValueConverter when an unused layout has a nested array (#19363)
* Fix for invalid state in JsonBlockValueConverter when an unused layout has a nested array * Improved comments as suggested by copilot review, also fixed code style miss * Added check for malformed JSON with more closing array tokens then opening tokens
This commit is contained in:
committed by
GitHub
parent
ea8ec4e375
commit
ec6a38e052
@@ -189,9 +189,36 @@ public class JsonBlockValueConverter : JsonConverter<BlockValue>
|
||||
else
|
||||
{
|
||||
// ignore this layout - forward the reader to the end of the array and look for the next one
|
||||
while (reader.TokenType is not JsonTokenType.EndArray)
|
||||
|
||||
// Read past the current StartArray token before we start counting
|
||||
_ = reader.Read();
|
||||
|
||||
// Keep track of the number of open arrays to ensure we find the correct EndArray token
|
||||
var openCount = 0;
|
||||
while (true)
|
||||
{
|
||||
reader.Read();
|
||||
if (reader.TokenType is JsonTokenType.EndArray && openCount == 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
if (reader.TokenType is JsonTokenType.StartArray)
|
||||
{
|
||||
openCount++;
|
||||
}
|
||||
else if (reader.TokenType is JsonTokenType.EndArray)
|
||||
{
|
||||
openCount--;
|
||||
if (openCount < 0)
|
||||
{
|
||||
throw new JsonException($"Malformed JSON: Encountered more closing array tokens than opening ones while processing block editor alias: {blockEditorAlias}.");
|
||||
}
|
||||
}
|
||||
|
||||
if (!reader.Read())
|
||||
{
|
||||
throw new JsonException($"Unexpected end of JSON while looking for the end of the layout items array for block editor alias: {blockEditorAlias}.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user