Skip to content

Commit c9be1cf

Browse files
authored
JetStream config JSON fixed (#394)
* JetStream config JSON fixed * Placement JSON test * removed unused ifdef * format
1 parent 9cf6b4a commit c9be1cf

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

src/NATS.Client.JetStream/Models/Placement.cs

+2-9
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,8 @@ public record Placement
1010
/// The desired cluster name to place the stream
1111
/// </summary>
1212
[System.Text.Json.Serialization.JsonPropertyName("cluster")]
13-
[System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.Never)]
14-
[System.ComponentModel.DataAnnotations.Required(AllowEmptyStrings = true)]
15-
#if NET6_0
16-
public string Cluster { get; set; } = default!;
17-
#else
18-
#pragma warning disable SA1206
19-
public required string Cluster { get; set; }
20-
#pragma warning restore SA1206
21-
#endif
13+
[System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull)]
14+
public string? Cluster { get; set; }
2215

2316
/// <summary>
2417
/// Tags required on servers hosting this stream
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System.Buffers;
2+
using System.Text;
3+
using NATS.Client.JetStream.Internal;
4+
using NATS.Client.JetStream.Models;
5+
6+
namespace NATS.Client.JetStream.Tests;
7+
8+
public class ParseJsonTests
9+
{
10+
[Fact]
11+
public void Placement_properties_should_be_optional()
12+
{
13+
// This is necessary because when a KV bucket created using nats client, the placement is not set
14+
// and the server will return an empty object for placement.
15+
var serializer = NatsJSJsonSerializer<Placement>.Default;
16+
17+
var bw = new NatsBufferWriter<byte>();
18+
serializer.Serialize(bw, new Placement());
19+
20+
var json = Encoding.UTF8.GetString(bw.WrittenSpan);
21+
Assert.Equal("{}", json);
22+
23+
var result = serializer.Deserialize(new ReadOnlySequence<byte>(bw.WrittenMemory));
24+
Assert.NotNull(result);
25+
Assert.Null(result.Cluster);
26+
Assert.Null(result.Tags);
27+
}
28+
}

0 commit comments

Comments
 (0)