Add support for repeated XML elements without a name attribute (#44608)
authorAlexander Moerman <alexander.moerman@gmail.com>
Wed, 10 Mar 2021 19:55:04 +0000 (20:55 +0100)
committerGitHub <noreply@github.com>
Wed, 10 Mar 2021 19:55:04 +0000 (11:55 -0800)
commite0af8cf40a60e018ab6c359a9eff1d5b09cbcdd9
tree80e1bdf70b2105455df4b94b48bb70fea718b50a
parent3973fc6b785a6adb24453b88681bde2ac83f370c
Add support for repeated XML elements without a name attribute (#44608)

* Add support for repeated XML elements without a name attribute

This commit adds support in Microsoft.Extensions.Configuration.Xml for repeated XML elements without requiring a Name attribute.
This solves a particularly subtle bug when configuring Serilog from an XML configuration source. For a full description, see #36541

The original implementation of the XmlStreamConfigurationProvider has been modified to do the following:

- Maintain a stack of encountered XML elements while traversing the XML source. This is needed to detect siblings.
- When siblings are detected, automatically append an index to the generated configuration keys. This makes it work exactly the same as the JSON configuration provider with JSON arrays.

Tests are updated to reflect the new behavior:
- Tests that verified an exception occurs when entering duplicate keys have been removed. Duplicate keys are supported now.
- Add tests that verify duplicate keys result in the correct configuration keys, with all the lower/upper case variants and with support for the special "Name" attribute handling.

Co-authored-by: Santiago Fernandez Madero <safern@microsoft.com>
src/libraries/Microsoft.Extensions.Configuration.Xml/src/XmlConfigurationElement.cs [new file with mode: 0644]
src/libraries/Microsoft.Extensions.Configuration.Xml/src/XmlConfigurationElementAttributeValue.cs [new file with mode: 0644]
src/libraries/Microsoft.Extensions.Configuration.Xml/src/XmlConfigurationElementTextContent.cs [new file with mode: 0644]
src/libraries/Microsoft.Extensions.Configuration.Xml/src/XmlStreamConfigurationProvider.cs
src/libraries/Microsoft.Extensions.Configuration.Xml/tests/ConfigurationProviderXmlTest.cs
src/libraries/Microsoft.Extensions.Configuration.Xml/tests/XmlConfigurationTest.cs