Skip to content

Commit

Permalink
Root models complex type properties constructor issue fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
MCKanpolat committed Mar 1, 2018
1 parent a7a5c91 commit 30436d4
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 8 deletions.
9 changes: 9 additions & 0 deletions src/StubGenerator.Test.Models/EnTestEnum.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace StubGenerator.Test.Models
{
public enum EnTestEnum
{
Option1,
Option2,
Option3
}
}
31 changes: 27 additions & 4 deletions src/StubGenerator.Test.Models/ModelWithComplexTypeProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ public class ModelWithComplexTypeProperty

public InnerComplexType ComplexType { get; set; }

public ModelConstructorHasParameters ModelConstructorHasParameters { get; set; }

public ModelConstructorMixed ModelConstructorMixed { get; set; }

public List<InnerComplexType> CollectionTypeComplex { get; set; }
}

Expand All @@ -42,10 +46,29 @@ public class InnerComplexType
public Nullable<int> NullableIntegerProperty2 { get; set; }
}

public enum EnTestEnum

public class ModelConstructorHasParameters
{
public ModelConstructorHasParameters(int intField)
{
IntField = intField;
}

public int IntField { get; set; }
}


public class ModelConstructorMixed
{
Option1,
Option2,
Option3
public ModelConstructorMixed()
{
}

public ModelConstructorMixed(string firstName)
{
FirstName = firstName;
}

public string FirstName { get; set; }
}
}
9 changes: 9 additions & 0 deletions src/StubGenerator.Test/StubManagerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,5 +146,14 @@ public void Should_Set_Given_Values_While_Generating_Data()
Assert.Equal(givenEnum, generatedStubData.EnumProperty);
Assert.Equal(givenInteger, generatedStubData.IntegerProperty);
}

[Fact(DisplayName = "Should Generate Data For Complex Type Properties Check Parameterless")]
public void Should_Generate_Data_For_Complex_Type_Properties_Check_Parameterless()
{
var generatedStubData = _stubManager.CreateNew<ModelWithComplexTypeProperty>();
Assert.NotNull(generatedStubData.ComplexType);
Assert.NotNull(generatedStubData.ModelConstructorMixed);
Assert.Null(generatedStubData.ModelConstructorHasParameters);
}
}
}
12 changes: 8 additions & 4 deletions src/StubMiddleware.Core/Core/StubManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class StubManager : IStubManager
{
private readonly IFakeDataFactory _fakeDataFactory;
private readonly IStubTypeCache _stubTypeCache;

public StubManager(StubManagerOptions stubManagerOptions)
: this(stubManagerOptions, new MemoryStubTypeCache(), new FakeDataFactory())
{
Expand Down Expand Up @@ -88,9 +88,13 @@ private void FillPropertiesWithFakeData<TObject>(TObject obj, PropertyInfo[] pro
{
if (!property.PropertyType.IsSimple())
{
dynamic innerComplexObj = Activator.CreateInstance(property.PropertyType);
obj.GetType().GetProperty(property.Name).SetValue(obj, innerComplexObj);
FillPropertiesWithFakeData(innerComplexObj, _stubTypeCache.GetOrAdd(innerComplexObj, innerComplexObj.GetType().GetProperties()));
/*check the type has parameterless constructor*/
if ((property.PropertyType.GetConstructor(Type.EmptyTypes) != null))
{
dynamic innerComplexObj = Activator.CreateInstance(property.PropertyType);
obj.GetType().GetProperty(property.Name).SetValue(obj, innerComplexObj);
FillPropertiesWithFakeData(innerComplexObj, _stubTypeCache.GetOrAdd(innerComplexObj, innerComplexObj.GetType().GetProperties()));
}
}
else
{
Expand Down

0 comments on commit 30436d4

Please sign in to comment.