Skip to content

Commit 77d8505

Browse files
CopiloteNeRGy164
andauthored
Fix Parameters not being deserialized with System.Text.Json
* Fix Parameters not being deserialized with System.Text.Json --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: eNeRGy164 <10671831+eNeRGy164@users.noreply.github.com>
1 parent b40a277 commit 77d8505

File tree

4 files changed

+204
-6
lines changed

4 files changed

+204
-6
lines changed

src/DendroDocs.Shared/Descriptions/ConstructorDescription.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,18 @@ namespace DendroDocs;
99
public class ConstructorDescription(string name) : MemberDescription(name), IHaveAMethodBody, IJsonOnDeserialized
1010
{
1111
/// <summary>
12-
/// Initializes a new instance of the <see cref="ConstructorDescription"/> class with constructor statements.
12+
/// Initializes a new instance of the <see cref="ConstructorDescription"/> class with constructor parameters and statements.
1313
/// </summary>
1414
/// <param name="name">The name of the constructor.</param>
15+
/// <param name="parameters">The collection of parameter descriptions for the constructor.</param>
1516
/// <param name="statements">The collection of statements in the constructor body.</param>
1617
[Newtonsoft.Json.JsonConstructor]
1718
[JsonConstructor]
18-
public ConstructorDescription(string name, List<Statement> statements)
19+
public ConstructorDescription(string name, List<ParameterDescription>? parameters, List<Statement>? statements)
1920
: this(name)
2021
{
21-
this.Statements = statements ?? [];
22+
if (parameters is not null) this.Parameters.AddRange(parameters);
23+
if (statements is not null) this.Statements.AddRange(statements);
2224
}
2325

2426
/// <summary>

src/DendroDocs.Shared/Descriptions/MethodDescription.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,19 @@ namespace DendroDocs;
99
public class MethodDescription(string? returnType, string name) : MemberDescription(name), IHaveAMethodBody, IJsonOnDeserialized
1010
{
1111
/// <summary>
12-
/// Initializes a new instance of the <see cref="MethodDescription"/> class with method statements.
12+
/// Initializes a new instance of the <see cref="MethodDescription"/> class with method parameters and statements.
1313
/// </summary>
1414
/// <param name="returnType">The return type of the method.</param>
1515
/// <param name="name">The name of the method.</param>
16+
/// <param name="parameters">The collection of parameter descriptions for the method.</param>
1617
/// <param name="statements">The collection of statements in the method body.</param>
1718
[Newtonsoft.Json.JsonConstructor]
1819
[JsonConstructor]
19-
public MethodDescription(string? returnType, string name, List<Statement> statements)
20+
public MethodDescription(string? returnType, string name, List<ParameterDescription>? parameters, List<Statement>? statements)
2021
: this(returnType, name)
2122
{
22-
this.Statements = statements ?? [];
23+
if (parameters is not null) this.Parameters.AddRange(parameters);
24+
if (statements is not null) this.Statements.AddRange(statements);
2325
}
2426

2527
/// <summary>

tests/DendroDocs.Shared.Tests/Serialization/NewtonsoftDeserializationTests.cs

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,4 +391,98 @@ public void BaseTypes_Should_BeDeserializedCorrectly()
391391
types[0].BaseTypes.ShouldContain("Pitstop.Infrastructure.Messaging.Event");
392392
types[0].BaseTypes.ShouldContain("System.Object");
393393
}
394+
395+
[TestMethod]
396+
public void MethodParameters_Should_BeDeserializedCorrectly()
397+
{
398+
// Assign
399+
var json = @"[{
400+
""FullName"": ""Pitstop.Application.CustomerManagementAPI.Controllers.CustomersController"",
401+
""BaseTypes"": [
402+
""Microsoft.AspNetCore.Mvc.Controller""
403+
],
404+
""Modifiers"": 2,
405+
""Methods"": [{
406+
""Parameters"": [{
407+
""Type"": ""Pitstop.CustomerManagementAPI.Commands.RegisterCustomer"",
408+
""Name"": ""command"",
409+
""Attributes"": [{
410+
""Type"": ""Microsoft.AspNetCore.Mvc.FromBodyAttribute"",
411+
""Name"": ""FromBody""
412+
}]
413+
}],
414+
""Name"": ""RegisterAsync"",
415+
""Modifiers"": 258,
416+
""Attributes"": [{
417+
""Type"": ""Microsoft.AspNetCore.Mvc.HttpPostAttribute"",
418+
""Name"": ""HttpPost""
419+
}]
420+
}]
421+
}]";
422+
423+
// Act
424+
var types = JsonConvert.DeserializeObject<List<TypeDescription>>(json, JsonDefaults.DeserializerSettings())!;
425+
426+
// Assert
427+
types.Count.ShouldBe(1);
428+
types[0].Methods.Count.ShouldBe(1);
429+
430+
var method = types[0].Methods[0];
431+
method.Name.ShouldBe("RegisterAsync");
432+
method.Parameters.Count.ShouldBe(1);
433+
434+
var parameter = method.Parameters[0];
435+
parameter.Type.ShouldBe("Pitstop.CustomerManagementAPI.Commands.RegisterCustomer");
436+
parameter.Name.ShouldBe("command");
437+
parameter.Attributes.Count.ShouldBe(1);
438+
439+
var parameterAttribute = parameter.Attributes[0];
440+
parameterAttribute.Type.ShouldBe("Microsoft.AspNetCore.Mvc.FromBodyAttribute");
441+
parameterAttribute.Name.ShouldBe("FromBody");
442+
}
443+
444+
[TestMethod]
445+
public void ConstructorParameters_Should_BeDeserializedCorrectly()
446+
{
447+
// Assign
448+
var json = @"[{
449+
""FullName"": ""Test.Class"",
450+
""Constructors"": [{
451+
""Name"": ""Test"",
452+
""Parameters"": [{
453+
""Type"": ""string"",
454+
""Name"": ""name"",
455+
""Attributes"": [{
456+
""Type"": ""System.ComponentModel.DataAnnotations.RequiredAttribute"",
457+
""Name"": ""Required""
458+
}]
459+
}, {
460+
""Type"": ""int"",
461+
""Name"": ""value""
462+
}]
463+
}]
464+
}]";
465+
466+
// Act
467+
var types = JsonConvert.DeserializeObject<List<TypeDescription>>(json, JsonDefaults.DeserializerSettings())!;
468+
469+
// Assert
470+
types.Count.ShouldBe(1);
471+
types[0].Constructors.Count.ShouldBe(1);
472+
473+
var constructor = types[0].Constructors[0];
474+
constructor.Name.ShouldBe("Test");
475+
constructor.Parameters.Count.ShouldBe(2);
476+
477+
var firstParameter = constructor.Parameters[0];
478+
firstParameter.Type.ShouldBe("string");
479+
firstParameter.Name.ShouldBe("name");
480+
firstParameter.Attributes.Count.ShouldBe(1);
481+
firstParameter.Attributes[0].Type.ShouldBe("System.ComponentModel.DataAnnotations.RequiredAttribute");
482+
483+
var secondParameter = constructor.Parameters[1];
484+
secondParameter.Type.ShouldBe("int");
485+
secondParameter.Name.ShouldBe("value");
486+
secondParameter.Attributes.Count.ShouldBe(0);
487+
}
394488
}

tests/DendroDocs.Shared.Tests/Serialization/TextJsonDeserializationTests.cs

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,4 +480,104 @@ public void BaseTypes_Should_BeDeserializedCorrectly()
480480
types[0].BaseTypes.ShouldContain("Pitstop.Infrastructure.Messaging.Event");
481481
types[0].BaseTypes.ShouldContain("System.Object");
482482
}
483+
484+
[TestMethod]
485+
public void MethodParameters_Should_BeDeserializedCorrectly()
486+
{
487+
// Assign
488+
var json =
489+
"""
490+
[{
491+
"FullName": "Pitstop.Application.CustomerManagementAPI.Controllers.CustomersController",
492+
"BaseTypes": [
493+
"Microsoft.AspNetCore.Mvc.Controller"
494+
],
495+
"Modifiers": 2,
496+
"Methods": [{
497+
"Parameters": [{
498+
"Type": "Pitstop.CustomerManagementAPI.Commands.RegisterCustomer",
499+
"Name": "command",
500+
"Attributes": [{
501+
"Type": "Microsoft.AspNetCore.Mvc.FromBodyAttribute",
502+
"Name": "FromBody"
503+
}]
504+
}],
505+
"Name": "RegisterAsync",
506+
"Modifiers": 258,
507+
"Attributes": [{
508+
"Type": "Microsoft.AspNetCore.Mvc.HttpPostAttribute",
509+
"Name": "HttpPost"
510+
}]
511+
}]
512+
}]
513+
""";
514+
515+
// Act
516+
var types = JsonSerializer.Deserialize<List<TypeDescription>>(json, JsonDefaults.DeserializerOptions())!;
517+
518+
// Assert
519+
types.Count.ShouldBe(1);
520+
types[0].Methods.Count.ShouldBe(1);
521+
522+
var method = types[0].Methods[0];
523+
method.Name.ShouldBe("RegisterAsync");
524+
method.Parameters.Count.ShouldBe(1);
525+
526+
var parameter = method.Parameters[0];
527+
parameter.Type.ShouldBe("Pitstop.CustomerManagementAPI.Commands.RegisterCustomer");
528+
parameter.Name.ShouldBe("command");
529+
parameter.Attributes.Count.ShouldBe(1);
530+
531+
var parameterAttribute = parameter.Attributes[0];
532+
parameterAttribute.Type.ShouldBe("Microsoft.AspNetCore.Mvc.FromBodyAttribute");
533+
parameterAttribute.Name.ShouldBe("FromBody");
534+
}
535+
536+
[TestMethod]
537+
public void ConstructorParameters_Should_BeDeserializedCorrectly()
538+
{
539+
// Assign
540+
var json =
541+
"""
542+
[{
543+
"FullName": "Test.Class",
544+
"Constructors": [{
545+
"Name": "Test",
546+
"Parameters": [{
547+
"Type": "string",
548+
"Name": "name",
549+
"Attributes": [{
550+
"Type": "System.ComponentModel.DataAnnotations.RequiredAttribute",
551+
"Name": "Required"
552+
}]
553+
}, {
554+
"Type": "int",
555+
"Name": "value"
556+
}]
557+
}]
558+
}]
559+
""";
560+
561+
// Act
562+
var types = JsonSerializer.Deserialize<List<TypeDescription>>(json, JsonDefaults.DeserializerOptions())!;
563+
564+
// Assert
565+
types.Count.ShouldBe(1);
566+
types[0].Constructors.Count.ShouldBe(1);
567+
568+
var constructor = types[0].Constructors[0];
569+
constructor.Name.ShouldBe("Test");
570+
constructor.Parameters.Count.ShouldBe(2);
571+
572+
var firstParameter = constructor.Parameters[0];
573+
firstParameter.Type.ShouldBe("string");
574+
firstParameter.Name.ShouldBe("name");
575+
firstParameter.Attributes.Count.ShouldBe(1);
576+
firstParameter.Attributes[0].Type.ShouldBe("System.ComponentModel.DataAnnotations.RequiredAttribute");
577+
578+
var secondParameter = constructor.Parameters[1];
579+
secondParameter.Type.ShouldBe("int");
580+
secondParameter.Name.ShouldBe("value");
581+
secondParameter.Attributes.Count.ShouldBe(0);
582+
}
483583
}

0 commit comments

Comments
 (0)