Skip to content

Commit 5838d75

Browse files
committed
fix: Don't create a static field in a generic class
1 parent d6cd3dc commit 5838d75

File tree

4 files changed

+84
-35
lines changed

4 files changed

+84
-35
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace DotNet.Testcontainers.Configurations
2+
{
3+
using System.Text.Json;
4+
5+
public static class DefaultJsonSerializerOptions
6+
{
7+
static DefaultJsonSerializerOptions()
8+
{
9+
Instance.Converters.Add(new JsonOrderedKeysConverter());
10+
}
11+
12+
public static JsonSerializerOptions Instance { get; } = new JsonSerializerOptions();
13+
}
14+
}

src/Testcontainers/Configurations/Commons/JsonIgnoreRuntimeResourceLabels.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,16 @@ namespace DotNet.Testcontainers.Configurations
88

99
internal sealed class JsonIgnoreRuntimeResourceLabels : JsonOrderedKeysConverter
1010
{
11-
private static readonly ISet<string> IgnoreLabels = new HashSet<string> { ResourceReaper.ResourceReaperSessionLabel, TestcontainersClient.TestcontainersVersionLabel, TestcontainersClient.TestcontainersSessionIdLabel };
11+
private static readonly ISet<string> IgnoreLabels = new HashSet<string>
12+
{
13+
ResourceReaper.ResourceReaperSessionLabel,
14+
TestcontainersClient.TestcontainersVersionLabel,
15+
TestcontainersClient.TestcontainersSessionIdLabel,
16+
};
1217

1318
public override void Write(Utf8JsonWriter writer, IReadOnlyDictionary<string, string> value, JsonSerializerOptions options)
1419
{
1520
var labels = value.Where(label => !IgnoreLabels.Contains(label.Key)).ToDictionary(label => label.Key, label => label.Value);
16-
1721
base.Write(writer, labels, options);
1822
}
1923
}

src/Testcontainers/Configurations/Commons/ResourceConfiguration.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,6 @@ namespace DotNet.Testcontainers.Configurations
1414
[PublicAPI]
1515
public class ResourceConfiguration<TCreateResourceEntity> : IResourceConfiguration<TCreateResourceEntity>
1616
{
17-
private static readonly JsonSerializerOptions JsonSerializerOptions;
18-
19-
static ResourceConfiguration()
20-
{
21-
JsonSerializerOptions = new JsonSerializerOptions { Converters = { new JsonOrderedKeysConverter() } };
22-
}
23-
2417
/// <summary>
2518
/// Initializes a new instance of the <see cref="ResourceConfiguration{TCreateResourceEntity}" /> class.
2619
/// </summary>
@@ -95,7 +88,7 @@ protected ResourceConfiguration(IResourceConfiguration<TCreateResourceEntity> ol
9588
/// <inheritdoc />
9689
public virtual string GetReuseHash()
9790
{
98-
var jsonUtf8Bytes = JsonSerializer.SerializeToUtf8Bytes(this, GetType(), JsonSerializerOptions);
91+
var jsonUtf8Bytes = JsonSerializer.SerializeToUtf8Bytes(this, GetType(), DefaultJsonSerializerOptions.Instance);
9992

10093
#if NET6_0_OR_GREATER
10194
return Convert.ToBase64String(SHA1.HashData(jsonUtf8Bytes));

tests/Testcontainers.Platform.Linux.Tests/ReusableResourceTest.cs

Lines changed: 63 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -104,37 +104,75 @@ public sealed class EqualTest
104104
{
105105
[Fact]
106106
[Trait(nameof(DockerCli.DockerPlatform), nameof(DockerCli.DockerPlatform.Linux))]
107-
public void ForSameConfigurationCreatedInDifferentOrder()
107+
public void ForKnownConfiguration()
108108
{
109-
var env1 = new Dictionary<string, string>
110-
{
111-
["keyA"] = "valueA",
112-
["keyB"] = "valueB",
113-
};
114-
var env2 = new Dictionary<string, string>
115-
{
116-
["keyB"] = "valueB",
117-
["keyA"] = "valueA",
118-
};
119-
var hash1 = new ReuseHashContainerBuilder().WithEnvironment(env1).WithLabel("labelA", "A").WithLabel("labelB", "B").GetReuseHash();
120-
var hash2 = new ReuseHashContainerBuilder().WithEnvironment(env2).WithLabel("labelB", "B").WithLabel("labelA", "A").GetReuseHash();
121-
Assert.Equal(hash1, hash2);
109+
// Given
110+
var env = new Dictionary<string, string>();
111+
env["keyA"] = "valueA";
112+
env["keyB"] = "valueB";
113+
114+
// When
115+
var hash = new ReuseHashContainerBuilder()
116+
.WithEnvironment(env)
117+
.WithLabel("labelA", "A")
118+
.WithLabel("labelB", "B")
119+
.GetReuseHash();
120+
121+
// Then
122+
123+
// `50MEP+vnxEkQFo5PrndJ7oKOfh8=` is the Base64-encoded SHA-1 hash of this JSON:
124+
//
125+
// {
126+
// "Image": null,
127+
// "Name": null,
128+
// "Entrypoint": null,
129+
// "Command": [],
130+
// "Environments": {
131+
// "keyA": "valueA",
132+
// "keyB": "valueB"
133+
// },
134+
// "ExposedPorts": {},
135+
// "PortBindings": {},
136+
// "NetworkAliases": [],
137+
// "ExtraHosts": [],
138+
// "Labels": {
139+
// "labelA": "A",
140+
// "labelB": "B",
141+
// "org.testcontainers": "true",
142+
// "org.testcontainers.lang": "dotnet"
143+
// }
144+
// }
145+
Assert.Equal("50MEP+vnxEkQFo5PrndJ7oKOfh8=", hash);
122146
}
123147

124148
[Fact]
125149
[Trait(nameof(DockerCli.DockerPlatform), nameof(DockerCli.DockerPlatform.Linux))]
126-
public void ForGivenConfiguration()
150+
public void ForSameConfigurationInDifferentOrder()
127151
{
128-
var env = new Dictionary<string, string>
129-
{
130-
["keyB"] = "valueB",
131-
["keyA"] = "valueA",
132-
};
133-
var hash = new ReuseHashContainerBuilder().WithEnvironment(env).WithLabel("labelB", "B").WithLabel("labelA", "A").GetReuseHash();
134-
135-
// 50MEP+vnxEkQFo5PrndJ7oKOfh8= is the base64 encoded SHA1 of this JSON:
136-
// {"Image":null,"Name":null,"Entrypoint":null,"Command":[],"Environments":{"keyA":"valueA","keyB":"valueB"},"ExposedPorts":{},"PortBindings":{},"NetworkAliases":[],"ExtraHosts":[],"Labels":{"labelA":"A","labelB":"B","org.testcontainers":"true","org.testcontainers.lang":"dotnet"}}
137-
Assert.Equal("50MEP+vnxEkQFo5PrndJ7oKOfh8=", hash);
152+
// Given
153+
var env1 = new Dictionary<string, string>();
154+
env1["keyA"] = "valueA";
155+
env1["keyB"] = "valueB";
156+
157+
var env2 = new Dictionary<string, string>();
158+
env2["keyB"] = "valueB";
159+
env2["keyA"] = "valueA";
160+
161+
// When
162+
var hash1 = new ReuseHashContainerBuilder()
163+
.WithEnvironment(env1)
164+
.WithLabel("labelA", "A")
165+
.WithLabel("labelB", "B")
166+
.GetReuseHash();
167+
168+
var hash2 = new ReuseHashContainerBuilder()
169+
.WithEnvironment(env2)
170+
.WithLabel("labelB", "B")
171+
.WithLabel("labelA", "A")
172+
.GetReuseHash();
173+
174+
// Then
175+
Assert.Equal(hash1, hash2);
138176
}
139177
}
140178

0 commit comments

Comments
 (0)