Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion src/LEGO.AsyncAPI.Readers/V3/AsyncApiV3VersionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public AsyncApiReference ConvertToAsyncApiReference(

public AsyncApiDocument LoadDocument(RootNode rootNode)
{
return AsyncApiV2Deserializer.LoadAsyncApi(rootNode);
return AsyncApiV3Deserializer.LoadAsyncApi(rootNode);
}

public T LoadElement<T>(ParseNode node)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ public AsyncApiMessageReference(string reference)

public bool Equals(AsyncApiMessageReference other)
{
if (other.Target is AsyncApiMessageReference reference)
{
return this.Equals(reference);
}

return this.Target == other.Target;
}

Expand Down
6 changes: 6 additions & 0 deletions src/LEGO.AsyncAPI/Validation/AsyncApiValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,12 @@ public void AddWarning(AsyncApiValidatorWarning warning)
/// <param name="item">The object to be validated.</param>
public override void Visit(AsyncApiServer item) => this.Validate(item);

/// <summary>
/// Execute validation rules against an <see cref="AsyncApiOperation"/>.
/// </summary>
/// <param name="item">The object to be validated.</param>
public override void Visit(AsyncApiOperation item) => this.Validate(item);

public override void Visit(IServerBinding item) => this.Validate(item);

public override void Visit(IChannelBinding item) => this.Validate(item);
Expand Down
39 changes: 25 additions & 14 deletions src/LEGO.AsyncAPI/Validation/Rules/AsyncApiOperationRules.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) The LEGO Group. All rights reserved.

using System.Collections.Generic;
using System.Linq;

namespace LEGO.AsyncAPI.Validation.Rules
Expand Down Expand Up @@ -46,40 +47,50 @@ public static class AsyncApiOperationRules
}

var channels =
context.RootDocument.Channels.Values.Where(channel => channel.Equals(operation.Channel));
context.RootDocument.Channels.Values.Where(channel => operation.Channel.Equals(channel));

var referencedChannel = channels.FirstOrDefault(c => c.Equals(operation.Channel));
var referencedChannel = channels.FirstOrDefault(c => operation.Channel.Equals(c));
if (referencedChannel == null)
{
context.CreateError(
"OperationChannelRef",
string.Format(Resource.Validation_OperationMustReferenceValidChannel, operation.Title));
return;
}
if (!operation.Messages.All(refMessage => referencedChannel.Messages.Any(message => message.Equals(refMessage))))
{
context.CreateError(
"OperationChannelRef",
string.Format(Resource.Validation_OperationMessagesMustReferenceOperationChannel, operation.Title));
return;
}
});

public static ValidationRule<AsyncApiOperation> OperationMessages =>
new ValidationRule<AsyncApiOperation>(
(context, operation) =>
{
if (context.RootDocument?.Operations.Values.FirstOrDefault(op => op == operation) is null)
{
return;
}

var channels =
context.RootDocument.Channels.Values.Where(channel => channel.Equals(operation.Channel));
context.RootDocument.Channels.Values.Where(channel => operation.Channel.Equals(channel));

var referencedChannel = channels.FirstOrDefault(c => operation.Channel.Equals(c));

if (referencedChannel == null)
{
return;
}

var referencedChannel = channels.FirstOrDefault(c => c.Equals(operation.Channel));
if (!operation.Messages.All(refMessage => referencedChannel.Messages.Any(message => message.Equals(refMessage))))
if (!AllOperationsMessagesReferencesChannelMessages(operation.Messages, referencedChannel.Messages.Values))
{
context.CreateError(
"OperationChannelRef",
string.Format(Resource.Validation_OperationMessagesMustReferenceOperationChannel, operation.Title));
return;
}
});

private static bool AllOperationsMessagesReferencesChannelMessages(
IList<AsyncApiMessageReference> operationMessages, ICollection<AsyncApiMessage> channelMessages) =>
operationMessages.All(opMessage => OperationMessageReferencesAnyChannelMessage(opMessage, channelMessages));

private static bool OperationMessageReferencesAnyChannelMessage(
AsyncApiMessageReference operationMessage, ICollection<AsyncApiMessage> channelMessages) =>
channelMessages.Any(channelMessage => channelMessage.Equals(operationMessage));
}
}
2 changes: 1 addition & 1 deletion src/LEGO.AsyncAPI/Writers/AsyncApiWriterSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public ReferenceInlineSetting ReferenceInline
/// <summary>
/// Returns back if the refernece should be inlined or not.
/// </summary>
/// <param name="reference">The refernece.</param>
/// <param name="reference">The reference.</param>
/// <returns>True if it should be inlined otherwise false.</returns>
public bool ShouldInlineReference(AsyncApiReference reference)
{
Expand Down
242 changes: 161 additions & 81 deletions test/LEGO.AsyncAPI.Tests/Validation/ValidationRuleTests.cs
Original file line number Diff line number Diff line change
@@ -1,89 +1,169 @@
// Copyright (c) The LEGO Group. All rights reserved.

namespace LEGO.AsyncAPI.Tests.Validation
namespace LEGO.AsyncAPI.Tests.Validation;

using System.Linq;
using FluentAssertions;
using LEGO.AsyncAPI.Readers;
using NUnit.Framework;

public class ValidationRuleTests
{
using FluentAssertions;
using LEGO.AsyncAPI.Readers;
using NUnit.Framework;
using System.Linq;
[Test]
public void V2_OperationId_WithNonUniqueKey_DiagnosticsError()
{
var input =
"""
asyncapi: 2.6.0
info:
title: Chat Application
version: 1.0.0
servers:
testing:
url: test.mosquitto.org:1883
protocol: mqtt
description: Test broker
channels:
chat/{personId}:
publish:
operationId: onMessageReceieved
message:
name: text
payload:
type: string
chat/{personIdentity}:
publish:
operationId: onMessageReceieved
message:
name: text
payload:
type: string
""";

new AsyncApiStringReader().Read(input, out var diagnostic);
diagnostic.Errors.First().Message.Should().Be("OperationId: 'onMessageReceieved' is not unique.");
diagnostic.Errors.First().Pointer.Should().Be("#/channels/chat~1{personIdentity}");
}

[Test]
public void V3_OperationChannel_NotReferencingARootChannel_DiagnosticsError()
{
var input =
"""
asyncapi: 3.0.0
info:
title: Chat Application
version: 1.0.0
servers:
testing:
host: test.mosquitto.org:1883
protocol: mqtt
description: Test broker
channels:
chatPersonId:
address: chat.{personId}
messages:
messageReceived:
name: text
payload:
type: string
operations:
onMessageReceived:
title: Message received
channel:
$ref: '#/components/channels/secondChannel'
messages:
- $ref: '#/channels/chatPersonId/messages/messageReceived'
components:
channels:
secondChannel:
address: chat.{secondChannel}
""";

public class ValidationRuleTests
{
[Test]
public void V2_OperationId_WithNonUniqueKey_DiagnosticsError()
{
var input =
"""
asyncapi: 2.6.0
info:
title: Chat Application
version: 1.0.0
servers:
testing:
url: test.mosquitto.org:1883
protocol: mqtt
description: Test broker
channels:
chat/{personId}:
publish:
operationId: onMessageReceieved
message:
name: text
payload:
type: string
chat/{personIdentity}:
publish:
operationId: onMessageReceieved
message:
name: text
payload:
type: string
""";
new AsyncApiStringReader().Read(input, out var diagnostic);
diagnostic.Errors.First().Message.Should().Be("The operation 'Message received' MUST point to a channel definition located in the root Channels Object.");
diagnostic.Errors.First().Pointer.Should().Be("#/operations/onMessageReceived");
}

var document = new AsyncApiStringReader().Read(input, out var diagnostic);
diagnostic.Errors.First().Message.Should().Be("OperationId: 'onMessageReceieved' is not unique.");
diagnostic.Errors.First().Pointer.Should().Be("#/channels/chat~1{personIdentity}");
}
[Test]
public void V3_OperationMessage_NotReferencingARootChannel_DiagnosticsError()
{
var input =
"""
asyncapi: 3.0.0
info:
title: Chat Application
version: 1.0.0
servers:
testing:
host: test.mosquitto.org:1883
protocol: mqtt
description: Test broker
channels:
chatPersonId:
address: chat.{personId}
messages:
messageReceived:
name: text
payload:
type: string
operations:
onMessageReceived:
title: Message received
channel:
$ref: '#/channels/chatPersonId'
messages:
- $ref: '#/components/messages/messageSent'
components:
messages:
messageSent:
name: text
payload:
type: string
""";

[Test]
[TestCase("chat")]
[TestCase("/some/chat/{personId}")]
[TestCase("chat-{personId}")]
[TestCase("chat-{person_id}")]
[TestCase("chat-{person%2Did}")]
[TestCase("chat-{personId2}")]
public void ChannelKey_WithValidKey_Success(string channelKey)
{
var input =
$"""
asyncapi: 2.6.0
info:
title: Chat Application
version: 1.0.0
servers:
testing:
url: test.mosquitto.org:1883
protocol: mqtt
description: Test broker
channels:
{channelKey}:
publish:
operationId: onMessageReceieved
message:
name: text
payload:
type: string
subscribe:
operationId: sendMessage
message:
name: text
payload:
type: string
""";
new AsyncApiStringReader().Read(input, out var diagnostic);
diagnostic.Errors.First().Message.Should().Be("The messages of operation 'Message received' MUST be a subset of the referenced channels messages.");
diagnostic.Errors.First().Pointer.Should().Be("#/operations/onMessageReceived");
}

var document = new AsyncApiStringReader().Read(input, out var diagnostic);
diagnostic.Errors.Should().BeEmpty();
}
}
[Test]
[TestCase("chat")]
[TestCase("/some/chat/{personId}")]
[TestCase("chat-{personId}")]
[TestCase("chat-{person_id}")]
[TestCase("chat-{person%2Did}")]
[TestCase("chat-{personId2}")]
public void ChannelKey_WithValidKey_Success(string channelKey)
{
var input =
$"""
asyncapi: 2.6.0
info:
title: Chat Application
version: 1.0.0
servers:
testing:
url: test.mosquitto.org:1883
protocol: mqtt
description: Test broker
channels:
{channelKey}:
publish:
operationId: onMessageReceieved
message:
name: text
payload:
type: string
subscribe:
operationId: sendMessage
message:
name: text
payload:
type: string
""";

}
new AsyncApiStringReader().Read(input, out var diagnostic);
diagnostic.Errors.Should().BeEmpty();
}
}