Skip to content

Commit 049100c

Browse files
committed
[#32] [add] impl
1 parent 530a7ef commit 049100c

File tree

4 files changed

+45
-8
lines changed

4 files changed

+45
-8
lines changed

src/Simplify.Web.Postman/Assembly/Collection/PartBuilders/RequestBuilder.cs

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,24 @@ public static Request Build(IControllerMetaData metaData, KeyValuePair<HttpMetho
6868
return body;
6969
}
7070

71-
private static string BuildRequestJsonData(Type modelType) => JsonSerializer.Serialize(CreateObject(modelType), new JsonSerializerOptions
71+
private static string BuildRequestJsonData(Type modelType)
7272
{
73-
WriteIndented = true
74-
});
73+
var model = CreateObject(modelType);
7574

76-
private static object? CreateObject(Type modelType)
75+
InitializeListsSingleEmptyValues(model);
76+
77+
return JsonSerializer.Serialize(model, new JsonSerializerOptions
78+
{
79+
WriteIndented = true
80+
});
81+
}
82+
83+
private static object CreateObject(Type modelType)
7784
{
7885
if (IsGenericList(modelType))
7986
modelType = ConstructGenericListTypeFromGenericIList(modelType);
8087

81-
return Activator.CreateInstance(modelType);
88+
return Activator.CreateInstance(modelType) ?? throw new InvalidOperationException("Error creating model.");
8289
}
8390

8491
private static bool IsGenericList(Type type) => type.IsGenericType && typeof(IList<>).IsAssignableFrom(type.GetGenericTypeDefinition());
@@ -91,4 +98,25 @@ private static Type ConstructGenericListTypeFromGenericIList(Type sourceListType
9198

9299
return type.MakeGenericType(typeArgs);
93100
}
101+
102+
private static void InitializeListsSingleEmptyValues(object model)
103+
{
104+
var type = model.GetType();
105+
106+
foreach (var propertyInfo in type.GetProperties())
107+
{
108+
if (!IsGenericList(propertyInfo.PropertyType))
109+
continue;
110+
111+
var listObjectType = propertyInfo.PropertyType.GetGenericArguments()[0];
112+
var emptyListType = ConstructGenericListTypeFromGenericIList(propertyInfo.PropertyType);
113+
114+
var emptyList = Activator.CreateInstance(emptyListType);
115+
var emptyItem = Activator.CreateInstance(listObjectType);
116+
117+
emptyListType.GetMethod("Add")!.Invoke(emptyList, new[] { emptyItem });
118+
119+
propertyInfo.SetValue(model, emptyList);
120+
}
121+
}
94122
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace TesterApp.ViewModels.Addresses;
2+
3+
public class AddressViewModel
4+
{
5+
public string CityName { get; set; }
6+
public string AddressLine1 { get; set; }
7+
}

src/TesterApp/ViewModels/Users/UserAddViewModel.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using Simplify.Web.Model.Validation.Attributes;
1+
using System.Collections.Generic;
2+
using Simplify.Web.Model.Validation.Attributes;
3+
using TesterApp.ViewModels.Addresses;
24

35
namespace TesterApp.ViewModels.Users;
46

@@ -9,4 +11,6 @@ public class UserAddViewModel
911

1012
[Required]
1113
public string Password { get; set; }
14+
15+
public IList<AddressViewModel> Addresses { get; set; }
1216
}

src/TesterApp/ViewModels/Users/UserViewModel.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,4 @@ public class UserViewModel
88
{
99
public string UserName { get; set; }
1010
public DateTime CreationTime { get; set; }
11-
12-
public IList<AddressViewModel> Addresses { get; set; }
1311
}

0 commit comments

Comments
 (0)