Skip to content

Represent CookieSameSite as a readonly struct - #69992

Merged
ilonatommy merged 5 commits into
dotnet:mainfrom
jsakamotoIGJP:components-platform-webidl-enum
Nov 9, 2026
Merged

ilonatommy merged 5 commits into
dotnet:mainfrom
jsakamotoIGJP:components-platform-webidl-enum

Conversation

@jsakamotoIGJP

@jsakamotoIGJP jsakamotoIGJP commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor
  • You've read the Contributor Guide and Code of Conduct.
  • You've included unit or integration tests for your change, where applicable.
  • You've included inline docs for your change, where applicable.
  • There's an open issue for the PR that you are making. If you'd like to propose a new feature or change, please open an issue to discuss the change or find an existing issue.

Represent CookieSameSite as a readonly struct

Description

This PR changes CookieSameSite from an enum to a readonly struct that wraps the string, as discussed in #69991. The issue explains why. This description covers the implementation. The Cookie Store API proposal #69844 is updated to match.

One converter for all Web IDL enum types

Each type implements the internal IWebIdlEnum<TSelf>. It has the string Value and a static abstract Create(string). One generic WebIdlEnumJsonConverter<T> reads and writes every such type through this interface. C# has no constraint for a constructor with arguments, so the static abstract method is the way to create T without reflection. The type implements Create explicitly, so the interface does not appear in the public API.

A later family adds a type like this, with no new converter.

[JsonConverter(typeof(WebIdlEnumJsonConverter<OrientationType>))]
public readonly struct OrientationType : IEquatable<OrientationType>, IWebIdlEnum<OrientationType>
{
    // Constructor, static properties for the known values, Value, equality members, Create, and ToString, as in CookieSameSite.
}

The type is a plain readonly struct, not a record struct, because the repo's public API conventions keep synthesized members out of the public contract. The equality members are written out instead. Equals(CookieSameSite), Equals(object), GetHashCode, ==, and != are one line each and compare the stored string ordinally, as Web IDL does. PublicAPI.Unshipped.txt lists all of them, together with the two constructors (the implicit parameterless one and the one that takes the string), the static properties, Value, and ToString.

CookieStore and CookieInit do not change. CookieInit.SameSite keeps its type name and its default, CookieSameSite.Strict.

default(T)

A struct can be created without its constructor, for example with default or new CookieSameSite(). Such a value has no string. As answered in #69991, it means the Web IDL default when the spec has one, and it is an error when the spec has none.

CookieSameSite has a Web IDL default, "strict" (from CookieInit.sameSite). The struct stores this string as null, and Value returns "strict" for null. So default(CookieSameSite) is sent as "strict", and the equality members, which compare the stored field, treat it as equal to CookieSameSite.Strict.

private const string DefaultValue = "strict";
private readonly string? _value;

public CookieSameSite(string value)
{
    ArgumentNullException.ThrowIfNull(value);
    _value = value == DefaultValue ? null : value;
}

public string Value => _value ?? DefaultValue;

A later type with no Web IDL default, such as OrientationType, does not store anything as null. Its Value throws InvalidOperationException for the default value, so sending it fails before anything reaches the browser. Its ToString() returns an empty string instead, so it never throws. The converter itself has no check for the default value.

Tests

  • The unit tests check the JSON that the real JSRuntime serializer sends for the three known values, for a value created from a string, and for default(CookieSameSite). They also check that any string from the browser can be read, that "strict" from the browser equals the default value, and the equality members.
  • A new E2E test runs in both render modes. It sets a cookie with new CookieSameSite("lax"), then sets one with an unknown value, which Chromium rejects. This shows that the string reaches the browser. Cookie Store returns no SameSite, so the test reads an unknown value through IJSRuntime.InvokeAsync<CookieSameSite>("String", ...) instead. The demo page has three buttons for these steps.
  • The latest commit passes all Platform unit tests and E2E tests locally, both from the build output and from the output of dotnet publish. Before merging main, I also checked the trimmed WebAssembly client. The converter and the used members of CookieSameSite were kept.

Contributes to #69991
Contributes to #69844

A prototype for representing Web IDL enums as structs that wrap the
string, instead of .NET enums. A browser can return a value that the
library does not know, and an app can pass a value that the library
does not define yet.

- Change CookieSameSite from an enum to a readonly record struct with
  Strict, Lax, and None as static properties and a constructor that
  takes the string.
- Add the internal IWebIdlEnum<TSelf> interface and one generic JSON
  converter for all such types. It needs no reflection.
- Add unit tests for the JSON sent and read, and for equality.
- Add demo buttons and an E2E test that pass a value created from a
  string, pass an unknown value, and read an unknown value.
@github-actions github-actions Bot added the area-blazor Includes: Blazor, Razor Components label Oct 23, 2026
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Oct 23, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Thanks for your PR, @jsakamotoIGJP. Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

The default value is stored as null, so default(CookieSameSite) equals
Strict with the generated equality. The converter no longer rejects
default(T); a type without a Web IDL default throws from Value instead.
Replace the test that expected JsonException for default(CookieSameSite)
with tests that it is sent as "strict", equals Strict, and that "strict"
read from the browser equals the default.
@jsakamotoIGJP
jsakamotoIGJP marked this pull request as ready for review November 9, 2026 04:58
@jsakamotoIGJP
jsakamotoIGJP requested a review from a team as a code owner November 9, 2026 04:58
Copilot AI balanced review requested due to automatic review settings November 9, 2026 04:58

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.

🟡 Changes recommended

The public CookieSameSite record struct conflicts with API conventions because synthesized equality and hashing couple the shipping contract to the storage layout rather than explicit equality members.

1 open issue
What changed in this PR

This PR changes CookieSameSite from an enum to a string-backed readonly record struct, allowing Components.Platform to pass and read values that the library does not yet define.

Changes:

  • Preserves known values and makes default(CookieSameSite) equal to Strict.
  • Adds a shared interface and JSON converter for string-backed Web IDL values.
  • Updates the API baseline and adds unit and browser coverage for serialization, equality, and unknown values.
File Description
src/​Components/​Platform/​testassets/​PlatformTestApp.E2E.Tests/​Tests/​CookieStoreTests.cs Tests string-backed values in both render modes.
src/​Components/​Platform/​testassets/​PlatformTestApp.Client/​Components/​CookieStoreDemo.razor Adds controls for string values and JavaScript readback.
src/​Components/​Platform/​test/​CookieStoreTest.cs Covers serialization, defaults, deserialization, and equality.
src/​Components/​Platform/​src/​WebIdlEnumJsonConverter.cs Adds reusable string serialization for Web IDL values.
src/​Components/​Platform/​src/​PublicAPI.Unshipped.txt Updates the CookieSameSite public API baseline.
src/​Components/​Platform/​src/​IWebIdlEnum.cs Defines the internal contract used by the converter.
src/​Components/​Platform/​src/​CookieSameSite.cs Replaces the enum with a string-backed readonly record struct.

Review effort: Balanced

The repo's public API conventions do not allow a public record struct,
because its synthesized members become part of the contract without
being listed. The equality members are now written out and listed in
PublicAPI.Unshipped.txt. Behavior does not change.
@jsakamotoIGJP jsakamotoIGJP changed the title Represent CookieSameSite as a readonly record struct Represent CookieSameSite as a readonly struct Nov 9, 2026
@ilonatommy ilonatommy added the feature-blazor-jsinterop This issue is related to JSInterop in Blazor label Nov 9, 2026
@ilonatommy
ilonatommy enabled auto-merge (squash) November 9, 2026 07:38
@ilonatommy
ilonatommy merged commit 09016c6 into dotnet:main Nov 9, 2026
39 checks passed
@github-actions

github-actions Bot commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

✅ No documentation PR was created.

Confidence that documentation is needed: 25%

No docs needed now. The enum-to-struct change applies to an unshipped API with no existing documentation. Human review can revisit when the Cookie Store feature is documented.

Documentation surfaces considered

  • Conceptual article: Not required — The change is a refinement to an API that is still unshipped and not yet documented; docs for the Cookie Store feature should be written when the feature is complete.
  • Migration guidance: Not required — The type was never released, so there is nothing for users to migrate from.
  • Breaking change: Not required — The API is listed in PublicAPI.Unshipped.txt, so it has no shipped compatibility impact.

This branch was previously deployed

1 inactive deployment
copilot-pat-pool — 2ea0d1c4 Deployed Nov 9, 2026 by ilonatommy via conclusion #247
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 community-contribution Indicates that the PR has been added by a community member feature-blazor-jsinterop This issue is related to JSInterop in Blazor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants