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
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ public static class IServiceCollectionExtensions
/// <returns>The configured <see cref="IServiceCollection"/></returns>
public static IServiceCollection AddAsyncApi(this IServiceCollection services)
{
services.AddSerialization();
services.AddJsonSerializer();
services.AddYamlDotNetSerializer();
services.AddAsyncApiIO();
services.TryAddSingleton<IAsyncApiDocumentReader, AsyncApiDocumentReader>();
services.TryAddSingleton<IAsyncApiDocumentWriter, AsyncApiDocumentWriter>();
services.TryAddTransient<IAsyncApiDocumentBuilder, AsyncApiDocumentBuilder>();
Expand Down
32 changes: 32 additions & 0 deletions src/Neuroglia.AsyncApi.IO/AsyncApiYamlTypeResolver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright © 2021-Present Neuroglia SRL. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using Neuroglia.Serialization.Yaml;
using YamlDotNet.Core.Events;
using YamlDotNet.Serialization;

namespace Neuroglia.AsyncApi.IO;

internal sealed class AsyncApiYamlTypeResolver
: INodeTypeResolver
{

readonly InferTypeResolver inner = new();

public bool Resolve(NodeEvent? nodeEvent, ref Type currentType)
{
if (currentType != typeof(object)) return false;
return this.inner.Resolve(nodeEvent, ref currentType);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Neuroglia.Serialization.Yaml;

namespace Neuroglia.AsyncApi.IO;

Expand All @@ -32,7 +33,14 @@ public static IServiceCollection AddAsyncApiIO(this IServiceCollection services,
{
services.AddSerialization();
services.AddJsonSerializer();
services.AddYamlDotNetSerializer();
services.AddYamlDotNetSerializer(builder =>
{
YamlSerializer.DefaultSerializerConfiguration(builder.Serializer);
YamlSerializer.DefaultDeserializerConfiguration(builder.Deserializer);
builder.Deserializer
.WithoutNodeTypeResolver<InferTypeResolver>()
.WithNodeTypeResolver(new AsyncApiYamlTypeResolver());
});
services.TryAdd(new ServiceDescriptor(typeof(IAsyncApiDocumentReader), typeof(AsyncApiDocumentReader), lifetime: lifetime));
services.TryAdd(new ServiceDescriptor(typeof(IAsyncApiDocumentWriter), typeof(AsyncApiDocumentWriter), lifetime: lifetime));
return services;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ public async Task Subscribe_Should_Work()
var messagesReceived = new List<IAsyncApiMessage>();
var subscription = result.Messages?.Subscribe(messagesReceived.Add);
await using var nats = new NatsClient(serverAddress);
// NATS.Net establishes async-enumerable subscriptions in the background.
await Task.Delay(1000);
foreach (var message in messagesToSend) await nats.PublishAsync(channelAddress, message.Item1, message.Item2);
await Task.Delay(3500);
subscription?.Dispose();
Expand All @@ -204,4 +206,4 @@ static void ConfigureServices(IServiceCollection services)
services.AddHostedService<ContainerBootstrapper>();
}

}
}
32 changes: 32 additions & 0 deletions tests/Neuroglia.AsyncApi.UnitTests/Cases/IO/IOTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
// limitations under the License.

using Neuroglia.AsyncApi.IO;
using Neuroglia.AsyncApi.Bindings.Mqtt;
using Neuroglia.AsyncApi.v3;

namespace Neuroglia.AsyncApi.UnitTests.Cases.IO;

Expand Down Expand Up @@ -99,4 +101,34 @@ public async Task Write_Then_Read_Yaml_Document_V3_Should_Work()
readDocument.Should().BeEquivalentTo(documentToWrite);
}

[Fact]
public async Task Read_Yaml_Document_With_Unquoted_String_And_Enum_Scalars_Should_Work()
{
const string yaml = """
asyncapi: 3.0.0
info:
title: Repro
version: 1.0.0
channels:
hello:
address: hello
operations:
greet:
action: send
channel:
$ref: '#/channels/hello'
bindings:
mqtt:
qos: 2
""";
using var stream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(yaml));

var document = await this.DocumentReader.ReadAsync(stream);

var v3Document = document.Should().BeOfType<V3AsyncApiDocument>().Subject;
v3Document.AsyncApi.Should().Be("3.0.0");
v3Document.Info.Version.Should().Be("1.0.0");
v3Document.Operations!["greet"].Bindings!.Mqtt!.QoS.Should().Be(MqttQualityOfServiceLevel.ExactlyOne);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public static IContainer Build()
.WithPortBinding(PublicPort, true)
.WithWaitStrategy(Wait
.ForUnixContainer()
.UntilPortIsAvailable(PublicPort)
.UntilMessageIsLogged(".* Time to start RabbitMQ: .*"))
.Build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public static IContainer Build()
.WithPortBinding(PublicPort, true)
.WithWaitStrategy(Wait
.ForUnixContainer()
.UntilPortIsAvailable(PublicPort)
.UntilMessageIsLogged(".* Listening for route connections on .*"))
.Build();
}
Expand Down
Loading