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

Support TypeConverterAttribute on bound properties #36545

Open
kyschouv opened this issue Mar 18, 2017 · 8 comments
Open

Support TypeConverterAttribute on bound properties #36545

kyschouv opened this issue Mar 18, 2017 · 8 comments
Labels
area-Extensions-Configuration feature-request help wanted [up-for-grabs] Good issue for external contributors
Milestone

Comments

@kyschouv
Copy link

It's very difficult to add a custom TypeConverter for framework types to have them bind correctly within ConfigurationBinder. The documentation for TypeConverterAttribute specifies that the attribute can be attached to a property, and that property will then use the specified TypeConverter during serialization/deserialization. Unfortunately, ConfigurationBinder doesn't take the property's attributes into consideration during BindProperty, instead passing the binding off to BindInstance with just the type of the property.

Presently, the only way to use custom TypeConverters is to register them using TypeDescriptor.AddAttributes before any configuration binding occurs, which is inconvenient if your library requires custom TypeConverters for standard types.

@HaoK
Copy link
Member

HaoK commented Jun 6, 2017

@ajcvickers We should retriage this, as I think TypeConverters might be a good option for how we deal with custom config binding of optoins in general.

@HaoK
Copy link
Member

HaoK commented Jun 6, 2017

Also look into general binding for options + config

@ajcvickers ajcvickers transferred this issue from aspnet/Configuration Nov 14, 2018
@ajcvickers
Copy link
Contributor

For anyone interested in contributing this, there is a good start in the PR here: aspnet/Configuration#787

natemcmaster referenced this issue in dotnet/extensions Nov 21, 2018
…master

[automated] Merge branch 'release/2.2' => 'master'
@dgioulakis
Copy link

dgioulakis commented Feb 23, 2019

I would personally rather see support for Json.net JsonConverter. I prototyped a sample here: https://github.com/Cephei/JsonNetConfigurationBinder

TypeConverter is a nice to have, however, not very powerful in this context considering it requires the Value of the IConfigurationSection to be present. You can't use it to parse ConfigurationSection's with children like in the link above.

@analogrelay analogrelay transferred this issue from dotnet/extensions May 15, 2020
@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added area-System.Runtime untriaged New issue has not been triaged by the area owner labels May 15, 2020
@analogrelay analogrelay added this to the Future milestone May 15, 2020
@maryamariyan maryamariyan removed the untriaged New issue has not been triaged by the area owner label Jul 1, 2020
@jchannon
Copy link

jchannon commented Oct 6, 2020

Are there any plans to control the deserilaization of IConfiguration, little seems to have been discussed for a while now

@maryamariyan
Copy link
Member

We looked at the PR in aspnet/Configuration#787 again and seems like when this gets addressed again we make the fix works well with all configuration providers

@maryamariyan maryamariyan added the help wanted [up-for-grabs] Good issue for external contributors label Oct 19, 2020
@ericstj
Copy link
Member

ericstj commented Dec 3, 2020

I can see the value in this change as it allows folks to define custom handling of types at the member level without globally registering the converter. It also allows the model to specify the converter, rather than the caller to Bind.

It looks to me like the PR added this functionality exclusively in the Configuration.Binder. Would this be used by all Configuration providers? (honest question, I don't know, but I think it would)

I do think the support for TypeConverter in Confiugration today and the PR is incomplete. I think the right way to handle would be to go wholesale through the TypeDescriptor system as a fallback. Where you get the PropertyDescriptors for the type and use those to get the converter. In this way, Configuration never ends up doing the work to find those attributes and the TypeDescriptor infra would do that (along with all of its functionality for overrides).

I can imagine that a larger dependency on metadata here can be challenging for source-generators for Configuration, so that might be something to consider. I believe for WPF they determine at design time what converter is used and bake that into generated source / baml.

All this said, I'm not sure if we want to go down a path of tighter coupling of Configuration with some other serialization tech (TypeDescriptor, JSON, or otherwise). I'd be interested to hear @davidfowl's opinion.

@KalleOlaviNiemitalo
Copy link

I wanted to bind an options class that has a TimeSpan Timeout property in which Timeout.InfiniteTimeSpan makes sense. This special value could perhaps be configured as "-00:00:00.001" but that's just too ugly. I'd much prefer being able to set a per-property TypeConverter that recognizes "Infinite" or the empty string.

A nullable property TimeSpan? Timeout doesn't work either, as the constructor of the options class stores a nonzero default value there and ConfigurationBinder does not overwrite this default value if it gets null as out object result from TryConvertValue.

if (propertyValue != null && hasSetter)
{
property.SetValue(instance, propertyValue);
}

I suppose the workaround is to add a sibling bool UseTimeout property.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-Extensions-Configuration feature-request help wanted [up-for-grabs] Good issue for external contributors
Projects
None yet
Development

No branches or pull requests

10 participants