Fix JSON serialization of nested Entity/EntityCollection in expando API responses#752
Merged
Merged
Conversation
…PI responses When a custom API returns an expando EntityCollection whose entities contain nested Entity or EntityCollection attribute values, the serialization previously fell into the default else branch and called JsonConvert.SerializeObject directly on the CRM SDK types. This produced verbose .NET-style JSON (Attributes[], Entities[], etc.) instead of the flat WebAPI-style JSON expected for use with JSON_VALUE/JSON_QUERY. Refactored SerializeAttributeValues into a BuildAttributeDictionary helper method that recursively handles: - Entity attribute values → nested JSON object via recursion - EntityCollection attribute values → JSON array of nested objects Added ExpandoMessage test fixture and a new test verifying that nested Entity attributes in expando responses are serialized as flat WebAPI-style JSON objects rather than verbose .NET format. Fixes #743 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Problem
Fixes #743
When a custom API returns an expando
EntityCollectionwhose entities contain nestedEntityorEntityCollectionattribute values, the serialization produced verbose .NET-style JSON instead of the flat WebAPI-style JSON:Before (broken):
{ "user": { "Attributes": [{"Key": "systemuser", "Value": "a81ca1d0-..."}], "LogicalName": null, "Id": "00000000-..." } }After (fixed):
{ "user": { "@odata.type": "systemuser", "@odata.id": "a81ca1d0-...", "__DisplayName__": "John Doe" } }Root Cause
In
ExecuteMessageNode.SerializeAttributeValues(), theelsebranch passedEntityandEntityCollectionattribute values directly toJsonConvert.SerializeObject()which used default .NET reflection serialization, producing theAttributes[]/Entities[]verbose format.Fix
Refactored
SerializeAttributeValuesinto aBuildAttributeDictionary()helper that returns aDictionary<string, object>, adding recursive handling for:Entityattribute values → nested JSON object via recursion intoBuildAttributeDictionaryEntityCollectionattribute values → JSON array of recursively serialized entity dictionariesAll existing type handling (
OptionSetValue,Money,EntityReference,@odata.typeannotations) is preserved unchanged.Changes
MarkMpn.Sql4Cds.Engine/ExecutionPlan/ExecuteMessageNode.cs— Core fix: refactoredSerializeAttributeValueswith a recursiveBuildAttributeDictionaryhelperMarkMpn.Sql4Cds.Engine.Tests/ExpandoMessageExecutor.cs— New fake executor returning entities with nestedEntityattributesMarkMpn.Sql4Cds.Engine.Tests/StubMessageCache.cs— AddedExpandoMessageentry for testingMarkMpn.Sql4Cds.Engine.Tests/FakeXrmEasyTestsBase.cs— RegisteredExpandoMessageExecutorMarkMpn.Sql4Cds.Engine.Tests/JsonFunctionTests.cs— New test verifying flat WebAPI-style JSON outputMarkMpn.Sql4Cds.Engine.Tests/MarkMpn.Sql4Cds.Engine.Tests.csproj— Added new executor file to project