Skip to content

Commit 4113d36

Browse files
committed
Fix tests
1 parent fe92af9 commit 4113d36

File tree

11 files changed

+215
-21
lines changed

11 files changed

+215
-21
lines changed

foreign/csharp/Iggy_SDK/Contracts/MessageResponse.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public sealed class MessageResponse
4040
/// <summary>
4141
/// Headers defined by the user.
4242
/// </summary>
43+
[JsonPropertyName("user_headers")]
4344
[JsonConverter(typeof(UserHeadersConverter))]
4445
public Dictionary<HeaderKey, HeaderValue>? UserHeaders { get; init; }
4546
}

foreign/csharp/Iggy_SDK/Contracts/Tcp/TcpContracts.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -554,12 +554,12 @@ private static byte[] GetHeadersBytes(Dictionary<HeaderKey, HeaderValue>? header
554554
return [];
555555
}
556556

557-
var headersLength = headers.Sum(header => 1 + 4 + header.Key.Value.Length + 1 + 4 + header.Value.Value.Length);
557+
var headersLength = headers.Sum(kvp => 1 + 4 + kvp.Key.Value.Length + 1 + 4 + kvp.Value.Value.Length);
558558
Span<byte> headersBytes = stackalloc byte[headersLength];
559559
var position = 0;
560-
foreach (var (headerKey, headerValue) in headers)
560+
foreach (var kvp in headers)
561561
{
562-
var headerBytes = GetBytesFromHeader(headerKey, headerValue);
562+
var headerBytes = GetBytesFromHeader(kvp.Key, kvp.Value);
563563
headerBytes.CopyTo(headersBytes[position..(position + headerBytes.Length)]);
564564
position += headerBytes.Length;
565565
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
namespace Apache.Iggy.Headers;
19+
20+
/// <summary>
21+
/// Represents a single header entry with key and value.
22+
/// </summary>
23+
/// <param name="Key">The header key.</param>
24+
/// <param name="Value">The header value.</param>
25+
public readonly record struct HeaderEntry(HeaderKey Key, HeaderValue Value);

foreign/csharp/Iggy_SDK/IggyClient/Implementations/HttpMessageStream.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,11 @@ internal HttpMessageStream(HttpClient httpClient)
5555
_jsonSerializerOptions = new JsonSerializerOptions
5656
{
5757
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower,
58-
Converters = { new JsonStringEnumConverter(JsonNamingPolicy.SnakeCaseLower) }
58+
Converters =
59+
{
60+
new JsonStringEnumConverter(JsonNamingPolicy.SnakeCaseLower),
61+
new JsonConverters.UserHeadersConverter()
62+
}
5963
};
6064
}
6165

foreign/csharp/Iggy_SDK/JsonConverters/UserHeadersConverter.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ internal class UserHeadersConverter : JsonConverter<Dictionary<HeaderKey, Header
3232

3333
if (reader.TokenType != JsonTokenType.StartArray)
3434
{
35-
throw new JsonException("Expected start of array for headers.");
35+
throw new JsonException($"Expected start of array for headers but got {reader.TokenType}.");
3636
}
3737

3838
var result = new Dictionary<HeaderKey, HeaderValue>();
@@ -41,7 +41,7 @@ internal class UserHeadersConverter : JsonConverter<Dictionary<HeaderKey, Header
4141
{
4242
if (reader.TokenType == JsonTokenType.EndArray)
4343
{
44-
break;
44+
return result.Count == 0 ? null : result;
4545
}
4646

4747
if (reader.TokenType != JsonTokenType.StartObject)
@@ -70,7 +70,7 @@ internal class UserHeadersConverter : JsonConverter<Dictionary<HeaderKey, Header
7070
switch (propertyName)
7171
{
7272
case "key":
73-
key = ReadHeaderField(ref reader);
73+
key = ReadHeaderKey(ref reader);
7474
break;
7575
case "value":
7676
value = ReadHeaderValue(ref reader);
@@ -89,7 +89,7 @@ internal class UserHeadersConverter : JsonConverter<Dictionary<HeaderKey, Header
8989
return result;
9090
}
9191

92-
private static HeaderKey ReadHeaderField(ref Utf8JsonReader reader)
92+
private static HeaderKey ReadHeaderKey(ref Utf8JsonReader reader)
9393
{
9494
if (reader.TokenType != JsonTokenType.StartObject)
9595
{
@@ -212,18 +212,18 @@ public override void Write(Utf8JsonWriter writer, Dictionary<HeaderKey, HeaderVa
212212

213213
writer.WriteStartArray();
214214

215-
foreach (var (headerKey, headerValue) in value)
215+
foreach (var kvp in value)
216216
{
217217
writer.WriteStartObject();
218218

219219
writer.WriteStartObject("key");
220-
writer.WriteString("kind", GetHeaderKindString(headerKey.Kind));
221-
writer.WriteBase64String("value", headerKey.Value);
220+
writer.WriteString("kind", GetHeaderKindString(kvp.Key.Kind));
221+
writer.WriteBase64String("value", kvp.Key.Value);
222222
writer.WriteEndObject();
223223

224224
writer.WriteStartObject("value");
225-
writer.WriteString("kind", GetHeaderKindString(headerValue.Kind));
226-
writer.WriteBase64String("value", headerValue.Value);
225+
writer.WriteString("kind", GetHeaderKindString(kvp.Value.Kind));
226+
writer.WriteBase64String("value", kvp.Value.Value);
227227
writer.WriteEndObject();
228228

229229
writer.WriteEndObject();

foreign/csharp/Iggy_SDK/Mappers/BinaryMapper.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -454,11 +454,8 @@ private static Dictionary<HeaderKey, HeaderValue> MapHeaders(ReadOnlySpan<byte>
454454
ReadOnlySpan<byte> value = payload[position..(position + valueLength)];
455455
position += valueLength;
456456

457-
headers.Add(new HeaderKey { Kind = keyKind, Value = keyValue }, new HeaderValue
458-
{
459-
Kind = valueKind,
460-
Value = value.ToArray()
461-
});
457+
headers[new HeaderKey { Kind = keyKind, Value = keyValue }] =
458+
new HeaderValue { Kind = valueKind, Value = value.ToArray() };
462459
}
463460

464461
return headers;

foreign/java/java-sdk/src/main/java/org/apache/iggy/message/Message.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
import com.fasterxml.jackson.annotation.JsonProperty;
2424
import com.fasterxml.jackson.annotation.JsonSetter;
2525
import com.fasterxml.jackson.annotation.Nulls;
26+
import org.apache.iggy.serde.Base64Serializer;
27+
import org.apache.iggy.serde.UserHeadersSerializer;
28+
import tools.jackson.databind.annotation.JsonSerialize;
2629

2730
import java.math.BigInteger;
2831
import java.util.Base64;
@@ -31,7 +34,10 @@
3134
import java.util.List;
3235
import java.util.Map;
3336

34-
public record Message(MessageHeader header, byte[] payload, Map<HeaderKey, HeaderValue> userHeaders) {
37+
public record Message(
38+
MessageHeader header,
39+
@JsonSerialize(using = Base64Serializer.class) byte[] payload,
40+
@JsonSerialize(using = UserHeadersSerializer.class) Map<HeaderKey, HeaderValue> userHeaders) {
3541
@JsonCreator
3642
public static Message fromJson(
3743
@JsonProperty("header") MessageHeader header,

foreign/java/java-sdk/src/main/java/org/apache/iggy/message/Partitioning.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,14 @@
2020
package org.apache.iggy.message;
2121

2222
import org.apache.commons.lang3.ArrayUtils;
23+
import org.apache.iggy.serde.Base64Serializer;
24+
import tools.jackson.databind.annotation.JsonSerialize;
2325

2426
import java.nio.ByteBuffer;
2527

26-
public record Partitioning(PartitioningKind kind, byte[] value) {
27-
28+
public record Partitioning(
29+
PartitioningKind kind,
30+
@JsonSerialize(using = Base64Serializer.class) byte[] value) {
2831
public static Partitioning balanced() {
2932
return new Partitioning(PartitioningKind.Balanced, new byte[] {});
3033
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.apache.iggy.serde;
21+
22+
import tools.jackson.core.JacksonException;
23+
import tools.jackson.core.JsonGenerator;
24+
import tools.jackson.databind.SerializationContext;
25+
import tools.jackson.databind.ValueSerializer;
26+
27+
import java.util.Base64;
28+
29+
public class Base64Serializer extends ValueSerializer<byte[]> {
30+
31+
@Override
32+
public void serialize(byte[] value, JsonGenerator gen, SerializationContext ctxt) throws JacksonException {
33+
gen.writeString(Base64.getEncoder().encodeToString(value));
34+
}
35+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.apache.iggy.serde;
21+
22+
import org.apache.iggy.message.HeaderEntry;
23+
import org.apache.iggy.message.HeaderKey;
24+
import org.apache.iggy.message.HeaderValue;
25+
import tools.jackson.core.JacksonException;
26+
import tools.jackson.core.JsonGenerator;
27+
import tools.jackson.databind.SerializationContext;
28+
import tools.jackson.databind.ValueSerializer;
29+
30+
import java.util.Map;
31+
32+
public class UserHeadersSerializer extends ValueSerializer<Map<HeaderKey, HeaderValue>> {
33+
34+
@Override
35+
public void serialize(Map<HeaderKey, HeaderValue> headers, JsonGenerator gen, SerializationContext ctxt)
36+
throws JacksonException {
37+
gen.writeStartArray();
38+
for (Map.Entry<HeaderKey, HeaderValue> entry : headers.entrySet()) {
39+
ctxt.findValueSerializer(HeaderEntry.class)
40+
.serialize(new HeaderEntry(entry.getKey(), entry.getValue()), gen, ctxt);
41+
}
42+
gen.writeEndArray();
43+
}
44+
}

0 commit comments

Comments
 (0)