Skip to content

Configuration binding swallows the conversion exception #73915

Closed

Description

@jvmlet

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

(

)

Activity

  1. ghost added
    untriagedNew issue has not been triaged by the area owner
    on Aug 14, 2022
  2. tarekgh commented on Aug 15, 2022

    @tarekgh
    Member

    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.

  3. added this to the Future milestone on Aug 15, 2022
  4. ghost removed
    untriagedNew issue has not been triaged by the area owner
    on Aug 15, 2022
  5. jvmlet commented on Aug 16, 2022

    @jvmlet
    Author

    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

  6. added
    untriagedNew issue has not been triaged by the area owner
    and removed
    untriagedNew issue has not been triaged by the area owner
    on Sep 8, 2022
  7. fnajera-rac-de commented on Oct 5, 2022

    @fnajera-rac-de

    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.

  8. jvmlet commented on Oct 5, 2022

    @jvmlet
    Author

    @tarekgh , would you please prioritize it ?

  9. modified the milestones: Future, 8.0.0 on Oct 5, 2022
  10. tarekgh commented on Oct 5, 2022

    @tarekgh
    Member

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

  11. SteveDunn commented on Oct 31, 2022

    @SteveDunn
    Contributor

    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.

  12. SteveDunn commented on Oct 31, 2022

    @SteveDunn
    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?

  13. tarekgh commented on Nov 1, 2022

    @tarekgh
    Member

    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.

  14. added
    breaking-changeIssue or PR that represents a breaking API or functional change over a previous release.
    on Nov 1, 2022
  15. tarekgh commented on Nov 8, 2022

    @tarekgh
    Member

    This is already done by the PR #77708

  16. ghost locked as resolved and limited conversation to collaborators on Dec 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-Extensions-Configurationbreaking-changeIssue or PR that represents a breaking API or functional change over a previous release.

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions