Skip to content

Configuration Binding a dictionary with a key that has empty value skips the key as well #66229

Closed
@vidhyapg

Description

@vidhyapg

I am using Configuration Binding to read a config file. Empty dictionary value leads to not generating an entry for key as well.

Here is where I bind the configuration to a type defined in my project DistributedQueueConfig

                        var opts = new DistributedQueueConfig();
                        var config = hostContext.Configuration.GetSection("queueConfig");
                        // bind manually
                        config.Bind(opts);

Attached the json file used.

This is examining opts after Bind.

opts.Namespaces[0].Queues.Count
2
opts.Namespaces[1].Queues.Count
1
opts.Namespaces[1].Queues
Count = 1
    [0]: {[q3, {CognitiveServices.Batch.Common.DistributedQueueConfiguration.Models.QueueProperties}]}

q4 which has empty value does have an entry in the dictionary which is not expected. When I used JsonSerializer.Deserialize(), I can see q4 in the object. It is valid for a queue to not have any queueproperties fields set, but still should show up in the list of queues. I tried both BinderOptions which has no effect. This seems to be a bug.

    /// <summary>
    /// Top level of the config json
    /// </summary>
    public class DistributedQueueConfig
    {
        /// <summary>
        /// Contains list of namespaces.
        /// </summary>
        [JsonPropertyName("namespaces")]
        public IList<QueueNamespaces> Namespaces { get; set; }
    }

    public class QueueNamespaces
    {
        /// <summary>
        /// Namespace value
        /// </summary>
        [JsonPropertyName("namespace")]
        public string Namespace { get; set; }

        /// <summary>
        /// Map of queue name and its properties.
        /// </summary>
        [JsonPropertyName("queues")]
        public Dictionary<string, QueueProperties> Queues { get; set; } = new ();
    }

    public class QueueProperties
    {
        /// <summary>
        /// The date when the queue is marked to be dequeueOnly.
        /// Used to determine if queue is AvailableForEnqueueing.
        /// </summary>
        [JsonPropertyName("creationDate")]
        public DateTimeOffset? CreationDate { get; set; }

        /// <summary>
        /// The date when the queue is marked to be dequeueOnly.
        /// Used to determine if queue is AvailableForEnqueueing.
        /// </summary>
        [JsonPropertyName("dequeueOnlyMarkedDate")]
        public DateTimeOffset? DequeueOnlyMarkedDate { get; set; } = default(DateTimeOffset);
    }

queue_config.json

{
	"queueConfig": {
		"namespaces": [
			{
				"namespace": "devnortheurope",
				"queues": {
					"q1": {
						"dequeueOnlyMarkedDate": "2022-01-20T12:49:03.395150-08:00"
					},
					"q2": {
						"dequeueOnlyMarkedDate": "2022-01-20T12:49:03.395150-08:00"
					}
				}
			},
			{
				"namespace": "devnortheurope2",
				"queues": {
					"q3": {
						"dequeueOnlyMarkedDate": "2022-01-20T12:49:03.395150-08:00"
					},
					"q4": {
					}
				}
			}
		]
	}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions