Skip to content

Fixes #237 - support multi-dimensional arrays#238

Closed
mlankamp wants to merge 1 commit into
AzureCosmosDB:mainfrom
mlankamp:issue-237
Closed

Fixes #237 - support multi-dimensional arrays#238
mlankamp wants to merge 1 commit into
AzureCosmosDB:mainfrom
mlankamp:issue-237

Conversation

@mlankamp
Copy link
Copy Markdown

@mlankamp mlankamp commented Mar 15, 2026

Extended the WriteFieldValue function to support multi-dimensional arrays

Fixes #237

@mlankamp
Copy link
Copy Markdown
Author

@philnach & @markjbrown How do I request a review?

@philnach
Copy link
Copy Markdown
Collaborator

@mlankamp, thanks for submitting the PR and reaching out. you've done the right thing by opening a PR. You could in the Reviewers panel on the PR request specific people Requesting reviews from collaborators and organization members to review your code.

With that said, I'll take a look.

@philnach philnach requested review from Copilot and philnach March 30, 2026 18:29
Copy link
Copy Markdown
Collaborator

@philnach philnach left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add tests for this scenario to ensure the behavior works? Tests would go into this file: https://github.com/AzureCosmosDB/data-migration-desktop-tool/blob/main/Interfaces/Cosmos.DataTransfer.Common.UnitTests/DataItemJsonConverterTests.cs

@mlankamp
Copy link
Copy Markdown
Author

Of course! Will add them tomorrow

@philnach philnach review requested due to automatic review settings March 30, 2026 18:46
}
else if (arrayItem is not string && arrayItem is IEnumerable childs)
{
WriteFieldValue(writer, "", childs, includeNullFields);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR — adding support for nested (multi-dimensional) arrays makes sense.

That said, I’m a bit concerned about the current approach of handling nested arrays by calling:

WriteFieldValue(writer, "", childs, includeNullFields);

relies on:

if (!string.IsNullOrWhiteSpace(propertyName.Value)) { writer.WriteStartArray(propertyName); }
else { writer.WriteStartArray(); }

Could we refactor nested-array writing so it doesn’t depend on passing fieldName == "" and the propertyName.Value check?

For example:

introduce a dedicated helper that writes an array value (no property name), e.g. WriteArrayValue(Utf8JsonWriter writer, IEnumerable items, ...)
use that helper for nested arrays so it always calls writer.WriteStartArray() / writer.WriteEndArray()

(Optional)
The nested-array detection (arrayItem is not string && arrayItem is IEnumerable) is very broad and may accidentally treat things like byte[] as arrays of numbers. It might be worth tightening this to the specific collection types we expect to see here.

@thucnguyen77
Copy link
Copy Markdown
Contributor

Hi,

By any chance this PR can be merged soon?
I am working with GeoJSON and facing the same issue when trying to migrate data between Cosmos DB accounts.

Thanks,
Thuc

@philnach
Copy link
Copy Markdown
Collaborator

Hi @mlankamp — thanks again for the original fix and your patience. I've opened #243 which builds on your work and addresses the two review concerns:

  1. Avoids the sentinel fieldName == """" recursion by splitting array writes into dedicated WriteArrayItems / WriteArrayItemValue helpers, so legitimate empty-string and whitespace property names are preserved.
  2. Adds a MaxJsonDepth guard so deeply nested or recursive input throws a clean InvalidOperationException instead of stack-overflowing the host process — the failure mode I was worried about based on past Newtonsoft.Json bugs.

I also added a comprehensive test suite (17 new tests) covering the GeoJSON scenarios from #237, edge cases with nulls / mixed types / dictionaries inside nested arrays, regression guards for property-name handling, and at/over-max-depth tests for both arrays and objects.

Credit goes to you — I added a Co-authored-by: trailer on the commit. Closing this PR in favor of #243. Sorry for the long round-trip!

cc @thucnguyen77 — this should also fix the GeoJSON scenario you mentioned.

@mlankamp mlankamp closed this May 15, 2026
markjbrown added a commit that referenced this pull request May 16, 2026
…port

Fix #237 - Support multi-dimensional arrays in DataItemJsonConverter (supersedes #238)
pull Bot pushed a commit to ehtick/data-migration-desktop-tool that referenced this pull request May 16, 2026
…sonConverter

Refactor WriteFieldValue to handle nested (multi-dimensional) arrays via a
dedicated WriteArrayItems / WriteArrayItemValue pair instead of recursing back
into WriteFieldValue with a sentinel empty-string field name. This:

- Restores GeoJSON support: Polygon/MultiPolygon `coordinates` arrays (depth
  3+) now serialize correctly instead of falling back to
  `System.Collections.Generic.List`1[System.Object]`.
- Removes the implicit `IsNullOrWhiteSpace(propertyName)` sentinel so legitimate
  empty-string and whitespace property names are preserved.
- Adds a MaxJsonDepth (64) guard that throws InvalidOperationException for
  pathological / recursively nested input instead of stack-overflowing the host
  process.

Tests:
- 17 new tests in DataItemJsonConverterTests covering GeoJSON Point/LineString/
  Polygon, the full issue-237 document, mixed scalars/nulls/IDataItems inside
  nested arrays, dictionary-of-dictionary nested arrays, wide-array smoke,
  empty/whitespace property-name regression guards, deserialize/re-serialize
  round-trip, and depth-guard at and over the limit (arrays, objects, mixed).
- All 125 Common.UnitTests pass; full solution test suite is green.

Supersedes AzureCosmosDB#238. Original fix authored by Mart Lankamp - thanks!

Closes AzureCosmosDB#237

Co-authored-by: Mart Lankamp <mart.lankamp@users.noreply.github.com>
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.

Support multi-dimensional arrays when writing json (example geojson polygon)

3 participants