From aab691e42df6d4268bc844c5ad5d35ef0b9a743a Mon Sep 17 00:00:00 2001 From: MCKanpolat Date: Wed, 28 Feb 2018 18:18:54 +0300 Subject: [PATCH] StubManager complex type issue while caching the item --- src/StubGenerator.Test.Models/ComplexModel.cs | 20 +++++++++++++++++++ .../DefaultStubTypeCacheKeyGenerator.cs | 4 ++++ src/StubMiddleware.Core/Core/StubManager.cs | 4 ++-- .../Controllers/ValuesController.cs | 4 ++-- 4 files changed, 28 insertions(+), 4 deletions(-) create mode 100644 src/StubGenerator.Test.Models/ComplexModel.cs diff --git a/src/StubGenerator.Test.Models/ComplexModel.cs b/src/StubGenerator.Test.Models/ComplexModel.cs new file mode 100644 index 0000000..97bf07b --- /dev/null +++ b/src/StubGenerator.Test.Models/ComplexModel.cs @@ -0,0 +1,20 @@ +using System; + +namespace StubGenerator.Test.Models +{ + public abstract class EntityType where T : IComparable + { + public T Id { get; set; } + } + + public class ComplexModelChild : EntityType + { + public string Email { get; set; } + } + + public class ComplexModel : EntityType + { + public string FirstName { get; set; } + public ComplexModelChild ComplexModelChild { get; set; } + } +} diff --git a/src/StubMiddleware.Core/Caching/DefaultStubTypeCacheKeyGenerator.cs b/src/StubMiddleware.Core/Caching/DefaultStubTypeCacheKeyGenerator.cs index 1a35212..e7f4c7c 100644 --- a/src/StubMiddleware.Core/Caching/DefaultStubTypeCacheKeyGenerator.cs +++ b/src/StubMiddleware.Core/Caching/DefaultStubTypeCacheKeyGenerator.cs @@ -5,6 +5,10 @@ public sealed class DefaultStubTypeCacheKeyGenerator : IStubTypeCacheKeyGenerato public string GenerateKey() { var refType = typeof(T); + if (refType.IsGenericType) + { + return $"{refType.Assembly.GetName().Name}_{refType.GetGenericTypeDefinition().FullName}"; + } return $"{refType.Assembly.GetName().Name}_{refType.FullName}"; } } diff --git a/src/StubMiddleware.Core/Core/StubManager.cs b/src/StubMiddleware.Core/Core/StubManager.cs index 476e230..62c315a 100644 --- a/src/StubMiddleware.Core/Core/StubManager.cs +++ b/src/StubMiddleware.Core/Core/StubManager.cs @@ -79,7 +79,7 @@ private void FillPropertiesWithFakeData(TObject obj, PropertyInfo[] pro property.SetValue(obj, collectionTypeInstance); for (var i = 0; i < listItemSize; i++) { - var item = Activator.CreateInstance(complexType); + dynamic item = Activator.CreateInstance(complexType); FillPropertiesWithFakeData(item, _stubTypeCache.GetOrAdd(item, property.PropertyType.GetGenericArguments()[0].GetProperties())); collectionTypeInstance.GetType().GetMethod("Add").Invoke(collectionTypeInstance, new[] { item }); } @@ -88,7 +88,7 @@ private void FillPropertiesWithFakeData(TObject obj, PropertyInfo[] pro { if (!property.PropertyType.IsSimple()) { - var innerComplexObj = Activator.CreateInstance(property.PropertyType); + dynamic innerComplexObj = Activator.CreateInstance(property.PropertyType); obj.GetType().GetProperty(property.Name).SetValue(obj, innerComplexObj); FillPropertiesWithFakeData(innerComplexObj, _stubTypeCache.GetOrAdd(innerComplexObj, innerComplexObj.GetType().GetProperties())); } diff --git a/src/StubMiddleware.Web.Test/Controllers/ValuesController.cs b/src/StubMiddleware.Web.Test/Controllers/ValuesController.cs index 5331f4b..220b6a0 100644 --- a/src/StubMiddleware.Web.Test/Controllers/ValuesController.cs +++ b/src/StubMiddleware.Web.Test/Controllers/ValuesController.cs @@ -9,8 +9,8 @@ public class ValuesController : Controller [HttpGet] public IActionResult Get() { - var testModelNs = typeof(PersonDto).FullName; - var testModelAsmName = typeof(PersonDto).Assembly.GetName().Name; + var testModelNs = typeof(ComplexModel).FullName; + var testModelAsmName = typeof(ComplexModel).Assembly.GetName().Name; var result = new RestApiResult(); var url = Url.Action("get", "stubmiddleware", new { className = $"{testModelNs}, {testModelAsmName}", subItemlistSize = 3, culture = "en-us" }); result.Links.Add(new LinkInfo() { Href = url, Method = "GET", Rel = "self" });