Skip to content

Commit e804548

Browse files
CopiloteNeRGy164
andcommitted
Fix Parameters not being deserialized with System.Text.Json by adding init accessors
Co-authored-by: eNeRGy164 <10671831+eNeRGy164@users.noreply.github.com>
1 parent 5a170f3 commit e804548

File tree

3 files changed

+102
-2
lines changed

3 files changed

+102
-2
lines changed

src/DendroDocs.Shared/Descriptions/ConstructorDescription.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public ConstructorDescription(string name, List<Statement> statements)
2626
/// </summary>
2727
[Newtonsoft.Json.JsonProperty(ItemTypeNameHandling = Newtonsoft.Json.TypeNameHandling.None)]
2828
[Newtonsoft.Json.JsonConverter(typeof(ConcreteTypeConverter<List<ParameterDescription>>))]
29-
public List<ParameterDescription> Parameters { get; } = [];
29+
public List<ParameterDescription> Parameters { get; init; } = [];
3030

3131
/// <summary>
3232
/// Gets the collection of statements that make up the constructor body.

src/DendroDocs.Shared/Descriptions/MethodDescription.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public MethodDescription(string? returnType, string name, List<Statement> statem
3434
/// </summary>
3535
[Newtonsoft.Json.JsonProperty(ItemTypeNameHandling = Newtonsoft.Json.TypeNameHandling.None)]
3636
[Newtonsoft.Json.JsonConverter(typeof(ConcreteTypeConverter<List<ParameterDescription>>))]
37-
public List<ParameterDescription> Parameters { get; } = [];
37+
public List<ParameterDescription> Parameters { get; init; } = [];
3838

3939
/// <summary>
4040
/// Gets the collection of statements that make up the method body.

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)