Skip to content

Commit 24cf1a9

Browse files
authored
fix: empty channels should be allowed (#208)
1 parent e6b9bf4 commit 24cf1a9

File tree

4 files changed

+17
-4
lines changed

4 files changed

+17
-4
lines changed

src/LEGO.AsyncAPI/Models/AsyncApiDocument.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ namespace LEGO.AsyncAPI.Models
66
using System.Collections.Generic;
77
using LEGO.AsyncAPI.Exceptions;
88
using LEGO.AsyncAPI.Models.Interfaces;
9-
using LEGO.AsyncAPI.Writers;
109
using LEGO.AsyncAPI.Services;
10+
using LEGO.AsyncAPI.Writers;
1111

1212
/// <summary>
1313
/// This is the root document object for the API specification. It combines resource listing and API declaration together into one document.
@@ -46,7 +46,7 @@ public class AsyncApiDocument : IAsyncApiExtensible, IAsyncApiSerializable
4646
/// <summary>
4747
/// REQUIRED. The available channels and messages for the API.
4848
/// </summary>
49-
public IDictionary<string, AsyncApiChannel> Channels { get; set; } = new Dictionary<string, AsyncApiChannel>();
49+
public IDictionary<string, AsyncApiChannel> Channels { get; set; }
5050

5151
/// <summary>
5252
/// an element to hold various schemas for the specification.

src/LEGO.AsyncAPI/Validation/Rules/AsyncApiDocumentRules.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public static class AsyncApiDocumentRules
3030
context.Exit();
3131

3232
context.Enter("channels");
33-
if (document.Channels == null || !document.Channels.Keys.Any())
33+
if (document.Channels == null)
3434
{
3535
context.CreateError(
3636
nameof(DocumentRequiredFields),

test/LEGO.AsyncAPI.Tests/AsyncApiDocumentBuilder.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
namespace LEGO.AsyncAPI.Tests
44
{
5+
using System;
6+
using System.Collections.Generic;
57
using LEGO.AsyncAPI.Models;
68
using LEGO.AsyncAPI.Models.Interfaces;
7-
using System;
89

910
internal class AsyncApiDocumentBuilder
1011
{
@@ -42,6 +43,10 @@ public AsyncApiDocumentBuilder WithDefaultContentType(string contentType = "appl
4243

4344
public AsyncApiDocumentBuilder WithChannel(string key, AsyncApiChannel channel)
4445
{
46+
if (this.document.Channels == null)
47+
{
48+
this.document.Channels = new Dictionary<string, AsyncApiChannel>();
49+
}
4550
this.document.Channels.Add(key, channel);
4651
return this;
4752
}

test/LEGO.AsyncAPI.Tests/AsyncApiDocumentV2Tests.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1202,6 +1202,10 @@ public void Serialize_WithBindingReferences_SerializesDeserializes()
12021202
},
12031203
},
12041204
};
1205+
if (doc.Channels == null)
1206+
{
1207+
doc.Channels = new Dictionary<string, AsyncApiChannel>();
1208+
}
12051209
doc.Channels.Add(
12061210
"testChannel",
12071211
new AsyncApiChannel
@@ -1260,6 +1264,10 @@ public void Serializev2_WithBindings_Serializes()
12601264
Protocol = "pulsar+ssl",
12611265
Url = "example.com",
12621266
});
1267+
if (doc.Channels == null)
1268+
{
1269+
doc.Channels = new Dictionary<string, AsyncApiChannel>();
1270+
}
12631271
doc.Channels.Add(
12641272
"testChannel",
12651273
new AsyncApiChannel

0 commit comments

Comments
 (0)