Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -247,17 +247,6 @@ private void CollectBuildableTypesFromFrameworkType(
}
}

private static bool ShouldProcessTypeProvider(TypeProvider provider, HashSet<TypeProvider> visitedTypeProviders)
{
if (!visitedTypeProviders.Add(provider))
{
return false;
}

// Check if the type provider implements the model reader/writer interface
return ImplementsModelReaderWriter(provider);
}

private static bool ShouldProcessCSharpType(CSharpType type, HashSet<CSharpType> visitedTypes)
{
if (!type.IsFrameworkType || !visitedTypes.Add(type))
Expand Down Expand Up @@ -401,6 +390,15 @@ private static bool ImplementsModelReaderWriter(TypeProvider typeProvider)
}
}

// Also consider serialization providers that may implement MRW
foreach (var serializationProvider in typeProvider.SerializationProviders)
{
if (ImplementsModelReaderWriter(serializationProvider))
{
return true;
}
}

return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1447,6 +1447,74 @@ public async Task ValidateCustomPropertiesOnModelsAreDiscovered()
"DependencyModel from custom property should be discovered");
}

[Test]
public void ValidateTypeWithCustomSerializationProviderImplementingIJsonModelIsIncluded()
{
var outputLibrary = new TestOutputLibrary([new ModelWithCustomSerialization()]);
var mockGenerator = MockHelpers.LoadMockGenerator(createOutputLibrary: () => outputLibrary);

var contextDefinition = new ModelReaderWriterContextDefinition();
var attributes = contextDefinition.Attributes;

Assert.IsNotNull(attributes);
Assert.IsTrue(attributes.Count > 0);

var buildableAttributes = attributes.Where(a => a.Type.IsFrameworkType && a.Type.FrameworkType == typeof(ModelReaderWriterBuildableAttribute)).ToList();
Assert.IsTrue(buildableAttributes.Any(a => a.Arguments.First().ToDisplayString().Contains("ModelWithCustomSerialization")),
"ModelWithCustomSerialization should be included because it has a serialization provider implementing IJsonModel");
}

[Test]
public void ValidateTypeWithCustomSerializationProviderImplementingIPersistableModelIsIncluded()
{
var outputLibrary = new TestOutputLibrary([new ModelWithCustomSerialization(usePersistableModel: true)]);
var mockGenerator = MockHelpers.LoadMockGenerator(createOutputLibrary: () => outputLibrary);

var contextDefinition = new ModelReaderWriterContextDefinition();
var attributes = contextDefinition.Attributes;

Assert.IsNotNull(attributes);
Assert.IsTrue(attributes.Count > 0);

var buildableAttributes = attributes.Where(a => a.Type.IsFrameworkType && a.Type.FrameworkType == typeof(ModelReaderWriterBuildableAttribute)).ToList();
Assert.IsTrue(buildableAttributes.Any(a => a.Arguments.First().ToDisplayString().Contains("ModelWithCustomSerialization")),
"ModelWithCustomSerialization should be included because it has a serialization provider implementing IJsonModel");
}

private class CustomSerializationProvider : TypeProvider
{
private readonly bool _usePersistableModel;

public CustomSerializationProvider(bool usePersistableModel = false)
{
_usePersistableModel = usePersistableModel;
}
protected override string BuildName() => "CustomSerializationProvider";
protected override string BuildRelativeFilePath() => "CustomSerializationProvider.cs";

protected override CSharpType[] BuildImplements()
{
return [new CSharpType(_usePersistableModel ? typeof(IPersistableModel<object>) : typeof(IJsonModel<object>))];
}
}

private class ModelWithCustomSerialization : TypeProvider
{
private readonly bool _usePersistableModel;

public ModelWithCustomSerialization(bool usePersistableModel = false)
{
_usePersistableModel = usePersistableModel;
}
protected override string BuildName() => "ModelWithCustomSerialization";
protected override string BuildRelativeFilePath() => "ModelWithCustomSerialization.cs";

protected override TypeProvider[] BuildSerializationProviders()
{
return [new CustomSerializationProvider(_usePersistableModel)];
}
}

// Test client provider that has methods with return types
private class TestClientProvider : TypeProvider
{
Expand Down
Loading