-
-
Notifications
You must be signed in to change notification settings - Fork 158
Nullable reference types #1095
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Nullable reference types #1095
Conversation
7fbd4dd
to
8663e30
Compare
…incoming null (not empty). Made non-public TryXXX methods tolerant against incoming nulls.
b9db066
to
5176b8f
Compare
- Removed LinkBuilderGetNamespaceFromPathBenchmarks, we don't have such code anymore - Cleanup query string benchmark
…tion tests, FluentAssertions everywhere
Due to recent enhancements, this produces better error messages for missing required relationships and avoids 500 errors due to foreign key constraint violations.
…d. This relies on the fact that the resource model is frozen after building the resource graph. | Method | Mean | Error | StdDev | | ------------------------------------ | -------- | ------- | ------- | | SerializeOperationsResponse (before) | 168.6 us | 1.74 us | 1.63 us | | SerializeOperationsResponse (after) | 148.5 us | 1.06 us | 0.99 us | | SerializeResourceResponse (before) | 123.7 us | 1.12 us | 1.05 us | | SerializeResourceResponse (after) | 119.6 us | 1.05 us | 0.98 us | This makes SerializeOperationsResponse 12% faster and SerializeResourceResponse 3% faster. What the benchmark does not show is the performance improvement on subsequent requests, so in practice the gain in higher.
4b4d898
to
1a57ea0
Compare
ba26952
to
a1dcf87
Compare
Codecov Report
@@ Coverage Diff @@
## master #1095 +/- ##
==========================================
+ Coverage 88.21% 88.54% +0.32%
==========================================
Files 256 255 -1
Lines 6931 7086 +155
==========================================
+ Hits 6114 6274 +160
+ Misses 817 812 -5
Continue to review full report at Codecov.
|
In the past, [cibuild hung after update to the latest version](#1045). Since then, the codebase has changed and a new minor version was released. These result in the cibuild no longer hanging. Stats from my laptop (old=v2021.1.4, new=v2021.2.2): ``` master, inspectcode (old): 2:59 master, inspectcode (new): 2:00 nrt, inspectcode (old): 3:04 nrt, inspectcode (new): 2:33 master, cleanupcode (old): 11:06 master, cleanupcode (new): 5:39 nrt, cleanupcode (old): 11:59 nrt, cleanupcode (new): 6:34 ``` So the newer versions got faster. And cleanupcode still takes the most time. In this PR, 722 of 1020 files have changed, which is 70% of the total codebase. Because during PR cibuild, we run cleanupcode only on changed files (in chunks), this PR takes very long and sometimes times out. This is an exceptional case, future PRs shouldn't touch so many files. So in the end, I believe this update is the best way forward.
d592bd2
to
8002a53
Compare
* Renamed TryXXX resource graph lookup methods to FindXXX, throwing on incoming null (not empty). Made non-public TryXXX methods tolerant against incoming nulls. * Added directive to disable NRT in all source files * Turn on NRT globally * Annotated JsonApiDotNetCore library * Use alternate public name for attribute, to improve test coverage * Annotated example projects * Annotated benchmark project - Removed LinkBuilderGetNamespaceFromPathBenchmarks, we don't have such code anymore - Cleanup query string benchmark * Enhanced rendering of error.source.pointer on ModelState validation errors * Respect configured MaxModelValidationErrors in operations * Added docs for nullable reference types * Automated cleanup code * Fixed: do not fail when clearing a required to-many relationship * Annotated controllers in integration tests * Annotated DbContexts in integration tests * Annotated fakers in integration tests * Annotated TestBuildingBlocks * Use Should() replacements that flow nullability * Re-align similar testsets * Annotated test projects, use fakers for non-trivial models in integration tests, FluentAssertions everywhere * Fixed: make faker dates deterministic See https://giters.com/bchavez/Bogus/issues/247 * Enable ASP.NET ModelState validation by default Due to recent enhancements, this produces better error messages for missing required relationships and avoids 500 errors due to foreign key constraint violations. * Check off roadmap entry * Documented required one-to-one relationships mapping * Added missing HEAD routes * Prefer IDictionary<,>.TryGetValue() over .Contains() followed by indexer lookup * Optimized response rendering time when no sparse fieldset is requested. This relies on the fact that the resource model is frozen after building the resource graph. | Method | Mean | Error | StdDev | | ------------------------------------ | -------- | ------- | ------- | | SerializeOperationsResponse (before) | 168.6 us | 1.74 us | 1.63 us | | SerializeOperationsResponse (after) | 148.5 us | 1.06 us | 0.99 us | | SerializeResourceResponse (before) | 123.7 us | 1.12 us | 1.05 us | | SerializeResourceResponse (after) | 119.6 us | 1.05 us | 0.98 us | This makes SerializeOperationsResponse 12% faster and SerializeResourceResponse 3% faster. What the benchmark does not show is the performance improvement on subsequent requests, so in practice the gain in higher. * Fixed: handle nulls in request body * Workaround for crash in cibuild * Update to latest JetBrains tools In the past, [cibuild hung after update to the latest version](#1045). Since then, the codebase has changed and a new minor version was released. These result in the cibuild no longer hanging. Stats from my laptop (old=v2021.1.4, new=v2021.2.2): ``` master, inspectcode (old): 2:59 master, inspectcode (new): 2:00 nrt, inspectcode (old): 3:04 nrt, inspectcode (new): 2:33 master, cleanupcode (old): 11:06 master, cleanupcode (new): 5:39 nrt, cleanupcode (old): 11:59 nrt, cleanupcode (new): 6:34 ``` So the newer versions got faster. And cleanupcode still takes the most time. In this PR, 722 of 1020 files have changed, which is 70% of the total codebase. Because during PR cibuild, we run cleanupcode only on changed files (in chunks), this PR takes very long and sometimes times out. This is an exceptional case, future PRs shouldn't touch so many files. So in the end, I believe this update is the best way forward. * Package updates * Review non-public TryXXX methods * Addressed review feedback
* Renamed TryXXX resource graph lookup methods to FindXXX, throwing on incoming null (not empty). Made non-public TryXXX methods tolerant against incoming nulls. * Added directive to disable NRT in all source files * Turn on NRT globally * Annotated JsonApiDotNetCore library * Use alternate public name for attribute, to improve test coverage * Annotated example projects * Annotated benchmark project - Removed LinkBuilderGetNamespaceFromPathBenchmarks, we don't have such code anymore - Cleanup query string benchmark * Enhanced rendering of error.source.pointer on ModelState validation errors * Respect configured MaxModelValidationErrors in operations * Added docs for nullable reference types * Automated cleanup code * Fixed: do not fail when clearing a required to-many relationship * Annotated controllers in integration tests * Annotated DbContexts in integration tests * Annotated fakers in integration tests * Annotated TestBuildingBlocks * Use Should() replacements that flow nullability * Re-align similar testsets * Annotated test projects, use fakers for non-trivial models in integration tests, FluentAssertions everywhere * Fixed: make faker dates deterministic See https://giters.com/bchavez/Bogus/issues/247 * Enable ASP.NET ModelState validation by default Due to recent enhancements, this produces better error messages for missing required relationships and avoids 500 errors due to foreign key constraint violations. * Check off roadmap entry * Documented required one-to-one relationships mapping * Added missing HEAD routes * Prefer IDictionary<,>.TryGetValue() over .Contains() followed by indexer lookup * Optimized response rendering time when no sparse fieldset is requested. This relies on the fact that the resource model is frozen after building the resource graph. | Method | Mean | Error | StdDev | | ------------------------------------ | -------- | ------- | ------- | | SerializeOperationsResponse (before) | 168.6 us | 1.74 us | 1.63 us | | SerializeOperationsResponse (after) | 148.5 us | 1.06 us | 0.99 us | | SerializeResourceResponse (before) | 123.7 us | 1.12 us | 1.05 us | | SerializeResourceResponse (after) | 119.6 us | 1.05 us | 0.98 us | This makes SerializeOperationsResponse 12% faster and SerializeResourceResponse 3% faster. What the benchmark does not show is the performance improvement on subsequent requests, so in practice the gain in higher. * Fixed: handle nulls in request body * Workaround for crash in cibuild * Update to latest JetBrains tools In the past, [cibuild hung after update to the latest version](#1045). Since then, the codebase has changed and a new minor version was released. These result in the cibuild no longer hanging. Stats from my laptop (old=v2021.1.4, new=v2021.2.2): ``` master, inspectcode (old): 2:59 master, inspectcode (new): 2:00 nrt, inspectcode (old): 3:04 nrt, inspectcode (new): 2:33 master, cleanupcode (old): 11:06 master, cleanupcode (new): 5:39 nrt, cleanupcode (old): 11:59 nrt, cleanupcode (new): 6:34 ``` So the newer versions got faster. And cleanupcode still takes the most time. In this PR, 722 of 1020 files have changed, which is 70% of the total codebase. Because during PR cibuild, we run cleanupcode only on changed files (in chunks), this PR takes very long and sometimes times out. This is an exceptional case, future PRs shouldn't touch so many files. So in the end, I believe this update is the best way forward. * Package updates * Review non-public TryXXX methods * Addressed review feedback
* Renamed TryXXX resource graph lookup methods to FindXXX, throwing on incoming null (not empty). Made non-public TryXXX methods tolerant against incoming nulls. * Added directive to disable NRT in all source files * Turn on NRT globally * Annotated JsonApiDotNetCore library * Use alternate public name for attribute, to improve test coverage * Annotated example projects * Annotated benchmark project - Removed LinkBuilderGetNamespaceFromPathBenchmarks, we don't have such code anymore - Cleanup query string benchmark * Enhanced rendering of error.source.pointer on ModelState validation errors * Respect configured MaxModelValidationErrors in operations * Added docs for nullable reference types * Automated cleanup code * Fixed: do not fail when clearing a required to-many relationship * Annotated controllers in integration tests * Annotated DbContexts in integration tests * Annotated fakers in integration tests * Annotated TestBuildingBlocks * Use Should() replacements that flow nullability * Re-align similar testsets * Annotated test projects, use fakers for non-trivial models in integration tests, FluentAssertions everywhere * Fixed: make faker dates deterministic See https://giters.com/bchavez/Bogus/issues/247 * Enable ASP.NET ModelState validation by default Due to recent enhancements, this produces better error messages for missing required relationships and avoids 500 errors due to foreign key constraint violations. * Check off roadmap entry * Documented required one-to-one relationships mapping * Added missing HEAD routes * Prefer IDictionary<,>.TryGetValue() over .Contains() followed by indexer lookup * Optimized response rendering time when no sparse fieldset is requested. This relies on the fact that the resource model is frozen after building the resource graph. | Method | Mean | Error | StdDev | | ------------------------------------ | -------- | ------- | ------- | | SerializeOperationsResponse (before) | 168.6 us | 1.74 us | 1.63 us | | SerializeOperationsResponse (after) | 148.5 us | 1.06 us | 0.99 us | | SerializeResourceResponse (before) | 123.7 us | 1.12 us | 1.05 us | | SerializeResourceResponse (after) | 119.6 us | 1.05 us | 0.98 us | This makes SerializeOperationsResponse 12% faster and SerializeResourceResponse 3% faster. What the benchmark does not show is the performance improvement on subsequent requests, so in practice the gain in higher. * Fixed: handle nulls in request body * Workaround for crash in cibuild * Update to latest JetBrains tools In the past, [cibuild hung after update to the latest version](#1045). Since then, the codebase has changed and a new minor version was released. These result in the cibuild no longer hanging. Stats from my laptop (old=v2021.1.4, new=v2021.2.2): ``` master, inspectcode (old): 2:59 master, inspectcode (new): 2:00 nrt, inspectcode (old): 3:04 nrt, inspectcode (new): 2:33 master, cleanupcode (old): 11:06 master, cleanupcode (new): 5:39 nrt, cleanupcode (old): 11:59 nrt, cleanupcode (new): 6:34 ``` So the newer versions got faster. And cleanupcode still takes the most time. In this PR, 722 of 1020 files have changed, which is 70% of the total codebase. Because during PR cibuild, we run cleanupcode only on changed files (in chunks), this PR takes very long and sometimes times out. This is an exceptional case, future PRs shouldn't touch so many files. So in the end, I believe this update is the best way forward. * Package updates * Review non-public TryXXX methods * Addressed review feedback
Closes #1029.
This PR annotates the codebase for use with nullable reference types (NRT).
This does not mean your API project that uses JADNC must do so too. But once you do, additional warnings will light up for possible
NullReferenceException
s and unneeded null checks in your code.Overview of changes:
ResourceType
(formerlyResourceContext
) now providesGet
methods that throw when not found andFind
methods that returnnull
when not found. For example:GetAttributeByPublicName()
vsFindAttributeByPublicName()
. The same applies to methods onIResourceGraph
. For example:GetResourceType()
vsFindResourceType()
.IResourceGraph
was added to Controller constructors.error.source.pointer
for required relationships that are missing in a post/patch request body when ASP.NET ModelState validation is enabled. And it takesJsonPropertyNameAttribute
fromSystem.Text.Json
into account when properties on complex attributes are named differently. For details, see ./test/JsonApiDotNetCoreTests/UnitTests/ModelStateValidation/ModelStateValidationTests.cs.[Required]
on attributes and relationships provides the same benefits.MaxModelValidationErrors
(https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.mvcoptions.maxmodelvalidationerrors) in atomic:operations requests, which can be set inservices.AddMvcCore()
(defaults to 200).null
.QUALITY CHECKLIST