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

chore(bindings): change bindings to be ICollection #177

Closed
wants to merge 4 commits into from
Closed
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
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,48 @@ var stream = await httpClient.GetStreamAsync("master/examples/streetlights-kafka
var asyncApiDocument = new AsyncApiStreamReader().Read(stream, out var diagnostic);
```

#### Reading External $ref

You can read externally referenced AsyncAPI documents by setting the `ReferenceResolution` property of the `AsyncApiReaderSettings` object to `ReferenceResolutionSetting.ResolveAllReferences` and providing an implementation for the `IAsyncApiExternalReferenceReader` interface. This interface contains a single method to which the built AsyncAPI.NET reader library will pass the location content contained in a `$ref` property (usually some form of path) and interface will return the content which is retrieved from wherever the `$ref` points to as a `string`. The AsyncAPI.NET reader will then automatically infer the `T` type of the content and recursively parse the external content into an AsyncAPI document as a child of the original document that contained the `$ref`. This means that you can have externally referenced documents that themselves contain external references.

This interface allows users to load the content of their external reference however and from whereever is required. A new instance of the implementor of `IAsyncApiExternalReferenceReader` should be registered with the `ExternalReferenceReader` property of the `AsyncApiReaderSettings` when creating the reader from which the `GetExternalResource` method will be called when resolving external references.

Below is a very simple example of implementation for `IAsyncApiExternalReferenceReader` that simply loads a file and returns it as a string found at the reference endpoint.
```csharp
using System.IO;

public class AsyncApiExternalFileSystemReader : IAsyncApiExternalReferenceReader
{
public string GetExternalResource(string reference)
{
return File.ReadAllText(reference);
}
}
```

This can then be configured in the reader as follows:
```csharp
var settings = new AsyncApiReaderSettings
{
ReferenceResolution = ReferenceResolutionSetting.ResolveAllReferences,
ExternalReferenceReader = new AsyncApiExternalFileSystemReader(),
};
var reader = new AsyncApiStringReader(settings);
```

This would function for a AsyncAPI document with following reference to load the content of `message.yaml` as a `AsyncApiMessage` object inline with the document object.
```yaml
asyncapi: 2.3.0
info:
title: test
version: 1.0.0
channels:
workspace:
publish:
message:
$ref: "../../../message.yaml"
```

### Bindings
To add support for reading bindings, simply add the bindings you wish to support, to the `Bindings` collection of `AsyncApiReaderSettings`.
There is a nifty helper to add different types of bindings, or like in the example `All` of them.
Expand Down
1 change: 0 additions & 1 deletion src/LEGO.AsyncAPI.Bindings/AMQP/AMQPOperationBinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ namespace LEGO.AsyncAPI.Bindings.AMQP
using System;
using System.Collections.Generic;
using LEGO.AsyncAPI.Models;
using LEGO.AsyncAPI.Readers;
using LEGO.AsyncAPI.Readers.ParseNodes;
using LEGO.AsyncAPI.Writers;

Expand Down
18 changes: 9 additions & 9 deletions src/LEGO.AsyncAPI.Bindings/BindingsCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static TCollection Add<TCollection, TItem>(
return destination;
}

public static IEnumerable<IBindingParser<IBinding>> All => new List<IBindingParser<IBinding>>
public static ICollection<IBindingParser<IBinding>> All => new List<IBindingParser<IBinding>>
{
Pulsar,
Kafka,
Expand All @@ -57,51 +57,51 @@ public static TCollection Add<TCollection, TItem>(
MQTT,
};

public static IEnumerable<IBindingParser<IBinding>> Http => new List<IBindingParser<IBinding>>
public static ICollection<IBindingParser<IBinding>> Http => new List<IBindingParser<IBinding>>
{
new HttpOperationBinding(),
new HttpMessageBinding(),
};

public static IEnumerable<IBindingParser<IBinding>> Websockets => new List<IBindingParser<IBinding>>
public static ICollection<IBindingParser<IBinding>> Websockets => new List<IBindingParser<IBinding>>
{
new WebSocketsChannelBinding(),
};

public static IEnumerable<IBindingParser<IBinding>> Kafka => new List<IBindingParser<IBinding>>
public static ICollection<IBindingParser<IBinding>> Kafka => new List<IBindingParser<IBinding>>
{
new KafkaServerBinding(),
new KafkaChannelBinding(),
new KafkaOperationBinding(),
new KafkaMessageBinding(),
};

public static IEnumerable<IBindingParser<IBinding>> Pulsar => new List<IBindingParser<IBinding>>
public static ICollection<IBindingParser<IBinding>> Pulsar => new List<IBindingParser<IBinding>>
{
new PulsarServerBinding(),
new PulsarChannelBinding(),
};

public static IEnumerable<IBindingParser<IBinding>> Sqs => new List<IBindingParser<IBinding>>
public static ICollection<IBindingParser<IBinding>> Sqs => new List<IBindingParser<IBinding>>
{
new SqsChannelBinding(),
new SqsOperationBinding(),
};

public static IEnumerable<IBindingParser<IBinding>> Sns => new List<IBindingParser<IBinding>>
public static ICollection<IBindingParser<IBinding>> Sns => new List<IBindingParser<IBinding>>
{
new SnsChannelBinding(),
new SnsOperationBinding(),
};

public static IEnumerable<IBindingParser<IBinding>> AMQP => new List<IBindingParser<IBinding>>
public static ICollection<IBindingParser<IBinding>> AMQP => new List<IBindingParser<IBinding>>
{
new AMQPChannelBinding(),
new AMQPOperationBinding(),
new AMQPMessageBinding(),
};

public static IEnumerable<IBindingParser<IBinding>> MQTT => new List<IBindingParser<IBinding>>
public static ICollection<IBindingParser<IBinding>> MQTT => new List<IBindingParser<IBinding>>
{
new MQTTServerBinding(),
new MQTTOperationBinding(),
Expand Down
2 changes: 1 addition & 1 deletion src/LEGO.AsyncAPI.Bindings/Http/HttpMessageBinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public override void SerializeProperties(IAsyncApiWriter writer)
protected override FixedFieldMap<HttpMessageBinding> FixedFieldMap => new()
{
{ "bindingVersion", (a, n) => { a.BindingVersion = n.GetScalarValue(); } },
{ "headers", (a, n) => { a.Headers = JsonSchemaDeserializer.LoadSchema(n); } },
{ "headers", (a, n) => { a.Headers = AsyncApiSchemaDeserializer.LoadSchema(n); } },
};
}
}
2 changes: 1 addition & 1 deletion src/LEGO.AsyncAPI.Bindings/Http/HttpOperationBinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public override void SerializeProperties(IAsyncApiWriter writer)
{ "bindingVersion", (a, n) => { a.BindingVersion = n.GetScalarValue(); } },
{ "type", (a, n) => { a.Type = n.GetScalarValue().GetEnumFromDisplayName<HttpOperationType>(); } },
{ "method", (a, n) => { a.Method = n.GetScalarValue(); } },
{ "query", (a, n) => { a.Query = JsonSchemaDeserializer.LoadSchema(n); } },
{ "query", (a, n) => { a.Query = AsyncApiSchemaDeserializer.LoadSchema(n); } },
};

public override string BindingKey => "http";
Expand Down
2 changes: 1 addition & 1 deletion src/LEGO.AsyncAPI.Bindings/Kafka/KafkaMessageBinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public override void SerializeProperties(IAsyncApiWriter writer)
protected override FixedFieldMap<KafkaMessageBinding> FixedFieldMap => new()
{
{ "bindingVersion", (a, n) => { a.BindingVersion = n.GetScalarValue(); } },
{ "key", (a, n) => { a.Key = JsonSchemaDeserializer.LoadSchema(n); } },
{ "key", (a, n) => { a.Key = AsyncApiSchemaDeserializer.LoadSchema(n); } },
{ "schemaIdLocation", (a, n) => { a.SchemaIdLocation = n.GetScalarValue(); } },
{ "schemaIdPayloadEncoding", (a, n) => { a.SchemaIdPayloadEncoding = n.GetScalarValue(); } },
{ "schemaLookupStrategy", (a, n) => { a.SchemaLookupStrategy = n.GetScalarValue(); } },
Expand Down
4 changes: 2 additions & 2 deletions src/LEGO.AsyncAPI.Bindings/Kafka/KafkaOperationBinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public class KafkaOperationBinding : OperationBinding<KafkaOperationBinding>
protected override FixedFieldMap<KafkaOperationBinding> FixedFieldMap => new()
{
{ "bindingVersion", (a, n) => { a.BindingVersion = n.GetScalarValue(); } },
{ "groupId", (a, n) => { a.GroupId = JsonSchemaDeserializer.LoadSchema(n); } },
{ "clientId", (a, n) => { a.ClientId = JsonSchemaDeserializer.LoadSchema(n); } },
{ "groupId", (a, n) => { a.GroupId = AsyncApiSchemaDeserializer.LoadSchema(n); } },
{ "clientId", (a, n) => { a.ClientId = AsyncApiSchemaDeserializer.LoadSchema(n); } },
};

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/LEGO.AsyncAPI.Bindings/MQTT/LastWill.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace LEGO.AsyncAPI.Bindings.MQTT
{
using System;
using LEGO.AsyncAPI.Models.Interfaces;
using LEGO.AsyncAPI.Writers;
using System;

public class LastWill : IAsyncApiElement
{
Expand Down
2 changes: 1 addition & 1 deletion src/LEGO.AsyncAPI.Bindings/MQTT/MQTTMessageBinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public override void SerializeProperties(IAsyncApiWriter writer)
protected override FixedFieldMap<MQTTMessageBinding> FixedFieldMap => new()
{
{ "payloadFormatIndicator", (a, n) => { a.PayloadFormatIndicator = n.GetIntegerValueOrDefault(); } },
{ "correlationData", (a, n) => { a.CorrelationData = JsonSchemaDeserializer.LoadSchema(n); } },
{ "correlationData", (a, n) => { a.CorrelationData = AsyncApiSchemaDeserializer.LoadSchema(n); } },
{ "contentType", (a, n) => { a.ContentType = n.GetScalarValue(); } },
{ "responseTopic", (a, n) => { a.ResponseTopic = n.GetScalarValue(); } },
};
Expand Down
3 changes: 0 additions & 3 deletions src/LEGO.AsyncAPI.Bindings/MQTT/MQTTOperationBinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
namespace LEGO.AsyncAPI.Bindings.MQTT
{
using System;
using System.Collections.Generic;
using LEGO.AsyncAPI.Models;
using LEGO.AsyncAPI.Readers;
using LEGO.AsyncAPI.Readers.ParseNodes;
using LEGO.AsyncAPI.Writers;

Expand Down
2 changes: 1 addition & 1 deletion src/LEGO.AsyncAPI.Bindings/Sns/Ordering.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace LEGO.AsyncAPI.Bindings.Sns
public class Ordering : IAsyncApiExtensible
{
/// <summary>
/// What type of SNS Topic is this?
/// What type of SNS Topic is this?.
/// </summary>
public OrderingType Type { get; set; }

Expand Down
2 changes: 1 addition & 1 deletion src/LEGO.AsyncAPI.Bindings/Sns/Statement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class Statement : IAsyncApiExtensible
public StringOrStringList Principal { get; set; }

/// <summary>
/// The SNS permission being allowed or denied e.g. sns:Publish
/// The SNS permission being allowed or denied e.g. sns:Publish.
/// </summary>
public StringOrStringList Action { get; set; }

Expand Down
4 changes: 2 additions & 2 deletions src/LEGO.AsyncAPI.Bindings/Sqs/Queue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class Queue : IAsyncApiExtensible
public string Name { get; set; }

/// <summary>
/// Is this a FIFO queue?
/// Is this a FIFO queue?.
/// </summary>
public bool FifoQueue { get; set; }

Expand Down Expand Up @@ -56,7 +56,7 @@ public class Queue : IAsyncApiExtensible
public RedrivePolicy RedrivePolicy { get; set; }

/// <summary>
/// The security policy for the SQS Queue
/// The security policy for the SQS Queue.
/// </summary>
public Policy Policy { get; set; }

Expand Down
2 changes: 1 addition & 1 deletion src/LEGO.AsyncAPI.Bindings/Sqs/RedrivePolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
namespace LEGO.AsyncAPI.Bindings.Sqs
{
using System;
using System.Collections.Generic;
using LEGO.AsyncAPI.Models.Interfaces;
using LEGO.AsyncAPI.Writers;
using System.Collections.Generic;

public class RedrivePolicy : IAsyncApiExtensible
{
Expand Down
2 changes: 1 addition & 1 deletion src/LEGO.AsyncAPI.Bindings/Sqs/SqsOperationBinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace LEGO.AsyncAPI.Bindings.Sqs
public class SqsOperationBinding : OperationBinding<SqsOperationBinding>
{
/// <summary>
/// Queue objects that are either the endpoint for an SNS Operation Binding Object, or the deadLetterQueue of the SQS Operation Binding Object
/// Queue objects that are either the endpoint for an SNS Operation Binding Object, or the deadLetterQueue of the SQS Operation Binding Object.
/// </summary>
public List<Queue> Queues { get; set; }

Expand Down
2 changes: 1 addition & 1 deletion src/LEGO.AsyncAPI.Bindings/Sqs/Statement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class Statement : IAsyncApiExtensible
public StringOrStringList Principal { get; set; }

/// <summary>
/// The SNS permission being allowed or denied e.g. sns:Publish
/// The SNS permission being allowed or denied e.g. sns:Publish.
/// </summary>
public StringOrStringList Action { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public class WebSocketsChannelBinding : ChannelBinding<WebSocketsChannelBinding>
{
{ "bindingVersion", (a, n) => { a.BindingVersion = n.GetScalarValue(); } },
{ "method", (a, n) => { a.Method = n.GetScalarValue(); } },
{ "query", (a, n) => { a.Query = JsonSchemaDeserializer.LoadSchema(n); } },
{ "headers", (a, n) => { a.Headers = JsonSchemaDeserializer.LoadSchema(n); } },
{ "query", (a, n) => { a.Query = AsyncApiSchemaDeserializer.LoadSchema(n); } },
{ "headers", (a, n) => { a.Headers = AsyncApiSchemaDeserializer.LoadSchema(n); } },
};

public override void SerializeProperties(IAsyncApiWriter writer)
Expand Down
Loading
Loading