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
18 changes: 18 additions & 0 deletions src/DendroDocs.Shared/Descriptions/InvocationDescription.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using DendroDocs.Json;

namespace DendroDocs;

/// <summary>
Expand All @@ -6,6 +8,20 @@ namespace DendroDocs;
[DebuggerDisplay("Invocation \"{ContainingType,nq}.{Name,nq}\"")]
public class InvocationDescription(string containingType, string name) : Statement
{
/// <summary>
/// Initializes a new instance of the <see cref="InvocationDescription"/> class with arguments.
/// </summary>
/// <param name="containingType">The type that contains the invoked method or constructor.</param>
/// <param name="name">The name of the invoked method or constructor.</param>
/// <param name="arguments">The collection of arguments passed to the invocation.</param>
[Newtonsoft.Json.JsonConstructor]
[JsonConstructor]
public InvocationDescription(string containingType, string name, List<ArgumentDescription>? arguments)
: this(containingType, name)
{
if (arguments is not null) this.Arguments.AddRange(arguments);
}

/// <summary>
/// Gets the type that contains the invoked method or constructor.
/// </summary>
Expand All @@ -19,5 +35,7 @@ public class InvocationDescription(string containingType, string name) : Stateme
/// <summary>
/// Gets the collection of arguments passed to the invocation.
/// </summary>
[Newtonsoft.Json.JsonProperty(ItemTypeNameHandling = Newtonsoft.Json.TypeNameHandling.None)]
[Newtonsoft.Json.JsonConverter(typeof(ConcreteTypeConverter<List<ArgumentDescription>>))]
public List<ArgumentDescription> Arguments { get; } = [];
}
Original file line number Diff line number Diff line change
Expand Up @@ -485,4 +485,100 @@ public void ConstructorParameters_Should_BeDeserializedCorrectly()
secondParameter.Name.ShouldBe("value");
secondParameter.Attributes.Count.ShouldBe(0);
}

[TestMethod]
public void InvocationArguments_Should_BeDeserializedCorrectly_Newtonsoft()
{
// Assign
var json = """
[{
"FullName": "Test.Controller",
"Methods": [{
"Name": "TestMethod",
"Statements": [{
"$type": "DendroDocs.InvocationDescription, DendroDocs.Shared",
"ContainingType": "Test.Service",
"Name": "PublishMessageAsync",
"Arguments": [{
"Type": "string",
"Text": "e.MessageType"
}, {
"Type": "Test.Event",
"Text": "e"
}]
}]
}]
}]
""";

// Act
var types = JsonConvert.DeserializeObject<List<TypeDescription>>(json, JsonDefaults.DeserializerSettings())!;

// Assert
types.Count.ShouldBe(1);
types[0].Methods.Count.ShouldBe(1);
types[0].Methods[0].Statements.Count.ShouldBe(1);

var invocation = types[0].Methods[0].Statements[0].ShouldBeOfType<InvocationDescription>();
invocation.ContainingType.ShouldBe("Test.Service");
invocation.Name.ShouldBe("PublishMessageAsync");
invocation.Arguments.Count.ShouldBe(2);

invocation.Arguments[0].Type.ShouldBe("string");
invocation.Arguments[0].Text.ShouldBe("e.MessageType");

invocation.Arguments[1].Type.ShouldBe("Test.Event");
invocation.Arguments[1].Text.ShouldBe("e");
}

[TestMethod]
public void InvocationArguments_Should_BeDeserializedCorrectly_ComplexExample_Newtonsoft()
{
// Test case based on the original issue's JSON
var json = """
[{
"FullName": "Pitstop.Application.CustomerManagementAPI.Controllers.CustomersController",
"Methods": [{
"Name": "RegisterAsync",
"Statements": [{
"$type": "DendroDocs.InvocationDescription, DendroDocs.Shared",
"ContainingType": "Pitstop.Infrastructure.Messaging.IMessagePublisher",
"Name": "PublishMessageAsync",
"Arguments": [{
"Type": "string",
"Text": "e.MessageType"
}, {
"Type": "Pitstop.CustomerManagementAPI.Events.CustomerRegistered",
"Text": "e"
}, {
"Type": "string",
"Text": ""
}]
}]
}]
}]
""";

// Act
var types = JsonConvert.DeserializeObject<List<TypeDescription>>(json, JsonDefaults.DeserializerSettings())!;

// Assert
types.Count.ShouldBe(1);
types[0].Methods.Count.ShouldBe(1);
types[0].Methods[0].Statements.Count.ShouldBe(1);

var invocation = types[0].Methods[0].Statements[0].ShouldBeOfType<InvocationDescription>();
invocation.ContainingType.ShouldBe("Pitstop.Infrastructure.Messaging.IMessagePublisher");
invocation.Name.ShouldBe("PublishMessageAsync");
invocation.Arguments.Count.ShouldBe(3);

invocation.Arguments[0].Type.ShouldBe("string");
invocation.Arguments[0].Text.ShouldBe("e.MessageType");

invocation.Arguments[1].Type.ShouldBe("Pitstop.CustomerManagementAPI.Events.CustomerRegistered");
invocation.Arguments[1].Text.ShouldBe("e");

invocation.Arguments[2].Type.ShouldBe("string");
invocation.Arguments[2].Text.ShouldBe("");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -580,4 +580,100 @@ public void ConstructorParameters_Should_BeDeserializedCorrectly()
secondParameter.Name.ShouldBe("value");
secondParameter.Attributes.Count.ShouldBe(0);
}

[TestMethod]
public void InvocationArguments_Should_BeDeserializedCorrectly_TextJson()
{
// Assign
var json = """
[{
"FullName": "Test.Controller",
"Methods": [{
"Name": "TestMethod",
"Statements": [{
"$type": "DendroDocs.InvocationDescription, DendroDocs.Shared",
"ContainingType": "Test.Service",
"Name": "PublishMessageAsync",
"Arguments": [{
"Type": "string",
"Text": "e.MessageType"
}, {
"Type": "Test.Event",
"Text": "e"
}]
}]
}]
}]
""";

// Act
var types = JsonSerializer.Deserialize<List<TypeDescription>>(json, JsonDefaults.DeserializerOptions())!;

// Assert
types.Count.ShouldBe(1);
types[0].Methods.Count.ShouldBe(1);
types[0].Methods[0].Statements.Count.ShouldBe(1);

var invocation = types[0].Methods[0].Statements[0].ShouldBeOfType<InvocationDescription>();
invocation.ContainingType.ShouldBe("Test.Service");
invocation.Name.ShouldBe("PublishMessageAsync");
invocation.Arguments.Count.ShouldBe(2);

invocation.Arguments[0].Type.ShouldBe("string");
invocation.Arguments[0].Text.ShouldBe("e.MessageType");

invocation.Arguments[1].Type.ShouldBe("Test.Event");
invocation.Arguments[1].Text.ShouldBe("e");
}

[TestMethod]
public void InvocationArguments_Should_BeDeserializedCorrectly_ComplexExample_TextJson()
{
// Test case based on the original issue's JSON
var json = """
[{
"FullName": "Pitstop.Application.CustomerManagementAPI.Controllers.CustomersController",
"Methods": [{
"Name": "RegisterAsync",
"Statements": [{
"$type": "DendroDocs.InvocationDescription, DendroDocs.Shared",
"ContainingType": "Pitstop.Infrastructure.Messaging.IMessagePublisher",
"Name": "PublishMessageAsync",
"Arguments": [{
"Type": "string",
"Text": "e.MessageType"
}, {
"Type": "Pitstop.CustomerManagementAPI.Events.CustomerRegistered",
"Text": "e"
}, {
"Type": "string",
"Text": ""
}]
}]
}]
}]
""";

// Act
var types = JsonSerializer.Deserialize<List<TypeDescription>>(json, JsonDefaults.DeserializerOptions())!;

// Assert
types.Count.ShouldBe(1);
types[0].Methods.Count.ShouldBe(1);
types[0].Methods[0].Statements.Count.ShouldBe(1);

var invocation = types[0].Methods[0].Statements[0].ShouldBeOfType<InvocationDescription>();
invocation.ContainingType.ShouldBe("Pitstop.Infrastructure.Messaging.IMessagePublisher");
invocation.Name.ShouldBe("PublishMessageAsync");
invocation.Arguments.Count.ShouldBe(3);

invocation.Arguments[0].Type.ShouldBe("string");
invocation.Arguments[0].Text.ShouldBe("e.MessageType");

invocation.Arguments[1].Type.ShouldBe("Pitstop.CustomerManagementAPI.Events.CustomerRegistered");
invocation.Arguments[1].Text.ShouldBe("e");

invocation.Arguments[2].Type.ShouldBe("string");
invocation.Arguments[2].Text.ShouldBe("");
}
}
Loading