.NET: Fix JSON arrays of objects parsed as empty records when no schema is defined#4199
.NET: Fix JSON arrays of objects parsed as empty records when no schema is defined#4199LEDazzio01 wants to merge 1 commit intomicrosoft:mainfrom
Conversation
…ords When parsing JSON arrays containing objects without a predefined schema, `DetermineElementType()` was creating a `VariableType` with an empty (non-null) schema via `targetType.Schema?.Select(...) ?? []`. This caused `ParseRecord` to take the schema-based parsing path, iterating over zero schema fields and silently discarding all JSON properties. The fix checks `targetType.HasSchema` and falls back to `VariableType.RecordType` (which has `Schema = null`) when no schema is defined, ensuring `ParseRecord` takes the dynamic `ParseValues()` path that preserves all JSON properties. Closes microsoft#4195
|
I have sole ownership of intellectual property rights to my Submissions and I am not making Submissions in the course of work for my employer. |
There was a problem hiding this comment.
Pull request overview
Fixes a declarative workflow JSON parsing bug where arrays of objects could be inferred as “empty-schema records,” causing nested object properties to be dropped when no schema is defined.
Changes:
- Updates
ParseTableelement-type inference to useVariableType.RecordTypewhen the target list type has no schema, avoiding creation of an empty (non-null) schema. - Uses
targetType.HasSchemato decide when to propagate a schema into inferred record element types.
| JsonValueKind.Object => targetType.HasSchema | ||
| ? VariableType.Record(targetType.Schema!.Select(kvp => (kvp.Key, kvp.Value))) | ||
| : VariableType.RecordType, |
There was a problem hiding this comment.
Add a regression unit test for the schema-less path this fixes: parsing a JSON object (using VariableType.RecordType) that contains an array of objects should preserve nested object properties (e.g., { "items": [ {"name":"Alice"} ] } should yield dictionaries with a name key). Current unit tests cover record parsing without schema, and arrays of records with an explicit schema, but not arrays of objects when the array element type must be inferred with no schema (the scenario that previously produced {} elements).
Motivation and Context
Fixes #4195 — JSON arrays of objects are parsed as empty records when no schema is defined in declarative workflows.
When an
InvokeAzureAgentaction returns a JSON object containing an array of objects, and the output is captured viaresponseObject, all nested object properties are silently dropped. Each object in the array becomes{}. SubsequentForeachiteration over the array crashes with an unhandled workflow failure.Description
Root cause: In
JsonDocumentExtensions.cs, theParseTablemethod'sDetermineElementType()function creates a schema-boundVariableTypewith an empty (non-null) schema whentargetType.Schemais null:When
targetType.Schemaisnull, the?? []fallback passes an empty collection toVariableType.Record(), creating aVariableTypewithSchemaset to an emptyFrozenDictionary(non-null, count = 0). Downstream,ParseRecordcheckstargetType.Schema is nulland takes theParseSchemapath (which iterates over zero fields), silently discarding all JSON properties.Fix: Check
targetType.HasSchema(which correctly handles empty schemas) and fall back toVariableType.RecordTypewhen no schema is defined:This ensures
ParseRecordtakes the dynamicParseValues()path that preserves all JSON properties.Contribution Checklist