Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

attempted fix for #196 #197

Merged
merged 1 commit into from
Aug 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Gridify/GridifyMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ private void GenerateMappingsRecursive(Type type, string prefix, ushort maxNesti
var fullName = string.IsNullOrEmpty(prefix) ? propertyName : $"{prefix}.{propertyName}";

// Skip classes if nestingLevel is exceeded
if (item.PropertyType.IsClass && item.PropertyType != typeof(string) && !item.PropertyType.IsSimpleTypeCollection(out _))
if (item.PropertyType.IsClass && item.PropertyType != typeof(string) && !item.PropertyType.IsSimpleTypeCollection(out _)
&& !item.PropertyType.IsCollection())
{
if (currentDepth >= maxNestingDepth)
{
Expand Down
12 changes: 12 additions & 0 deletions src/Gridify/Reflection/CollectionTypeHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Collections;

namespace Gridify.Reflection;

public static class CollectionTypeHelper
{
public static bool IsCollection(this Type type)
{
return typeof(IEnumerable).IsAssignableFrom(type) && type != typeof(string);
}
}
44 changes: 44 additions & 0 deletions test/Gridify.Tests/IssueTests/Issue196Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System.Collections.Generic;
using Xunit;

namespace Gridify.Tests.IssueTests;

public class Issue196Tests
{
[Fact]
public void MapperShouldNotThrowExceptionWhenMappingToCollection()
{
var mapper = new GridifyMapper<TestClass>();
mapper.GenerateMappings(2);
// test that no exception is thrown
Assert.True(true);

var issueMapper = new GridifyMapper<Issue196TestClass>();
issueMapper.GenerateMappings(2);
// test that no exception is thrown
Assert.True(true);
}
}

public class Issue196TestClass
{
public string Name { get; set; }

Check warning on line 25 in test/Gridify.Tests/IssueTests/Issue196Tests.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Non-nullable property 'Name' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 25 in test/Gridify.Tests/IssueTests/Issue196Tests.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Non-nullable property 'Name' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 25 in test/Gridify.Tests/IssueTests/Issue196Tests.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Name' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 25 in test/Gridify.Tests/IssueTests/Issue196Tests.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Name' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
// DOES NOT WORK
// public CustomFields CustomFields { get; set; }

// WORDS
public List<CustomField> CustomFields { get; set; }

Check warning on line 30 in test/Gridify.Tests/IssueTests/Issue196Tests.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Non-nullable property 'CustomFields' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 30 in test/Gridify.Tests/IssueTests/Issue196Tests.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Non-nullable property 'CustomFields' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 30 in test/Gridify.Tests/IssueTests/Issue196Tests.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'CustomFields' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 30 in test/Gridify.Tests/IssueTests/Issue196Tests.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'CustomFields' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
}

public class CustomField
{
public string Key { get; init; }
public string Value { get; set; }

public CustomField(string key, string value)
{
Key = key;
Value = value;
}
public CustomField() { }

Check warning on line 43 in test/Gridify.Tests/IssueTests/Issue196Tests.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Non-nullable property 'Key' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 43 in test/Gridify.Tests/IssueTests/Issue196Tests.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Non-nullable property 'Value' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 43 in test/Gridify.Tests/IssueTests/Issue196Tests.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Non-nullable property 'Key' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 43 in test/Gridify.Tests/IssueTests/Issue196Tests.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Non-nullable property 'Value' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 43 in test/Gridify.Tests/IssueTests/Issue196Tests.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Key' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 43 in test/Gridify.Tests/IssueTests/Issue196Tests.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Value' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 43 in test/Gridify.Tests/IssueTests/Issue196Tests.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Key' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 43 in test/Gridify.Tests/IssueTests/Issue196Tests.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Value' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
}
Loading