semantic-kernel icon indicating copy to clipboard operation
semantic-kernel copied to clipboard

.Net: Fix RedisJsonCollection upsert persisting unannotated POCO properties

Open jluocsa opened this issue 6 months ago • 2 comments

Motivation and Context

Fixes #14021.

RedisJsonCollection<TKey, TRecord>.UpsertAsync was persisting POCO properties that are not part of the vector-store schema (no [VectorStoreKey] / [VectorStoreData] / [VectorStoreVector] attribute and not declared in the collection definition).

Root cause: RedisJsonMapper<TConsumerDataModel>.MapFromDataToStorageModel serialized the entire user record with JsonSerializer.SerializeToNode(dataModel, jsonSerializerOptions) and only removed the key field afterwards. Anything else on the type — including unannotated public properties — flowed straight into the Redis JSON payload.

RedisJsonDynamicMapper (the Dictionary<string, object?> path) already does the right thing: it iterates model.DataProperties / model.VectorProperties and only emits those.

Description

Refactor RedisJsonMapper<TConsumerDataModel>.MapFromDataToStorageModel to build the storage JsonObject explicitly from the CollectionModel, mirroring the dynamic mapper:

  1. Pull the key via model.KeyProperty.GetValueAsObject(dataModel) and validate it is a non-null string (unchanged semantics — Redis JSON keys are still string-typed and the constructor still validates that up front).
  2. Emit one JSON property per model.DataProperties entry, keyed by dataProperty.StorageName (which CollectionJsonModelBuilder.Customize() already populates from [JsonPropertyName] / the configured PropertyNamingPolicy / the .NET property name — exactly what the previous whole-object serialization produced for annotated properties). Each value is serialized through JsonSerializer.SerializeToNode(value, dataProperty.Type, jsonSerializerOptions) so all of the user's JsonSerializerOptions continue to apply.
  3. Emit one JSON property per model.VectorProperties entry, keyed by property.StorageName, with the existing precedence: generated embedding for this slot, otherwise the value pulled from the POCO. Vector values continue to be written as a flat JsonArray of float/double. The same ReadOnlyMemory<T> | Embedding<T> | T[] switch as in RedisJsonDynamicMapper is used, so both float and double vector types and their nullable variants are handled.

Properties without a vector-store attribute and not declared in the collection definition are now simply not serialized — same behavior as the dynamic mapper.

Tests:

  • RedisJsonCollectionTests.CanUpsertRecordAsync / CanUpsertManyRecordsAsync: dropped the "NotAnnotated":null / "notAnnotated":null fragments from the expected JSON payloads and removed the matching // TODO: Fix issue where NotAnnotated is being included in the JSON. comments.
  • RedisJsonMapperTests.MapsAllFieldsFromDataToStorageModel{,WithCustomSerializerOptions}: added a positive Assert.False(jsonObject.ContainsKey("NotAnnotated")) (and the notAnnotated camelCase variant) to lock in the fix.

Other tests in Redis.UnitTests were not touched.

Verification

$env:GITHUB_ACTIONS = 'true'   # skip the local pre-build `dotnet format` target
& $dotnet test .\dotnet\test\VectorData\Redis.UnitTests\Redis.UnitTests.csproj -c Release --verbosity normal

Result: Total tests: 109. Passed: 109. Failed: 0. Skipped: 0.

The targeted reproducer from the issue body:

& $dotnet test .\dotnet\test\VectorData\Redis.UnitTests\Redis.UnitTests.csproj `
    --filter "FullyQualifiedName~RedisJsonCollectionTests.CanUpsertRecordAsync" --verbosity normal

passes on all four [InlineData] rows.

Contribution Checklist

  • [x] The code builds clean without any errors or warnings
  • [x] I ran all the unit tests in the affected project (Redis.UnitTests)
  • [x] The PR follows the SK Contribution Guidelines and the pre-submission formatting script raises no violations
  • [x] All unit tests pass, and I have added new tests where possible
  • [x] I didn't break anyone :smile:

Closes #14021

jluocsa avatar May 23 '26 18:05 jluocsa

The three failing CI legs (dotnet-build-and-test (10.0, ubuntu-latest, Release, true, integration), (10.0, windows-latest, Debug), (9.0, windows-latest, Release)) are not caused by this PR — they're hitting a fresh NU1903 advisory on Scriban 7.1.0 (GHSA-24c8-4792-22hx, High / CVSS 8.7) that breaks every dotnet build --warnaserror run in the repo right now.

error NU1903: Package 'Scriban' 7.1.0 has a known high severity vulnerability,
https://github.com/advisories/GHSA-24c8-4792-22hx

I opened #14031 as a single-line bump to Scriban 7.2.0 (the patched version) — once that lands, this PR's CI should turn green on a re-run.

jluocsa avatar May 23 '26 19:05 jluocsa

@jluocsa, thanks for the contribution. This looks like a good improvement. Note that we are in the process of moving the code to a new repo, which involves renaming the packages as well. The new repo should be live very soon (fingers crossed). It would make sense to apply the fix in the new repo, rather than here. I'll ping you as soon as that's ready and we can hopefully get it in there instead.

CC @roji

westey-m avatar May 26 '26 09:05 westey-m