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

Configuration binding swallows the conversion exception #73915

Closed
Tracked by #77390
jvmlet opened this issue Aug 14, 2022 · 10 comments
Closed
Tracked by #77390

Configuration binding swallows the conversion exception #73915

jvmlet opened this issue Aug 14, 2022 · 10 comments
Labels
area-Extensions-Configuration breaking-change Issue or PR that represents a breaking API or functional change over a prerelease.
Milestone

Comments

@jvmlet
Copy link

jvmlet commented Aug 14, 2022

Description

Configuration binding still swallows the conversion exception (#36502 ,#36129 and #36015 are still reproducible with 6.0.0, )

Reproduction Steps

public enum  Ingredients {
A,
B
}

public class  MyModel{
public  Ingredients[] {get;set;}
}

Starting the app with myapp.exe --MyModel:Ingredients:0 A --MyModel:Ingredients:1 C starts up with Ingredients == [A]

Expected behavior

Exception should be thrown when calling iConfiguration.GetSection("MyModel").Bind(new MyModel(),opts => opts.ErrorOnUnknownConfiguration = true))

Actual behavior

application starts with Ingredients == [A]

Regression?

No response

Known Workarounds

No response

Configuration

net5.0 , <PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="6.0.0" />

Other information

(

)

@ghost ghost added the untriaged New issue has not been triaged by the area owner label Aug 14, 2022
@ghost
Copy link

ghost commented Aug 14, 2022

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

Issue Details

Description

Configuration binding still swallows the conversion exception (#36502 is still reproducible with 6.0.0 )

Reproduction Steps

public enum  Ingredients {
A,
B
}

public class  MyModel{
public  Ingredients[] {get;set;}
}

Starting the app with myapp.exe --MyModel:Ingredients:0 A --MyModel:Ingredients:1 C starts up with Ingredients == [A]

Expected behavior

Exception should be thrown when calling iConfiguration.GetSection("MyModel").Bind(new MyModel(),opts => opts.ErrorOnUnknownConfiguration = true))

Actual behavior

application starts with Ingredients == [A]

Regression?

No response

Known Workarounds

No response

Configuration

net5.0 , <PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="6.0.0" />

Other information

No response

Author: jvmlet
Assignees: -
Labels:

area-Extensions-Configuration

Milestone: -

@tarekgh
Copy link
Member

tarekgh commented Aug 15, 2022

Looks there are some places in the ConfigurationBinder.cs which we swallow the exception. We may consider using BinderOptions.ErrorOnUnknownConfiguration in such places and allow throwing there.




We may consider the fix in the future releases.

@tarekgh tarekgh added this to the Future milestone Aug 15, 2022
@ghost ghost removed the untriaged New issue has not been triaged by the area owner label Aug 15, 2022
@jvmlet
Copy link
Author

jvmlet commented Aug 16, 2022

Is there NEAREST Future milestone ?
You ship the application documenting all the configuration options and have no chance to validate them at runtime.
No observability at all
Sounds like a blocker issue.
BTW, don't you have static code analysis as build-pipeline step ? Sonar has no issue to catch this violation
image

@ericstj ericstj added untriaged New issue has not been triaged by the area owner and removed untriaged New issue has not been triaged by the area owner labels Sep 8, 2022
@fnajera-rac-de
Copy link

I just wasted 4 hours debugging until I arrived at this issue. I agree with @jvmlet that this should be a blocker and at the very minimum a binding option should be provided to fail on children error.

@jvmlet
Copy link
Author

jvmlet commented Oct 5, 2022

@tarekgh , would you please prioritize it ?

@tarekgh tarekgh modified the milestones: Future, 8.0.0 Oct 5, 2022
@tarekgh
Copy link
Member

tarekgh commented Oct 5, 2022

@jvmlet I marked with the next release milestone. Are you interested in submitting a PR for it?

@SteveDunn
Copy link
Contributor

SteveDunn commented Oct 31, 2022

I'm happy to look at this as I vaguely remember adding ErrorOnUnknownConfiguration. I'm guessing there's no real urgency for this as time is a bit short at the moment.

@SteveDunn
Copy link
Contributor

The comments for ErrorOnUnknownConfiguration say:

/// <summary>
/// When false (the default), no exceptions are thrown when a configuration key is found for which the
/// provided model object does not have an appropriate property which matches the key's name.
/// When true, an <see cref="System.InvalidOperationException"/> is thrown with a description
/// of the missing properties.
/// </summary>

So, technically, in this case, the key exists, and the config exists, so 🙈

Of course, that config option implies that an error should be thrown, not just on missing properties in config, but also invalid properties in config.

By making ErrorOnUnknownConfiguration dual-purpose might now introduce unexpected breaking changes.

Maybe we need another property named ErrorOnInvalidConfiguration.

What do you think? Would this require another API Proposal?

@tarekgh
Copy link
Member

tarekgh commented Nov 1, 2022

I don't think we need to complicate the options by adding one more property like that. We can use ErrorOnUnknownConfiguration and clarify the documentation. Yes, it will be a breaking change, but I don't think anyone using ErrorOnUnknownConfiguration was not trying to catch all thrown exception when loading the configuration. Let me know if you think otherwise.

@tarekgh
Copy link
Member

tarekgh commented Nov 8, 2022

This is already done by the PR #77708

@tarekgh tarekgh closed this as completed Nov 8, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Dec 8, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-Extensions-Configuration breaking-change Issue or PR that represents a breaking API or functional change over a prerelease.
Projects
None yet
Development

No branches or pull requests

5 participants