Skip to content

Conversation

@mxschmitt
Copy link
Contributor

This patch updates the .NET channel generator to support nullable reference types. Although objects are constructed via System.Text.Json deserialization, the compiler still requires non-nullable reference properties to be initialized. To avoid warnings when nullable is enabled, this change adds = null!; for reference types (excluding bool, int, and JsonElement).

microsoft/playwright-dotnet#3163

@github-actions
Copy link
Contributor

Test results for "tests 1"

11 flaky ⚠️ [chromium-library] › library/browsercontext-reuse.spec.ts:256:1 › should work with clock emulation @chromium-ubuntu-22.04-node18
⚠️ [chromium-library] › library/chromium/oopif.spec.ts:284:3 › should click @chromium-ubuntu-22.04-node18
⚠️ [chromium-library] › library/browsercontext-reuse.spec.ts:256:1 › should work with clock emulation @chromium-ubuntu-22.04-node20
⚠️ [chromium-library] › library/browsercontext-reuse.spec.ts:256:1 › should work with clock emulation @chromium-ubuntu-22.04-node22
⚠️ [firefox-library] › library/inspector/cli-codegen-1.spec.ts:986:7 › cli codegen › should not throw csp directive violation errors @firefox-ubuntu-22.04-node18
⚠️ [firefox-page] › page/page-wait-for-function.spec.ts:104:3 › should work with strict CSP policy @firefox-ubuntu-22.04-node18
⚠️ [chromium-library] › library/browsercontext-reuse.spec.ts:256:1 › should work with clock emulation @ubuntu-22.04-chromium-tip-of-tree
⚠️ [playwright-test] › ui-mode-test-output.spec.ts:80:5 › should show console messages for test @ubuntu-latest-node22-1
⚠️ [webkit-library] › library/ignorehttpserrors.spec.ts:30:3 › should isolate contexts @webkit-ubuntu-22.04-node18
⚠️ [webkit-page] › page/page-set-input-files.spec.ts:38:5 › should upload a folder @webkit-ubuntu-22.04-node18
⚠️ [playwright-test] › ui-mode-test-watch.spec.ts:145:5 › should watch all @windows-latest-node18-1

39282 passed, 808 skipped
✔️✔️✔️

Merge workflow run.

@mxschmitt mxschmitt requested a review from dgozman May 28, 2025 13:33
Copy link
Contributor

@dgozman dgozman left a comment

Choose a reason for hiding this comment

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

Could you please post an example of the code before and after, with a link to the repo source?

@mxschmitt
Copy link
Contributor Author

Could you please post an example of the code before and after, with a link to the repo source?

Something like microsoft/playwright-dotnet@f374bd0?

let suffix = ''
if (!['bool', 'int', 'System.Text.Json.JsonElement'].includes(inner.ts))
suffix = ' = null!;'
ts.push(`${indent}public ${inner.ts}${nullableSuffix(inner)} ${toTitleCase(name)} { get; set; }${suffix}`);
Copy link
Contributor

Choose a reason for hiding this comment

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

It seems like this forces everything to be non-null, instead of allowing nullable channels? Could you please explain better what we are trying to achieve?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

When enabling nullability in the repository .NET complains that these classes we generate can be created with null values which is correct. In order to avoid that there are three ways:

a) constructor which forces to initialise these values - this doesn't apply to JSON serialises, since these classes are never constructed via us, they are always constructed via the underlying json library. So thats not an option.
b) use the required attribute - this won't work since we have netstandard2.0 but it was introduced in netstandard2.1. So thats not an option.
c) use = null!; - its the verbose way of skipping this null check, internally it was null anyways. In the future we could change it to the required attribute once we move to a more recent TFM.

@mxschmitt mxschmitt merged commit a6f71a6 into microsoft:main May 28, 2025
29 checks passed
@mxschmitt mxschmitt deleted the dotnet/allow-nullable-channels branch May 28, 2025 19:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants