Skip to content

Commit 254b5c6

Browse files
authored
Accept null tags/meta in Consul responses (#1631)
1 parent accee88 commit 254b5c6

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

src/Discovery/src/Consul/ConsulServiceInstance.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ namespace Steeltoe.Discovery.Consul;
1313
/// </summary>
1414
internal sealed class ConsulServiceInstance : IServiceInstance
1515
{
16+
private static readonly IReadOnlyList<string> EmptyStringList = Array.Empty<string>();
17+
private static readonly IReadOnlyDictionary<string, string?> EmptyStringDictionary = new Dictionary<string, string?>().AsReadOnly();
18+
1619
/// <inheritdoc />
1720
public string ServiceId { get; }
1821

@@ -53,9 +56,9 @@ internal ConsulServiceInstance(ServiceEntry serviceEntry)
5356
ArgumentNullException.ThrowIfNull(serviceEntry);
5457

5558
Host = ConsulServerUtils.FindHost(serviceEntry);
56-
Tags = serviceEntry.Service.Tags;
57-
Metadata = serviceEntry.Service.Meta.AsReadOnly();
58-
IsSecure = serviceEntry.Service.Meta != null && serviceEntry.Service.Meta.TryGetValue("secure", out string? secureString) && bool.Parse(secureString);
59+
Tags = serviceEntry.Service.Tags ?? EmptyStringList;
60+
Metadata = serviceEntry.Service.Meta?.AsReadOnly() ?? EmptyStringDictionary;
61+
IsSecure = Metadata.TryGetValue("secure", out string? secureString) && secureString != null && bool.Parse(secureString);
5962
ServiceId = serviceEntry.Service.Service;
6063
InstanceId = serviceEntry.Service.ID;
6164
Port = serviceEntry.Service.Port;

src/Discovery/test/Consul.Test/Discovery/ConsulServiceInstanceTest.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,24 @@ public void Constructor_Initializes()
4949
serviceInstance.NonSecureUri.Should().BeNull();
5050
serviceInstance.SecureUri.Should().Be(serviceInstance.Uri);
5151
}
52+
53+
[Fact]
54+
public void Constructor_accepts_null_tags_and_meta()
55+
{
56+
var healthService = new ServiceEntry
57+
{
58+
Service = new AgentService
59+
{
60+
Service = "ServiceId",
61+
ID = "Instance1",
62+
Address = "foo.bar.com",
63+
Port = 1234
64+
}
65+
};
66+
67+
var serviceInstance = new ConsulServiceInstance(healthService);
68+
69+
serviceInstance.Tags.Should().BeEmpty();
70+
serviceInstance.Metadata.Should().BeEmpty();
71+
}
5272
}

0 commit comments

Comments
 (0)