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

fix: prevent collection mapping using invalid length property #1108

Merged
merged 1 commit into from Feb 12, 2024

Conversation

TimothyMakkison
Copy link
Collaborator

@TimothyMakkison TimothyMakkison commented Feb 7, 2024

Fix bug where array to list mappings would attempt to use an invalid Length property on the lowered IReadOnlyCollection source.

Fixes #1106

  • EnumerableToList will always use Count instead of the non lowered sources CountPropertyName
    • I don't think this will cause issues, I did consider using CollectionInfoBuilder to get the new CountPropertyName
  • Added ArrayToListShouldImproveNullability test
  • Not sure if a similar issues exists with BuildEnumerableToArrayMapping, I think all calls to this method will always use Count for the original and lowered types. Perhaps CountPropertyName should be used here

Checklist

  • The existing code style is followed
  • The commit message follows our guidelines
  • Performed a self-review of my code
  • Unit tests are added/updated

Copy link

codecov bot commented Feb 7, 2024

Codecov Report

Attention: 3 lines in your changes are missing coverage. Please review.

Comparison is base (b9998a8) 91.38% compared to head (6dc09cb) 91.39%.

Files Patch % Lines
...numerables/EnsureCapacity/EnsureCapacityBuilder.cs 71.42% 0 Missing and 2 partials ⚠️
...iptors/MappingBuilders/DictionaryMappingBuilder.cs 92.30% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1108      +/-   ##
==========================================
+ Coverage   91.38%   91.39%   +0.01%     
==========================================
  Files         221      221              
  Lines        7300     7301       +1     
  Branches      931      931              
==========================================
+ Hits         6671     6673       +2     
+ Misses        411      410       -1     
  Partials      218      218              

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

Copy link
Contributor

@latonz latonz left a comment

Choose a reason for hiding this comment

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

Thank you for looking into this and contributing a fix!

Doesn't a similar problem exist in EnumerableMappingBuilder.BuildCustomTypeMapping with the EnsureCapacityBuilder.TryBuildEnsureCapacity(ctx) call using the old / wrong source count property? IMO the best solution would be to create a new CollectionInfos instance when overwriting the source type and passing that directly to TryBuildEnsureCapacity.
I think also DictionaryMappingBuilder.BuildCustomTypeMapping has a potential similar problem... I don't think it is a real problem there now (as it is always the Count property for dictionary), but I don't think the code now is really correct.

@TimothyMakkison TimothyMakkison force-pushed the collection_length branch 2 times, most recently from 5cbb0c0 to 6b57fef Compare February 10, 2024 13:02
@TimothyMakkison
Copy link
Collaborator Author

Another approach would probably be in
EnumerableMappingBuilder.BuildCollectionTypeForICollection instead of only returning the INamedTypeSymbol, also return the name of the count property:

CollectionInfo for IReadOnlyCollection / ICollection (I'd probably prefer this 🤔).

I opted to return CollectionInfo from BuildCollectionTypeForICollection.

  • Made CollectionInfoBuilder.BuildCollectionInfo public, passing in SymbolAccessor and WellKnownTypes is a bit of pain.
  • Updated TyrBuildEnsureCapacity to TryBuildEnsureCapacity(MappingBuilderContext ctx, CollectionInfo source, CollectionInfo target)
  • Updated all mappers to use the new EnsureCapacity
  • DictionaryMappingBuilder uses the correct IsCountKnown and CountProperty values.

Copy link
Contributor

@latonz latonz left a comment

Choose a reason for hiding this comment

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

Thank you for the updates, one small feedback point.

@latonz latonz merged commit 216f791 into riok:main Feb 12, 2024
19 checks passed
Copy link

🎉 This PR is included in version 3.4.0-next.4 🎉

The release is available on:

Your semantic-release bot 📦🚀

Copy link

🎉 This PR is included in version 3.4.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

IReadOnlyCollection as interface param uses Length instead of Count when source is an array
2 participants