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

Analyzer/Code fix idea: JsonConverters applied to properties implementing a collection #85540

Open
WhitWaldo opened this issue Apr 28, 2023 · 3 comments
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Text.Json code-analyzer Marks an issue that suggests a Roslyn analyzer
Milestone

Comments

@WhitWaldo
Copy link

Per #85479 and #54189 the preferred approach to apply a custom JsonConverter is to apply the converter type to the type being converted so that it's used whether (de)serializing a single entity or a collection of entities using that converter.

Having not realized that, I would propose that an analyzer (potentially with a code fix) be added that identifies when [JsonConverter] has been applied to a property that itself represents a collection of the type the JsonConverter is typed for since this will actually throw at runtime. The code fix should move the [JsonConverter] attribute from its position on any various properties to the type definition itself (when available in the same assembly) so it's applied consistently whether to a single or collection of the type.

Thank you for the consideration!

@ghost ghost added the untriaged New issue has not been triaged by the area owner label Apr 28, 2023
@ghost
Copy link

ghost commented Apr 28, 2023

Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis
See info in area-owners.md if you want to be subscribed.

Issue Details

Per #85479 and #54189 the preferred approach to apply a custom JsonConverter is to apply the converter type to the type being converted so that it's used whether (de)serializing a single entity or a collection of entities using that converter.

Having not realized that, I would propose that an analyzer (potentially with a code fix) be added that identifies when [JsonConverter] has been applied to a property that itself represents a collection of the type the JsonConverter is typed for since this will actually throw at runtime. The code fix should move the [JsonConverter] attribute from its position on any various properties to the type definition itself (when available in the same assembly) so it's applied consistently whether to a single or collection of the type.

Thank you for the consideration!

Author: WhitWaldo
Assignees: -
Labels:

area-System.Text.Json

Milestone: -

@eiriktsarpalis
Copy link
Member

It's an interesting proposal, we could consider it in the future. Thanks!

@eiriktsarpalis eiriktsarpalis removed the untriaged New issue has not been triaged by the area owner label May 2, 2023
@eiriktsarpalis eiriktsarpalis added this to the Future milestone May 2, 2023
@eiriktsarpalis eiriktsarpalis added the code-analyzer Marks an issue that suggests a Roslyn analyzer label May 2, 2023
@buyaa-n buyaa-n added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label Mar 12, 2024
@gpuchtel
Copy link

@eiriktsarpalis Why is this just "an interesting proposal?". This, IMO is a fundamental usage issue. I'm surprised (sad even) that Microsoft doesn't see it the same way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Text.Json code-analyzer Marks an issue that suggests a Roslyn analyzer
Projects
None yet
Development

No branches or pull requests

4 participants