Annotate SchemaRepository for nullable reference types#3958
Open
KitKeen wants to merge 1 commit into
Open
Conversation
…endev#3659) Add `#nullable enable` to `SchemaRepository.cs` so that consumers in nullable-enabled contexts get proper flow analysis around `TryLookupByType`: the `[NotNullWhen(true)]` attribute can now participate in null-state tracking because the `out` parameter is declared as `OpenApiSchemaReference?` rather than the implicitly non-null oblivious type. Project-wide nullability tracking has not yet been adopted (tracked by the TODO in Directory.Build.props), so RS0037 is suppressed locally for this file to avoid forcing assembly-wide adoption from a single-file change. Adds three tests: - `TryLookupByType` returns true and a non-null reference for a known type. - `TryLookupByType` returns false and a null reference for an unknown type. - The out parameter carries `[NotNullWhen(true)]`, ensuring the metadata that downstream callers rely on for null-state tracking isn't accidentally removed. Fixes domaindrivendev#3659.
| if (result) | ||
| { | ||
| referenceSchema = new OpenApiSchemaReference(schemaId); | ||
| referenceSchema = new OpenApiSchemaReference(schemaId!); |
Collaborator
There was a problem hiding this comment.
To avoid this mismatch, _reservedIds could be changed to be of type Dictionary<Type, string?>. Otherwise the value should be verifiably non-null so we don't need to use ! here.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #3958 +/- ##
=======================================
Coverage 95.04% 95.04%
=======================================
Files 111 111
Lines 3958 3958
Branches 801 801
=======================================
Hits 3762 3762
Misses 196 196
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
SchemaRepository.TryLookupByTypealready carries[NotNullWhen(true)], but because the file is not#nullable enabled theoutparameter is declared as the oblivious typeOpenApiSchemaReference. In a nullable-enabled caller the compiler treats the out value as non-null regardless of the return, so theNotNullWhenattribute can't actually drive null-state flow — hidden null-deref bugs slip past the compiler.Calling code currently needs a wrapper to recover the contract:
Change
#nullable enabletoSchemaRepository.cs.DocumentNameproperty /TryLookupByTypeout parameter / local TryGet outs.RS0037locally — project-wide PublicAPI nullability tracking has not yet been adopted (see TODO inDirectory.Build.props), so this single-file opt-in mustn't force assembly-wide rollout. PublicAPI tracker still records the surface (no nullability info), the analyzer is satisfied.Adds three unit tests in
SchemaRepositoryTests:TryLookupByType_KnownType_ReturnsTrueAndNonNullReferenceTryLookupByType_UnknownType_ReturnsFalseAndNullReferenceTryLookupByType_NotNullWhenAttributeIsAppliedToOutParameter(reflection guard so the contract metadata can't be removed accidentally)Verification
Full test suite (slnx) green: 996 tests, 0 failures across the four test assemblies that run on net10.0.
Fixes #3659.