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

Inconsistent nullability change in TypeConverter.CanConvertTo resulting in a breaking API change with .NET 6.0 #63186

Closed
MichaelKetting opened this issue Dec 29, 2021 · 4 comments · Fixed by #63874

Comments

@MichaelKetting
Copy link
Contributor

In .NET 6.0, the nullability of the overload TypeConverter.CanConvertTo(ITypeDescriptorContext? context, Type destinationType) was changed. Now, the destinationType paramter is also nullable. The same change was not done with TypeConverter.CanConvertTo(Type destinationType) and TypeConverter.ConvertTo

https://docs.microsoft.com/en-us/dotnet/api/system.componentmodel.typeconverter.canconvertto?view=net-6.0
public virtual bool CanConvertTo (System.ComponentModel.ITypeDescriptorContext? context, Type? destinationType);

https://docs.microsoft.com/en-us/dotnet/api/system.componentmodel.typeconverter.canconvertto?view=net-5.0
public virtual bool CanConvertTo (System.ComponentModel.ITypeDescriptorContext context, Type destinationType);

I believe that destinationType being nullable is an API bug introduced in .NET 6.0 since it is a) inconsistent with the remaining API, b) not documented as a use case in MSDN, and c) appears to defeat the purpose of the conversion API since you need to know the destination type in order to make an informed choice.

Note that the new API breaks existing code based on .NET 5.0 (custom TypeConverter implementations).

Note that according to git, the entire nullability of TypeConverter was done with ##54961 after .NET 5.0 was already released, so I can't really backtrack this to the root cause.

@dotnet-issue-labeler dotnet-issue-labeler bot added area-System.ComponentModel untriaged New issue has not been triaged by the area owner labels Dec 29, 2021
@ghost
Copy link

ghost commented Dec 29, 2021

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

Issue Details

In .NET 6.0, the nullability of the overload TypeConverter.CanConvertTo(ITypeDescriptorContext? context, Type destinationType) was changed. Now, the destinationType paramter is also nullable. The same change was not done with TypeConverter.CanConvertTo(Type destinationType) and TypeConverter.ConvertTo

https://docs.microsoft.com/en-us/dotnet/api/system.componentmodel.typeconverter.canconvertto?view=net-6.0
public virtual bool CanConvertTo (System.ComponentModel.ITypeDescriptorContext? context, Type? destinationType);

https://docs.microsoft.com/en-us/dotnet/api/system.componentmodel.typeconverter.canconvertto?view=net-5.0
public virtual bool CanConvertTo (System.ComponentModel.ITypeDescriptorContext context, Type destinationType);

I believe that destinationType being nullable is an API bug introduced in .NET 6.0 since it is a) inconsistent with the remaining API, b) not documented as a use case in MSDN, and c) appears to defeat the purpose of the conversion API since you need to know the destination type in order to make an informed choice.

Note that the new API breaks existing code based on .NET 5.0 (custom TypeConverter implementations).

Note that according to git, the entire nullability of TypeConverter was done with ##54961 after .NET 5.0 was already released, so I can't really backtrack this to the root cause.

Author: MichaelKetting
Assignees: -
Labels:

area-System.ComponentModel, untriaged

Milestone: -

@eerhardt
Copy link
Member

eerhardt commented Jan 3, 2022

I believe the change to public virtual bool CanConvertTo(ITypeDescriptorContext? context, Type? destinationType) was a mistake in #54961. It doesn't make any sense to pass null here (even though it doesn't throw an exception if you do). The ConvertTo method will throw an ArgumentNullException if you do.

cc @krwq - who made the change. Any thoughts here?

@maryamariyan maryamariyan added this to the 7.0.0 milestone Jan 11, 2022
@maryamariyan maryamariyan removed the untriaged New issue has not been triaged by the area owner label Jan 11, 2022
@maryamariyan
Copy link
Member

@MichaelKetting thanks for the report, would you like to add a PR for this?

@MichaelKetting
Copy link
Contributor Author

@maryamariyan Sure thing!

@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Jan 17, 2022
MichaelKetting added a commit to MichaelKetting/dotnet-runtime that referenced this issue Feb 28, 2022
The nullability of parameter 'destinationType' in
TypeConverter.CanConvertTo(...) was changed from
not-nullable to nullable during the development of .NET 6.

Since a destination type supported by this TypeConverter
can never be null, a NotNullWhenAttribute is added to the
'destinationType' parameter when the result value of
TypeConverter.CanConvertTo(...) is 'true'.

Fix dotnet#63186
eerhardt pushed a commit that referenced this issue Mar 1, 2022
The nullability of parameter 'destinationType' in
TypeConverter.CanConvertTo(...) was changed from
not-nullable to nullable during the development of .NET 6.

Since a destination type supported by this TypeConverter
can never be null, a NotNullWhenAttribute is added to the
'destinationType' parameter when the result value of
TypeConverter.CanConvertTo(...) is 'true'.

Fix #63186
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Mar 1, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Mar 31, 2022
ViktorHofer pushed a commit to dotnet/winforms that referenced this issue Dec 5, 2022
)

The nullability of parameter 'destinationType' in
TypeConverter.CanConvertTo(...) was changed from
not-nullable to nullable during the development of .NET 6.

Since a destination type supported by this TypeConverter
can never be null, a NotNullWhenAttribute is added to the
'destinationType' parameter when the result value of
TypeConverter.CanConvertTo(...) is 'true'.

Fix dotnet/runtime#63186

Commit migrated from dotnet/runtime@ac246b1
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants