diff --git a/src/LEGO.AsyncAPI/Models/Any/AsyncApiAny.cs b/src/LEGO.AsyncAPI/Models/Any/AsyncApiAny.cs index 691886b6..ea926853 100644 --- a/src/LEGO.AsyncAPI/Models/Any/AsyncApiAny.cs +++ b/src/LEGO.AsyncAPI/Models/Any/AsyncApiAny.cs @@ -2,10 +2,7 @@ namespace LEGO.AsyncAPI.Models { - using System.Collections; using System.Collections.Generic; - using System.ComponentModel; - using System.Linq; using System.Text.Json; using System.Text.Json.Nodes; using LEGO.AsyncAPI.Models.Interfaces; @@ -26,7 +23,7 @@ public class AsyncApiAny : IAsyncApiElement, IAsyncApiExtension private JsonNode node; /// - /// Initializes a new instance of the class. + /// Initializes a new instance of the class. /// /// The node. public AsyncApiAny(JsonNode node) @@ -34,11 +31,19 @@ public AsyncApiAny(JsonNode node) this.node = node; } + /// + /// Initializes a new instance of the class. + /// + /// The object. public AsyncApiAny(object obj) { this.node = JsonNode.Parse(JsonSerializer.Serialize(obj, this.options)); } + /// + /// Initializes a new instance of the class. + /// + /// The dictionary. public AsyncApiAny(Dictionary dictionary) { var jsonObject = new JsonObject(); @@ -50,6 +55,10 @@ public AsyncApiAny(Dictionary dictionary) this.node = jsonObject; } + /// + /// Initializes a new instance of the class. + /// + /// The list. public AsyncApiAny(List list) { var jsonArray = new JsonArray(); @@ -62,6 +71,10 @@ public AsyncApiAny(List list) this.node = jsonArray; } + /// + /// Initializes a new instance of the class. + /// + /// The dictionary. public AsyncApiAny(Dictionary dictionary) { var jsonObject = new JsonObject(); @@ -74,6 +87,7 @@ public AsyncApiAny(Dictionary dictionary) this.node = jsonObject; } + /// /// Initializes a new instance of the class. /// @@ -92,19 +106,66 @@ public AsyncApiAny(JsonObject node) this.node = node; } + /// + /// Converts to from an Extension. + /// + /// T. + /// The extension. + /// . + public static T FromExtension(IAsyncApiExtension extension) + { + if (extension is AsyncApiAny any) + { + return any.GetValueOrDefault(); + } + else + { + return default(T); + } + } + /// /// Gets the node. /// + /// . /// /// The node. /// public JsonNode GetNode() => this.node; + /// + /// Gets the value. + /// + /// . + /// . public T GetValue() { return JsonSerializer.Deserialize(this.node.ToJsonString()); } + /// + /// Gets the value or default. + /// + /// . + /// or default. + public T GetValueOrDefault() + { + try + { + return this.GetValue(); + } + catch (System.Exception) + { + return default(T); + } + } + + /// + /// Tries the get value. + /// + /// . + /// The value. + /// true if the value could be converted, otherwise false. public bool TryGetValue(out T value) { try diff --git a/test/LEGO.AsyncAPI.Tests/Bindings/Sns/SnsBindings_Should.cs b/test/LEGO.AsyncAPI.Tests/Bindings/Sns/SnsBindings_Should.cs index d4d5cf05..290ee107 100644 --- a/test/LEGO.AsyncAPI.Tests/Bindings/Sns/SnsBindings_Should.cs +++ b/test/LEGO.AsyncAPI.Tests/Bindings/Sns/SnsBindings_Should.cs @@ -381,6 +381,9 @@ public void SnsOperationBinding_WithFilledObject_SerializesAndDeserializes() var settings = new AsyncApiReaderSettings(); settings.Bindings = BindingsCollection.Sns; var binding = new AsyncApiStringReader(settings).ReadFragment(actual, AsyncApiVersion.AsyncApi2_0, out _); + var binding2 = new AsyncApiStringReader(settings).ReadFragment(expected, AsyncApiVersion.AsyncApi2_0, out _); + binding2.Bindings.First().Value.Extensions.TryGetValue("x-bindingExtension", out IAsyncApiExtension any); + var val = AsyncApiAny.FromExtension(any); // Assert Assert.AreEqual(actual, expected); @@ -388,5 +391,9 @@ public void SnsOperationBinding_WithFilledObject_SerializesAndDeserializes() var expectedSnsBinding = (SnsOperationBinding)operation.Bindings.Values.First(); expectedSnsBinding.Should().BeEquivalentTo((SnsOperationBinding)binding.Bindings.Values.First(), options => options.IgnoringCyclicReferences()); } + class ExtensionClass + { + public string bindingXPropertyName { get; set; } + } } } \ No newline at end of file diff --git a/test/LEGO.AsyncAPI.Tests/AsyncApiAnyTests.cs b/test/LEGO.AsyncAPI.Tests/Models/AsyncApiAnyTests.cs similarity index 60% rename from test/LEGO.AsyncAPI.Tests/AsyncApiAnyTests.cs rename to test/LEGO.AsyncAPI.Tests/Models/AsyncApiAnyTests.cs index a743499f..e335b6b1 100644 --- a/test/LEGO.AsyncAPI.Tests/AsyncApiAnyTests.cs +++ b/test/LEGO.AsyncAPI.Tests/Models/AsyncApiAnyTests.cs @@ -12,16 +12,7 @@ namespace LEGO.AsyncAPI.Tests public class AsyncApiAnyTests { [Test] - public void Test() - { - // Arrange - // Act - // Assert - } - - - [Test] - public void ctor_Primitives() + public void GetValue_ReturnsCorrectConversions() { // Arrange // Act @@ -35,18 +26,27 @@ public void ctor_Primitives() var h = new AsyncApiAny(new Dictionary() { { "t", 2 } }); var i = new AsyncApiAny(new Dictionary() { { "t", new MyType("test") } }); - var v = e.GetValue(); - var k = i.GetValue>(); // Assert + Assert.AreEqual("string", a.GetValue()); + Assert.AreEqual(1, b.GetValue()); + Assert.AreEqual(1.1, c.GetValue()); + Assert.AreEqual(true, d.GetValue()); + Assert.NotNull(e.GetValue()); + Assert.IsNotEmpty(f.GetValue>()); + Assert.IsNotEmpty(f.GetValue>()); + Assert.IsNotEmpty(g.GetValue>()); + Assert.IsNotEmpty(g.GetValue>()); + Assert.IsNotEmpty(h.GetValue>()); + Assert.IsNotEmpty(i.GetValue>()); } - class MyType { public MyType(string value) { this.Value = value; } + public string Value { get; set; } } }