Repository navigation
Represent CookieSameSite as a readonly struct - #69992
Conversation
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.
|
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.
There was a problem hiding this comment.
🟡 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 toStrict. - 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.
|
✅ 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
|

Represent CookieSameSite as a readonly struct
Description
This PR changes
CookieSameSitefrom anenumto areadonly structthat 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 stringValueand a static abstractCreate(string). One genericWebIdlEnumJsonConverter<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 createTwithout reflection. The type implementsCreateexplicitly, so the interface does not appear in the public API.A later family adds a type like this, with no new converter.
The type is a plain
readonly struct, not arecord 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.txtlists all of them, together with the two constructors (the implicit parameterless one and the one that takes the string), the static properties,Value, andToString.CookieStoreandCookieInitdo not change.CookieInit.SameSitekeeps its type name and its default,CookieSameSite.Strict.default(T)A struct can be created without its constructor, for example with
defaultornew 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.CookieSameSitehas a Web IDL default,"strict"(fromCookieInit.sameSite). The struct stores this string asnull, andValuereturns"strict"fornull. Sodefault(CookieSameSite)is sent as"strict", and the equality members, which compare the stored field, treat it as equal toCookieSameSite.Strict.A later type with no Web IDL default, such as
OrientationType, does not store anything asnull. ItsValuethrowsInvalidOperationExceptionfor the default value, so sending it fails before anything reaches the browser. ItsToString()returns an empty string instead, so it never throws. The converter itself has no check for the default value.Tests
JSRuntimeserializer sends for the three known values, for a value created from a string, and fordefault(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.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 throughIJSRuntime.InvokeAsync<CookieSameSite>("String", ...)instead. The demo page has three buttons for these steps.dotnet publish. Before mergingmain, I also checked the trimmed WebAssembly client. The converter and the used members ofCookieSameSitewere kept.Contributes to #69991
Contributes to #69844