Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
VisualBean committed Feb 26, 2024
1 parent b58f042 commit 45b8d6a
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 13 deletions.
2 changes: 2 additions & 0 deletions src/LEGO.AsyncAPI/Models/Any/AsyncAPIArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

namespace LEGO.AsyncAPI.Models
{
using System;
using System.Collections.ObjectModel;
using System.Text.Json.Nodes;
using LEGO.AsyncAPI.Models.Interfaces;
using LEGO.AsyncAPI.Writers;

[Obsolete("Please use AsyncApiAny instead")]
public class AsyncApiArray : Collection<AsyncApiAny>, IAsyncApiExtension, IAsyncApiElement
{

Expand Down
2 changes: 2 additions & 0 deletions src/LEGO.AsyncAPI/Models/Any/AsyncAPIObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace LEGO.AsyncAPI.Models
{
using System;
using System.Collections.Generic;
using System.Text.Json.Nodes;
using LEGO.AsyncAPI.Models.Interfaces;
Expand All @@ -10,6 +11,7 @@ namespace LEGO.AsyncAPI.Models
/// <summary>
/// AsyncApi object.
/// </summary>
[Obsolete("Please use AsyncApiAny instead")]
public class AsyncApiObject : Dictionary<string, AsyncApiAny>, IAsyncApiExtension, IAsyncApiElement
{

Expand Down
85 changes: 84 additions & 1 deletion src/LEGO.AsyncAPI/Models/Any/AsyncApiAny.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

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;
using LEGO.AsyncAPI.Writers;
Expand All @@ -13,6 +18,11 @@ namespace LEGO.AsyncAPI.Models
/// <seealso cref="LEGO.AsyncAPI.Models.Interfaces.IAsyncApiExtension" />
public class AsyncApiAny : IAsyncApiElement, IAsyncApiExtension
{
private JsonSerializerOptions options = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
};

private JsonNode node;

/// <summary>
Expand All @@ -24,6 +34,64 @@ public AsyncApiAny(JsonNode node)
this.node = node;
}

public AsyncApiAny(object obj)
{
this.node = JsonNode.Parse(JsonSerializer.Serialize(obj, this.options));
}

public AsyncApiAny(Dictionary<string, AsyncApiAny> dictionary)
{
var jsonObject = new JsonObject();
foreach (var kvp in dictionary)
{
jsonObject.Add(kvp.Key, kvp.Value.node);
}

this.node = jsonObject;
}

public AsyncApiAny(List<object> list)
{
var jsonArray = new JsonArray();
foreach (var item in list)
{
string jsonString = JsonSerializer.Serialize(item, this.options);
jsonArray.Add(JsonNode.Parse(jsonString));
}

this.node = jsonArray;
}

public AsyncApiAny(Dictionary<string, object> dictionary)
{
var jsonObject = new JsonObject();
foreach (var kvp in dictionary)
{
string jsonString = JsonSerializer.Serialize(kvp.Value, this.options);
jsonObject.Add(kvp.Key, JsonNode.Parse(jsonString));
}

this.node = jsonObject;
}

/// <summary>
/// Initializes a new instance of the <see cref="AsyncApiAny"/> class.
/// </summary>
/// <param name="node">The node.</param>
public AsyncApiAny(JsonArray node)
{
this.node = node;
}

/// <summary>
/// Initializes a new instance of the <see cref="AsyncApiAny"/> class.
/// </summary>
/// <param name="node">The node.</param>
public AsyncApiAny(JsonObject node)
{
this.node = node;
}

/// <summary>
/// Gets the node.
/// </summary>
Expand All @@ -34,7 +102,22 @@ public AsyncApiAny(JsonNode node)

public T GetValue<T>()
{
return this.node.GetValue<T>();
return JsonSerializer.Deserialize<T>(this.node.ToJsonString());
}

public bool TryGetValue<T>(out T value)
{
try
{

value = this.GetValue<T>();
return true;
}
catch (System.Exception)
{
value = default(T);
return false;
}
}

/// <summary>
Expand Down
53 changes: 53 additions & 0 deletions test/LEGO.AsyncAPI.Tests/AsyncApiAnyTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using LEGO.AsyncAPI.Models;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LEGO.AsyncAPI.Tests
{

public class AsyncApiAnyTests
{
[Test]
public void Test()
{
// Arrange
// Act
// Assert
}


[Test]
public void ctor_Primitives()
{
// Arrange
// Act
var a = new AsyncApiAny("string");
var b = new AsyncApiAny(1);
var c = new AsyncApiAny(1.1);
var d = new AsyncApiAny(true);
var e = new AsyncApiAny(new MyType("test"));
var f = new AsyncApiAny(new List<string>() { "test", "test2"});
var g = new AsyncApiAny(new List<MyType>() { new MyType("test") });
var h = new AsyncApiAny(new Dictionary<string, int>() { { "t", 2 } });
var i = new AsyncApiAny(new Dictionary<string, MyType>() { { "t", new MyType("test") } });

var v = e.GetValue<MyType>();
var k = i.GetValue<Dictionary<string, MyType>>();
// Assert
}


class MyType
{
public MyType(string value)
{
this.Value = value;
}
public string Value { get; set; }
}
}
}
27 changes: 16 additions & 11 deletions test/LEGO.AsyncAPI.Tests/AsyncApiDocumentV2Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,21 @@ namespace LEGO.AsyncAPI.Tests
using System.Globalization;
using System.IO;
using System.Linq;
using LEGO.AsyncAPI.Bindings.Pulsar;
using LEGO.AsyncAPI.Bindings;
using LEGO.AsyncAPI.Bindings.Http;
using LEGO.AsyncAPI.Bindings.Kafka;
using LEGO.AsyncAPI.Bindings.Pulsar;
using LEGO.AsyncAPI.Models;
using LEGO.AsyncAPI.Models.Interfaces;
using LEGO.AsyncAPI.Readers;
using LEGO.AsyncAPI.Writers;
using NUnit.Framework;

public class ExtensionClass
{
public string Key { get; set; }
public long OtherKey { get; set; }
}
public class AsyncApiDocumentV2Tests
{
[Test]
Expand Down Expand Up @@ -838,8 +843,6 @@ public void SerializeV2_WithFullSpec_Serializes()
string traitTitle = "traitTitle";
string schemaTitle = "schemaTitle";
string schemaDescription = "schemaDescription";
string anyKey = "key";
string anyOtherKey = "otherKey";
string anyStringValue = "value";
long anyLongValue = long.MaxValue;
string exampleSummary = "exampleSummary";
Expand All @@ -864,6 +867,8 @@ public void SerializeV2_WithFullSpec_Serializes()
string refreshUrl = "https://example.com/refresh";
string authorizationUrl = "https://example.com/authorization";
string requirementString = "requirementItem";


var document = new AsyncApiDocument()
{
Id = documentId,
Expand Down Expand Up @@ -1016,11 +1021,11 @@ public void SerializeV2_WithFullSpec_Serializes()
Description = schemaDescription,
Examples = new List<AsyncApiAny>
{
new AsyncApiObject
new AsyncApiAny(new ExtensionClass
{
{ anyKey, new AsyncApiAny(anyStringValue) },
{ anyOtherKey, new AsyncApiAny(anyLongValue) },
},
Key = anyStringValue,
OtherKey = anyLongValue,
}),
},
},
Examples = new List<AsyncApiMessageExample>
Expand All @@ -1029,11 +1034,11 @@ public void SerializeV2_WithFullSpec_Serializes()
{
Summary = exampleSummary,
Name = exampleName,
Payload = new AsyncApiObject
Payload =new AsyncApiAny(new ExtensionClass
{
{ anyKey, new AsyncApiAny(anyStringValue) },
{ anyOtherKey, new AsyncApiAny(anyLongValue) },
},
Key = anyStringValue,
OtherKey = anyLongValue,
}),
Extensions = new Dictionary<string, IAsyncApiExtension>
{
{ extensionKey, new AsyncApiAny(extensionString) },
Expand Down
1 change: 0 additions & 1 deletion test/LEGO.AsyncAPI.Tests/LEGO.AsyncAPI.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="xunit" Version="2.4.1" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit 45b8d6a

Please sign in to comment.