Skip to content
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

Use NullabilityInfoContext to determine dictionary value nullability #3041

Conversation

patrikwlund
Copy link
Contributor

@patrikwlund patrikwlund commented Aug 24, 2024

A follow-up of #3022 (issue) and #3023 (PR).

The #3023 PR broke the behavior for many of our IReadOnlyDictionary<string, string> (and similar) members.

The reason is that the logic for reading and interpreting NullableAttribute and NullableContextAttribute flags is too simplified and is incorrect for several cases. It just happens to pass the current test cases because of the layout of the test type and its members.

Too simplified

For example, it ignores the optimization of the flags array into a single byte if they are all the same value. An optimized Dictionary<string, string> has a single [1] flag, not two or three flags as the current logic expects.

It also does a lot of assumptions related to reference vs value types. An IDictionary itself can be a value type, as can the key, both of which would affect the length of the flag array. It should be checking the number of value types and the number of flags together.

It also doesn't seem to fully respect the NullableContextAttribute that's placed on the type containing the member. And the tests did not account for differences in the context value, as the testing type always only has a single flag. So the other flag value is not tested.

My suggested change

For NET 6 and onwards we should rely on the official NullabilityInfoContext to tell us what is and isn't nullable. For earlier versions we can still have custom logic, which we can try to improve on if we want to put in the effort. But it will require a lot of effort to make it fully compliant and flexible.

The tests I added do not pass with the custom flag logic, but they do pass with the NullabilityInfoContext code.

The tests could be more flexible or enumerated in other more dynamic ways if you would like, to get the number of lines down, but I tried to follow the existing pattern of laying them all out in-line.

@codecov-commenter
Copy link

codecov-commenter commented Aug 24, 2024

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

Attention: Patch coverage is 54.54545% with 5 lines in your changes missing coverage. Please review.

Project coverage is 90.46%. Comparing base (7a7230d) to head (bf5c554).

Files Patch % Lines
...SwaggerGen/SchemaGenerator/MemberInfoExtensions.cs 54.54% 5 Missing ⚠️

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3041      +/-   ##
==========================================
- Coverage   90.59%   90.46%   -0.13%     
==========================================
  Files          74       74              
  Lines        2988     2980       -8     
  Branches      478      470       -8     
==========================================
- Hits         2707     2696      -11     
- Misses        281      284       +3     
Flag Coverage Δ
Linux 90.46% <54.54%> (-0.13%) ⬇️
Windows 90.46% <54.54%> (-0.13%) ⬇️
macOS 90.46% <54.54%> (-0.13%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Collaborator

@martincostello martincostello left a comment

Choose a reason for hiding this comment

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

Thanks for the PR! Just a few questions but otherwise this looks great.

@martincostello martincostello added this to the v6.7.2 milestone Aug 24, 2024
@martincostello martincostello merged commit 31ad99f into domaindrivendev:master Aug 24, 2024
9 checks passed
@martincostello
Copy link
Collaborator

Thanks for fixing this - the fix is now available in NuGet as version 6.7.2.

@patrikwlund patrikwlund deleted the pw/nullabilityinfo-dictionary branch August 24, 2024 13:47
patrikwlund added a commit to patrikwlund/Swashbuckle.AspNetCore that referenced this pull request Aug 26, 2024
Add-on to domaindrivendev#3043
(should be no conflict)

Very similar to my previous PR: domaindrivendev#3041

In his PR, @VladimirTyrin surgically fixes a specific problem for all platforms. I suggest we use the new and fancy NET6+ toys when available, to avoid nullable logic completely in the future. I tested it with the same added test as in @VladimirTyrin's original PR.
renovate bot added a commit to orso-co/Orso.Arpa.Api that referenced this pull request Aug 27, 2024
[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
|
[Swashbuckle.AspNetCore](https://github.com/domaindrivendev/Swashbuckle.AspNetCore)
| `6.7.1` -> `6.7.2` |
[![age](https://developer.mend.io/api/mc/badges/age/nuget/Swashbuckle.AspNetCore/6.7.2?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://developer.mend.io/api/mc/badges/adoption/nuget/Swashbuckle.AspNetCore/6.7.2?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://developer.mend.io/api/mc/badges/compatibility/nuget/Swashbuckle.AspNetCore/6.7.1/6.7.2?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://developer.mend.io/api/mc/badges/confidence/nuget/Swashbuckle.AspNetCore/6.7.1/6.7.2?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|

---

### Release Notes

<details>
<summary>domaindrivendev/Swashbuckle.AspNetCore
(Swashbuckle.AspNetCore)</summary>

###
[`v6.7.2`](https://github.com/domaindrivendev/Swashbuckle.AspNetCore/releases/tag/v6.7.2)

#### What's Changed

- Use NullabilityInfoContext to determine dictionary value nullability
by [@&#8203;patrikwlund](https://github.com/patrikwlund) in
[domaindrivendev/Swashbuckle.AspNetCore#3041

**Full Changelog**:
domaindrivendev/Swashbuckle.AspNetCore@v6.7.1...v6.7.2

</details>

---

### Configuration

📅 **Schedule**: Branch creation - "after 10pm every weekday,every
weekend,before 5am every weekday" in timezone Europe/Berlin, Automerge -
At any time (no schedule defined).

🚦 **Automerge**: Enabled.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR was generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View the
[repository job
log](https://developer.mend.io/github/orso-co/Orso.Arpa.Api).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzOC4yNi4xIiwidXBkYXRlZEluVmVyIjoiMzguMjYuMSIsInRhcmdldEJyYW5jaCI6ImRldmVsb3AiLCJsYWJlbHMiOltdfQ==-->

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
renovate bot added a commit to smartive/cas-fee-adv-mumble-api that referenced this pull request Sep 5, 2024
This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
|
[Swashbuckle.AspNetCore](https://github.com/domaindrivendev/Swashbuckle.AspNetCore)
| `6.6.2` -> `6.7.3` |
[![age](https://developer.mend.io/api/mc/badges/age/nuget/Swashbuckle.AspNetCore/6.7.3?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://developer.mend.io/api/mc/badges/adoption/nuget/Swashbuckle.AspNetCore/6.7.3?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://developer.mend.io/api/mc/badges/compatibility/nuget/Swashbuckle.AspNetCore/6.6.2/6.7.3?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://developer.mend.io/api/mc/badges/confidence/nuget/Swashbuckle.AspNetCore/6.6.2/6.7.3?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
|
[Swashbuckle.AspNetCore.Annotations](https://github.com/domaindrivendev/Swashbuckle.AspNetCore)
| `6.6.2` -> `6.7.3` |
[![age](https://developer.mend.io/api/mc/badges/age/nuget/Swashbuckle.AspNetCore.Annotations/6.7.3?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://developer.mend.io/api/mc/badges/adoption/nuget/Swashbuckle.AspNetCore.Annotations/6.7.3?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://developer.mend.io/api/mc/badges/compatibility/nuget/Swashbuckle.AspNetCore.Annotations/6.6.2/6.7.3?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://developer.mend.io/api/mc/badges/confidence/nuget/Swashbuckle.AspNetCore.Annotations/6.6.2/6.7.3?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|

---

### Release Notes

<details>
<summary>domaindrivendev/Swashbuckle.AspNetCore
(Swashbuckle.AspNetCore)</summary>

###
[`v6.7.3`](https://github.com/domaindrivendev/Swashbuckle.AspNetCore/releases/tag/v6.7.3)

##### What's Changed

- Fix nested types nullable context check by
[@&#8203;VladimirTyrin](https://github.com/VladimirTyrin) in
[domaindrivendev/Swashbuckle.AspNetCore#3043
- Use NullabilityInfoContext to determine if member is nullable by
[@&#8203;patrikwlund](https://github.com/patrikwlund) in
[domaindrivendev/Swashbuckle.AspNetCore#3046

##### New Contributors

- [@&#8203;VladimirTyrin](https://github.com/VladimirTyrin) made their
first contribution in
[domaindrivendev/Swashbuckle.AspNetCore#3043

**Full Changelog**:
domaindrivendev/Swashbuckle.AspNetCore@v6.7.2...v6.7.3

###
[`v6.7.2`](https://github.com/domaindrivendev/Swashbuckle.AspNetCore/releases/tag/v6.7.2)

#### What's Changed

- Use NullabilityInfoContext to determine dictionary value nullability
by [@&#8203;patrikwlund](https://github.com/patrikwlund) in
[domaindrivendev/Swashbuckle.AspNetCore#3041

**Full Changelog**:
domaindrivendev/Swashbuckle.AspNetCore@v6.7.1...v6.7.2

###
[`v6.7.1`](https://github.com/domaindrivendev/Swashbuckle.AspNetCore/releases/tag/v6.7.1)

#### What's Changed

- docs: Update README.md by
[@&#8203;WeihanLi](https://github.com/WeihanLi) in
[domaindrivendev/Swashbuckle.AspNetCore#3002
- Support `[DataMember]` `IsRequired` in
`NewtonsoftDataContractResolver` by
[@&#8203;ouvreboite](https://github.com/ouvreboite) in
[domaindrivendev/Swashbuckle.AspNetCore#2644
- Add API analysers by
[@&#8203;martincostello](https://github.com/martincostello) in
[domaindrivendev/Swashbuckle.AspNetCore#3003
- Update README by
[@&#8203;martincostello](https://github.com/martincostello) in
[domaindrivendev/Swashbuckle.AspNetCore#3004
- docs: fix example code formatting by
[@&#8203;WeihanLi](https://github.com/WeihanLi) in
[domaindrivendev/Swashbuckle.AspNetCore#3010
- Fixes nullability problems with dictionaries by
[@&#8203;ozziepeeps](https://github.com/ozziepeeps) in
[domaindrivendev/Swashbuckle.AspNetCore#3023
- Fix handling of nullable structs by
[@&#8203;martincostello](https://github.com/martincostello) in
[domaindrivendev/Swashbuckle.AspNetCore#3015
- Fix missing form parameter XML documentation by
[@&#8203;martincostello](https://github.com/martincostello) in
[domaindrivendev/Swashbuckle.AspNetCore#3020

#### New Contributors

- [@&#8203;ouvreboite](https://github.com/ouvreboite) made their first
contribution in
[domaindrivendev/Swashbuckle.AspNetCore#2644

**Full Changelog**:
domaindrivendev/Swashbuckle.AspNetCore@v6.7.0...v6.7.1

###
[`v6.7.0`](https://github.com/domaindrivendev/Swashbuckle.AspNetCore/releases/tag/v6.7.0)

#### What's Changed

- Allow Swagger UI CSS and JS paths to be configurable by
[@&#8203;mag1art](https://github.com/mag1art) in
[domaindrivendev/Swashbuckle.AspNetCore#2908
- Add `IncludeXmlCommentsForAssembly()` convience overload by
[@&#8203;leotsarev](https://github.com/leotsarev) in
[domaindrivendev/Swashbuckle.AspNetCore#2909
- Add snapshot tests using Verify by
[@&#8203;keahpeters](https://github.com/keahpeters) in
[domaindrivendev/Swashbuckle.AspNetCore#2929
- Add posibility to ignore properties in `[FromForm]` with
`[SwaggerIgnore]` by
[@&#8203;jgarciadelanoceda](https://github.com/jgarciadelanoceda) in
[domaindrivendev/Swashbuckle.AspNetCore#2928
- Adding check for existing directory and creating if doesn't exist by
[@&#8203;matt-lethargic](https://github.com/matt-lethargic) in
[domaindrivendev/Swashbuckle.AspNetCore#2927
- Default null value on nullable types caused errors by
[@&#8203;jgarciadelanoceda](https://github.com/jgarciadelanoceda) in
[domaindrivendev/Swashbuckle.AspNetCore#2941
- Add additional Verify tests by
[@&#8203;keahpeters](https://github.com/keahpeters) in
[domaindrivendev/Swashbuckle.AspNetCore#2950
- Only apply a SchemaFilter to create the description on SwaggerUI by
[@&#8203;jgarciadelanoceda](https://github.com/jgarciadelanoceda) in
[domaindrivendev/Swashbuckle.AspNetCore#2943
- Add support for async filters by
[@&#8203;mauve](https://github.com/mauve) in
[domaindrivendev/Swashbuckle.AspNetCore#2938
- Fix package validation by
[@&#8203;martincostello](https://github.com/martincostello) in
[domaindrivendev/Swashbuckle.AspNetCore#2926
- Adding support for .NET 8 Model State attributes by
[@&#8203;jgarciadelanoceda](https://github.com/jgarciadelanoceda) in
[domaindrivendev/Swashbuckle.AspNetCore#2958
- Only set Exclusive Range when they are by
[@&#8203;jgarciadelanoceda](https://github.com/jgarciadelanoceda) in
[domaindrivendev/Swashbuckle.AspNetCore#2960
- `[AsParameters]` throwing error on cast when showing the description
with `EnableAnnotations()` by
[@&#8203;jgarciadelanoceda](https://github.com/jgarciadelanoceda) in
[domaindrivendev/Swashbuckle.AspNetCore#2962
- Fix `RequestBodyFilterAnnotation` and `MultipleFromForm` for Minimal
APIs by
[@&#8203;jgarciadelanoceda](https://github.com/jgarciadelanoceda) in
[domaindrivendev/Swashbuckle.AspNetCore#2963
- Swagger UI refactoring by
[@&#8203;martincostello](https://github.com/martincostello) in
[domaindrivendev/Swashbuckle.AspNetCore#2942
- Add help wanted badge by
[@&#8203;martincostello](https://github.com/martincostello) in
[domaindrivendev/Swashbuckle.AspNetCore#2911
- Move inline css and js to external files for SwaggerUI and ReDoc by
[@&#8203;junior-santana](https://github.com/junior-santana) in
[domaindrivendev/Swashbuckle.AspNetCore#2965
- Missing properties section when generating
`IFomFile`/`IFormFileCollection` by
[@&#8203;jgarciadelanoceda](https://github.com/jgarciadelanoceda) in
[domaindrivendev/Swashbuckle.AspNetCore#2972
- Missing Encoding and RequiredProperties when `IFormFile` with OpenAPI
by [@&#8203;jgarciadelanoceda](https://github.com/jgarciadelanoceda)
in
[domaindrivendev/Swashbuckle.AspNetCore#2979
- Use `ApiParameter.Type` by
[@&#8203;jgarciadelanoceda](https://github.com/jgarciadelanoceda) in
[domaindrivendev/Swashbuckle.AspNetCore#2980
- Document arrays of generic parameters with XML comments and support
overload methods by
[@&#8203;jgarciadelanoceda](https://github.com/jgarciadelanoceda) in
[domaindrivendev/Swashbuckle.AspNetCore#2982
- Take into account \[JsonRequired] for System.Text.Json by
[@&#8203;jgarciadelanoceda](https://github.com/jgarciadelanoceda) in
[domaindrivendev/Swashbuckle.AspNetCore#2988
- Configure non-nullable types as required by
[@&#8203;AntiGuideAkquinet](https://github.com/AntiGuideAkquinet) in
[domaindrivendev/Swashbuckle.AspNetCore#2803
- Use `HttpMethods.IsGet()` by
[@&#8203;martincostello](https://github.com/martincostello) in
[domaindrivendev/Swashbuckle.AspNetCore#2971

#### New Contributors

- [@&#8203;mag1art](https://github.com/mag1art) made their first
contribution in
[domaindrivendev/Swashbuckle.AspNetCore#2908
- [@&#8203;jgarciadelanoceda](https://github.com/jgarciadelanoceda)
made their first contribution in
[domaindrivendev/Swashbuckle.AspNetCore#2928
- [@&#8203;matt-lethargic](https://github.com/matt-lethargic) made
their first contribution in
[domaindrivendev/Swashbuckle.AspNetCore#2927
- [@&#8203;mauve](https://github.com/mauve) made their first
contribution in
[domaindrivendev/Swashbuckle.AspNetCore#2938
- [@&#8203;junior-santana](https://github.com/junior-santana) made
their first contribution in
[domaindrivendev/Swashbuckle.AspNetCore#2965
- [@&#8203;AntiGuideAkquinet](https://github.com/AntiGuideAkquinet)
made their first contribution in
[domaindrivendev/Swashbuckle.AspNetCore#2803

**Full Changelog**:
domaindrivendev/Swashbuckle.AspNetCore@v6.6.2...v6.7.0

</details>

---

### Configuration

📅 **Schedule**: Branch creation - "after 9pm,before 6am" in timezone
Europe/Zurich, Automerge - At any time (no schedule defined).

🚦 **Automerge**: Enabled.

♻ **Rebasing**: Whenever PR is behind base branch, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about these
updates again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR was generated by [Mend Renovate](https://mend.io/renovate/).
View the [repository job
log](https://developer.mend.io/github/smartive/cas-fee-adv-mumble-api).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy40NDAuNyIsInVwZGF0ZWRJblZlciI6IjM4LjU2LjAiLCJ0YXJnZXRCcmFuY2giOiJtYWluIiwibGFiZWxzIjpbImRlcGVuZGVuY2llcyJdfQ==-->

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants