Skip to content

Commit

Permalink
StubManager complex type issue while caching the item
Browse files Browse the repository at this point in the history
  • Loading branch information
MCKanpolat committed Feb 28, 2018
1 parent 2bc73c7 commit aab691e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
20 changes: 20 additions & 0 deletions src/StubGenerator.Test.Models/ComplexModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;

namespace StubGenerator.Test.Models
{
public abstract class EntityType<T> where T : IComparable
{
public T Id { get; set; }
}

public class ComplexModelChild : EntityType<Guid>
{
public string Email { get; set; }
}

public class ComplexModel : EntityType<Guid>
{
public string FirstName { get; set; }
public ComplexModelChild ComplexModelChild { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ public sealed class DefaultStubTypeCacheKeyGenerator : IStubTypeCacheKeyGenerato
public string GenerateKey<T>()
{
var refType = typeof(T);
if (refType.IsGenericType)
{
return $"{refType.Assembly.GetName().Name}_{refType.GetGenericTypeDefinition().FullName}";
}
return $"{refType.Assembly.GetName().Name}_{refType.FullName}";
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/StubMiddleware.Core/Core/StubManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ private void FillPropertiesWithFakeData<TObject>(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 });
}
Expand All @@ -88,7 +88,7 @@ private void FillPropertiesWithFakeData<TObject>(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()));
}
Expand Down
4 changes: 2 additions & 2 deletions src/StubMiddleware.Web.Test/Controllers/ValuesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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" });
Expand Down

0 comments on commit aab691e

Please sign in to comment.