Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for vendor-specific topic configuration #137

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/LEGO.AsyncAPI.Bindings/Kafka/KafkaChannelBinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ namespace LEGO.AsyncAPI.Bindings.Kafka
{
using System;
using LEGO.AsyncAPI.Models;
using LEGO.AsyncAPI.Models.Bindings.Kafka;
using LEGO.AsyncAPI.Readers.ParseNodes;
using LEGO.AsyncAPI.Writers;

Expand Down Expand Up @@ -47,10 +46,11 @@ public class KafkaChannelBinding : ChannelBinding<KafkaChannelBinding>
private static FixedFieldMap<TopicConfigurationObject> kafkaChannelTopicConfigurationObjectFixedFields = new ()
{
{ "cleanup.policy", (a, n) => { a.CleanupPolicy = n.CreateSimpleList(s => s.GetScalarValue()); } },
{ "retention.ms", (a, n) => { a.RetentionMiliseconds = n.GetIntegerValue(); } },
{ "retention.ms", (a, n) => { a.RetentionMilliseconds = n.GetIntegerValue(); } },
{ "retention.bytes", (a, n) => { a.RetentionBytes = n.GetIntegerValue(); } },
{ "delete.retention.ms", (a, n) => { a.DeleteRetentionMiliseconds = n.GetIntegerValue(); } },
{ "delete.retention.ms", (a, n) => { a.DeleteRetentionMilliseconds = n.GetIntegerValue(); } },
{ "max.message.bytes", (a, n) => { a.MaxMessageBytes = n.GetIntegerValue(); } },
{ "custom.configs", (a, n) => { a.CustomConfigs = n.CreateSimpleMap(s => s.GetScalarValue()); } },
};

/// <summary>
Expand Down
26 changes: 17 additions & 9 deletions src/LEGO.AsyncAPI.Bindings/Kafka/TopicConfigurationObject.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// Copyright (c) The LEGO Group. All rights reserved.

namespace LEGO.AsyncAPI.Models.Bindings.Kafka
using System;
using System.Collections.Generic;
using LEGO.AsyncAPI.Models;
using LEGO.AsyncAPI.Models.Interfaces;
using LEGO.AsyncAPI.Writers;

namespace LEGO.AsyncAPI.Bindings.Kafka
{
using System;
using System.Collections.Generic;
using LEGO.AsyncAPI.Models.Interfaces;
using LEGO.AsyncAPI.Writers;

public class TopicConfigurationObject : IAsyncApiElement
{
Expand All @@ -17,7 +19,7 @@ public class TopicConfigurationObject : IAsyncApiElement
/// <summary>
/// The retention.ms configuration option.
/// </summary>
public int? RetentionMiliseconds { get; set; }
public int? RetentionMilliseconds { get; set; }

/// <summary>
/// The retention.bytes configuration option.
Expand All @@ -27,12 +29,17 @@ public class TopicConfigurationObject : IAsyncApiElement
/// <summary>
/// The delete.retention.ms configuration option.
/// </summary>
public int? DeleteRetentionMiliseconds { get; set; }
public int? DeleteRetentionMilliseconds { get; set; }

/// <summary>
/// The max.message.bytes configuration option.
/// </summary>
public int? MaxMessageBytes { get; set; }

/// <summary>
/// The custom.configs properties configuration option.
/// </summary>
public Dictionary<string, string>? CustomConfigs { get; set; }

public void Serialize(IAsyncApiWriter writer)
{
Expand All @@ -43,10 +50,11 @@ public void Serialize(IAsyncApiWriter writer)

writer.WriteStartObject();
writer.WriteOptionalCollection(AsyncApiConstants.CleanupPolicy, this.CleanupPolicy, (w, s) => w.WriteValue(s));
writer.WriteOptionalProperty<int>(AsyncApiConstants.RetentionMiliseconds, this.RetentionMiliseconds);
writer.WriteOptionalProperty<int>(AsyncApiConstants.RetentionMilliseconds, this.RetentionMilliseconds);
writer.WriteOptionalProperty<int>(AsyncApiConstants.RetentionBytes, this.RetentionBytes);
writer.WriteOptionalProperty<int>(AsyncApiConstants.DeleteRetentionMiliseconds, this.DeleteRetentionMiliseconds);
writer.WriteOptionalProperty<int>(AsyncApiConstants.DeleteRetentionMilliseconds, this.DeleteRetentionMilliseconds);
writer.WriteOptionalProperty<int>(AsyncApiConstants.MaxMessageBytes, this.MaxMessageBytes);
writer.WriteOptionalMap(AsyncApiConstants.CustomConfigs, this.CustomConfigs, (w, t) => w.WriteValue(t));
writer.WriteEndObject();
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/LEGO.AsyncAPI/Models/AsyncApiConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,11 @@ public static class AsyncApiConstants
public const string ServerVariables = "serverVariables";
public const string MessageId = "messageId";
public const string CleanupPolicy = "cleanup.policy";
public const string RetentionMiliseconds = "retention.ms";
public const string RetentionMilliseconds = "retention.ms";
public const string RetentionBytes = "retention.bytes";
public const string DeleteRetentionMiliseconds = "delete.retention.ms";
public const string DeleteRetentionMilliseconds = "delete.retention.ms";
public const string MaxMessageBytes = "max.message.bytes";
public const string CustomConfigs = "custom.configs";
public const string TopicConfiguration = "topicConfiguration";
public const string GeoReplication = "geo-replication";
public const string AdditionalItems = "additionalItems";
Expand Down
18 changes: 13 additions & 5 deletions test/LEGO.AsyncAPI.Tests/Bindings/Kafka/KafkaBindings_Should.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@

namespace LEGO.AsyncAPI.Tests.Bindings.Kafka
{
using System.Collections.Generic;
using FluentAssertions;
using LEGO.AsyncAPI.Bindings;
using LEGO.AsyncAPI.Bindings.Kafka;
using LEGO.AsyncAPI.Models;
using LEGO.AsyncAPI.Models.Bindings.Kafka;
using LEGO.AsyncAPI.Readers;
using NUnit.Framework;
using System.Collections.Generic;

internal class KafkaBindings_Should
{
Expand All @@ -30,7 +29,10 @@ public void KafkaChannelBinding_WithFilledObject_SerializesAndDeserializes()
retention.ms: 1
retention.bytes: 2
delete.retention.ms: 3
max.message.bytes: 4";
max.message.bytes: 4
custom.configs:
key.schema.validation: 'true'
key.subject.name.strategy: TopicNameStrategy";

var channel = new AsyncApiChannel();
channel.Bindings.Add(new KafkaChannelBinding
Expand All @@ -41,11 +43,17 @@ public void KafkaChannelBinding_WithFilledObject_SerializesAndDeserializes()
TopicConfiguration = new TopicConfigurationObject()
{
CleanupPolicy = new List<string> { "delete", "compact" },
RetentionMiliseconds = 1,
RetentionMilliseconds = 1,
RetentionBytes = 2,
DeleteRetentionMiliseconds = 3,
DeleteRetentionMilliseconds = 3,
MaxMessageBytes = 4,
CustomConfigs = new Dictionary<string, string>
{
{ "key.schema.validation", "true" },
{ "key.subject.name.strategy", "TopicNameStrategy" }
},
},

});

// Act
Expand Down