Skip to content

Simplify and unify TempData and SupplyParameterFromSession serialization - #65483

Merged
dariatiurina merged 30 commits into
dotnet:mainfrom
dariatiurina:65231-tempdata-serialization
Aug 12, 2026
Merged

dariatiurina merged 30 commits into
dotnet:mainfrom
dariatiurina:65231-tempdata-serialization

Conversation

@dariatiurina

@dariatiurina dariatiurina commented Feb 20, 2026 •

Copy link
Copy Markdown
Contributor

Simplify and unify TempData and SupplyParameterFromSession serialization

Summary

Reworks how Blazor endpoints serialize stored values so TempData and [SupplyParameterFromSession] share a single serializer. Stored values include a constrained type token, replacing the heuristic type inference previously used by TempData and enabling session-backed parameters to restore supported enums and collection types using their declared property type.

Changes

  • Introduces IStoredDataSerializer and JsonStoredDataSerializer, replacing the TempData-specific ITempDataSerializer and JsonTempDataSerializer.
  • Uses the shared serializer for cookie-backed TempData, session-backed TempData, and [SupplyParameterFromSession].
  • Stores each non-null value as { "type": "<token>", "value": <json> }. Type tokens omit assembly metadata, and object[] values are handled recursively so each element retains its own token.
  • Restricts serialization to int, bool, string, Guid, DateTime, int-backed enums, and nullable forms of those types. Supported containers are single-dimensional arrays, List<T>, HashSet<T>, Collection<T>, Dictionary<string, T>, and recursively stored object[].
  • Normalizes enum tokens to System.Int32 for TempData payload compatibility across enum types. When [SupplyParameterFromSession] supplies a declared target type, values are deserialized directly to that type so enums and supported collections retain the component property type.
  • Validates concrete [SupplyParameterFromSession] property types when subscriptions are created. Unsupported types fail with a clear InvalidOperationException; object and abstract property types remain deferred to runtime serialization.
  • Uses byte-based ISession.Set and TryGetValue instead of the previous string-based JSON path. Session keys are normalized to lowercase, null values remove the corresponding entry, and getter failures are logged without preventing other values from being persisted.
  • Moves unsupported-type enforcement into the serializer instead of duplicating validation in each TempData provider.
  • Updates the non-generic TempData enumerator path to use the same Current implementation as generic enumeration, ensuring enumerated entries are marked as consumed consistently.

Testing

  • Replaces JsonTempDataSerializerTest with JsonStoredDataSerializerTest, covering supported and unsupported types, clean type tokens, scalar and collection round trips, nullable values, int-backed enums, recursive object[] values, typed session-value serialization, and byte-identical TempData reserialization.
  • Updates session supplier and subscription tests to use typed byte payloads and adds coverage for unsupported property validation, lowercase keys, enum and collection restoration, missing session contexts, and persistence behavior.
  • Updates cookie and session TempData provider tests and dependency-injection tests to use the shared serializer.
  • Removes the complex custom-type [SupplyParameterFromSession] E2E scenario because arbitrary complex types are no longer supported. Existing E2E coverage continues to verify scalar values, missing values, redirects, and navigation persistence.

Breaking Changes

  • Supported-type restriction: [SupplyParameterFromSession] no longer persists arbitrary complex/custom types. Unsupported concrete property types now fail when the subscription is created, and unsupported runtime values fail during serialization.

Fixes #65231

@github-actions github-actions Bot added the area-blazor Includes: Blazor, Razor Components label Feb 20, 2026
@dariatiurina dariatiurina self-assigned this Feb 20, 2026
@dariatiurina
dariatiurina marked this pull request as ready for review March 26, 2026 11:03
@dariatiurina
dariatiurina requested a review from a team as a code owner March 26, 2026 11:03
Copilot AI review requested due to automatic review settings March 26, 2026 11:03

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Updates the Components Endpoints TempData pipeline to preserve explicit type information per entry, removing heuristic deserialization in JsonTempDataSerializer and simplifying serialization/deserialization.

Changes:

  • Switches TempData storage/provider/serializer plumbing from IDictionary<string, object?> to IDictionary<string, (object? Value, Type? Type)>.
  • Changes the JSON payload format to store { type, value } per entry and removes the prior heuristic-based deserialization paths.
  • Updates TempData-related tests to account for tuple-based entries and expanded supported types.

Reviewed changes

Copilot reviewed 11 out of 11 changed files in this pull request and generated 8 comments.

Show a summary per file
File Description
src/Components/Endpoints/src/TempData/TempData.cs Stores (Value, Type) per key and adapts IDictionary<string, object?> behavior.
src/Components/Endpoints/src/TempData/JsonTempDataSerializer.cs New { type, value } serialization format; simplified deserialization via stored type.
src/Components/Endpoints/src/TempData/ITempDataSerializer.cs Updates serializer contract to use tuple-based dictionary.
src/Components/Endpoints/src/DependencyInjection/ITempDataProvider.cs Updates provider contract to use tuple-based dictionary.
src/Components/Endpoints/src/DependencyInjection/TempDataService.cs Propagates updated tuple-based dictionary type through the service.
src/Components/Endpoints/src/TempData/CookieTempDataProvider.cs Updates cookie temp data provider to read/write tuple-based values.
src/Components/Endpoints/src/TempData/SessionStorageTempDataProvider.cs Updates session temp data provider to read/write tuple-based values.
src/Components/Endpoints/test/TempData/TempDataTest.cs Updates TempData tests for tuple-based storage (but has remaining outdated cases).
src/Components/Endpoints/test/TempData/CookieTempDataProviderTest.cs Updates cookie provider tests for tuple-based load/save.
src/Components/Endpoints/test/TempData/SessionStorageTempDataProviderTest.cs Updates session provider tests for tuple-based load/save.
src/Components/Endpoints/test/TempData/JsonTempDataSerializerTest.cs Replaces per-type tests with parameterized round-trip tests and expands valid-type coverage.

@ilonatommy ilonatommy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

First pass.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Looks like this PR hasn't been active for some time and the codebase could have been changed in the meantime.
To make sure no conflicting changes have occurred, please rerun validation before merging. You can do this by leaving an /azp run comment here (requires commit rights), or by simply closing and reopening.

@dotnet-policy-service dotnet-policy-service Bot added the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Apr 2, 2026
@dariatiurina
dariatiurina marked this pull request as draft April 30, 2026 08:14
@dariatiurina dariatiurina removed the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Jul 1, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 23 out of 23 changed files in this pull request and generated 3 comments.

@dariatiurina dariatiurina changed the title Simplify the JsonTempDataSerializer Simplify and unify TempData and SupplyParameterFromSession serialization Jul 7, 2026
@dariatiurina dariatiurina added this to the 11.0-preview7 milestone Jul 7, 2026
@dariatiurina
dariatiurina marked this pull request as ready for review July 7, 2026 11:24

@ilonatommy ilonatommy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think these are all the ideas from framework review. I didn't go through tests yet. I will do that once the current discussions are resolved 😉

@dariatiurina
dariatiurina requested a review from ilonatommy July 24, 2026 12:32

@ilonatommy ilonatommy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Fast pass.

@ilonatommy ilonatommy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Another pass.

@dariatiurina
dariatiurina requested a review from ilonatommy August 6, 2026 14:51
{
if (type.IsEnum)
{
return "System.Int32";

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If we serialize enum it becomes System.Int32 and then when we deserialize, will we deserialize it as enum? Or as int?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

As int. If we want to deserialize it as enum, we need to have AssemblyQualifiedName

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

And it's better to change the type than to have AssemblyQualifiedName?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Or maybe you mean that that's the gain we should have flagged on #65483 (comment)?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We lose native enum, but it's not a hard transformation that users can't do themselves, so I am not against this trick.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I added that when we detect enum values we save AssemblyQualifiedName

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Cool! Should we add more test coverage for enums-related paths? We don't do SerializeValue/DeserializeValue with an enum-containing collections, e.g. List<TestEnum>, Dictionary<string, TestEnum>, and TestEnum?[].

@ilonatommy ilonatommy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If enum collections are working then it's good to merge.

@dariatiurina
dariatiurina enabled auto-merge (squash) August 11, 2026 15:39
@Youssef1313 Youssef1313 modified the milestones: 11.0-preview7, 11.0-rc1 Aug 12, 2026
@dariatiurina
dariatiurina merged commit 2daa22c into dotnet:main Aug 12, 2026
29 checks passed
@dariatiurina
dariatiurina deleted the 65231-tempdata-serialization branch September 29, 2026 09:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-blazor Includes: Blazor, Razor Components

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Blazor] Reconsider temp data serialization strategy.

5 participants