Skip to content

Commit 530a7ef

Browse files
committed
Merge branch 'bugfix/#31' into feature/#32
# Conflicts: # src/Simplify.Web.Postman/Assembly/Collection/PartBuilders/RequestBuilder.cs
2 parents 41a3d3b + 1843780 commit 530a7ef

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,27 @@ public static Request Build(IControllerMetaData metaData, KeyValuePair<HttpMetho
6868
return body;
6969
}
7070

71-
private static string BuildRequestJsonData(Type modelType) => JsonSerializer.Serialize(Activator.CreateInstance(modelType), new JsonSerializerOptions
71+
private static string BuildRequestJsonData(Type modelType) => JsonSerializer.Serialize(CreateObject(modelType), new JsonSerializerOptions
7272
{
7373
WriteIndented = true
7474
});
75+
76+
private static object? CreateObject(Type modelType)
77+
{
78+
if (IsGenericList(modelType))
79+
modelType = ConstructGenericListTypeFromGenericIList(modelType);
80+
81+
return Activator.CreateInstance(modelType);
82+
}
83+
84+
private static bool IsGenericList(Type type) => type.IsGenericType && typeof(IList<>).IsAssignableFrom(type.GetGenericTypeDefinition());
85+
86+
private static Type ConstructGenericListTypeFromGenericIList(Type sourceListType)
87+
{
88+
var type = typeof(List<>);
89+
90+
Type[] typeArgs = { sourceListType.GetGenericArguments()[0] };
91+
92+
return type.MakeGenericType(typeArgs);
93+
}
7594
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Threading.Tasks;
4+
using Microsoft.AspNetCore.Mvc;
5+
using Simplify.Web;
6+
using Simplify.Web.Attributes;
7+
using TesterApp.ViewModels.Users;
8+
9+
namespace TesterApp.Controllers.Api.v1.Users;
10+
11+
[Produces("application/text")]
12+
[Post("/api/v1/users/create-multiple")]
13+
public class CreateMultipleController : AsyncController<IList<UserAddViewModel>>
14+
{
15+
public override async Task<ControllerResponse> Invoke()
16+
{
17+
await ReadModelAsync();
18+
19+
return Content($"User created at {DateTime.Now.ToLongTimeString()}'");
20+
}
21+
}

0 commit comments

Comments
 (0)