Skip to content

Commit

Permalink
Fixed Types Analyzer to recognize the new generic attribute.
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelstaib committed Aug 30, 2022
1 parent 2123107 commit 4e154dd
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 13 deletions.
Expand Up @@ -18,7 +18,7 @@ public bool Consume(ISyntaxInfo syntaxInfo)
Compilation compilation,
IReadOnlyCollection<ISyntaxInfo> syntaxInfos)
{
ModuleInfo module =
var module =
syntaxInfos.OfType<ModuleInfo>().FirstOrDefault() ??
new ModuleInfo(
compilation.AssemblyName is null
Expand Down Expand Up @@ -56,7 +56,7 @@ public bool Consume(ISyntaxInfo syntaxInfo)

code.Append(Indent).Append(Indent).AppendLine("{");

foreach (ISyntaxInfo syntaxInfo in batch.Distinct())
foreach (var syntaxInfo in batch.Distinct())
{
switch (syntaxInfo)
{
Expand Down
Expand Up @@ -23,7 +23,7 @@ public bool Equals(AggregateInfo? other)
return true;
}

return _syntaxInfos == other._syntaxInfos;
return Equals(_syntaxInfos, other._syntaxInfos);
}

public override bool Equals(object? obj)
Expand All @@ -38,4 +38,4 @@ public override int GetHashCode()

public static bool operator !=(AggregateInfo? left, AggregateInfo? right)
=> !Equals(left, right);
}
}
Expand Up @@ -14,15 +14,15 @@ public class ModuleInspector : ISyntaxInspector
{
if (context.Node is AttributeListSyntax attributeList)
{
foreach (AttributeSyntax attributeSyntax in attributeList.Attributes)
foreach (var attributeSyntax in attributeList.Attributes)
{
var symbol = context.SemanticModel.GetSymbolInfo(attributeSyntax).Symbol;
if (symbol is not IMethodSymbol attributeSymbol)
{
continue;
}

INamedTypeSymbol attributeContainingTypeSymbol = attributeSymbol.ContainingType;
var attributeContainingTypeSymbol = attributeSymbol.ContainingType;
var fullName = attributeContainingTypeSymbol.ToDisplayString();

if (fullName.Equals(ModuleAttribute, Ordinal) &&
Expand Down
Expand Up @@ -3,6 +3,7 @@
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using static System.StringComparison;
using static HotChocolate.Types.Analyzers.WellKnownAttributes;

namespace HotChocolate.Types.Analyzers.Inspectors;

Expand All @@ -14,9 +15,9 @@ public class TypeAttributeInspector : ISyntaxInspector
{
if (context.Node is BaseTypeDeclarationSyntax { AttributeLists.Count: > 0 } possibleType)
{
foreach (AttributeListSyntax attributeListSyntax in possibleType.AttributeLists)
foreach (var attributeListSyntax in possibleType.AttributeLists)
{
foreach (AttributeSyntax attributeSyntax in attributeListSyntax.Attributes)
foreach (var attributeSyntax in attributeListSyntax.Attributes)
{
var symbol = context.SemanticModel.GetSymbolInfo(attributeSyntax).Symbol;

Expand All @@ -28,14 +29,16 @@ public class TypeAttributeInspector : ISyntaxInspector
var attributeContainingTypeSymbol = attributeSymbol.ContainingType;
var fullName = attributeContainingTypeSymbol.ToDisplayString();

if (WellKnownAttributes.ExtendObjectTypeAttribute.Equals(fullName, Ordinal) &&
// We do a start with here to capture the generic and non generic variant of
// the object type extension attribute.
if (fullName.StartsWith(ExtendObjectTypeAttribute, Ordinal) &&
context.SemanticModel.GetDeclaredSymbol(possibleType) is { } typeExt)
{
syntaxInfo = new TypeExtensionInfo(typeExt.ToDisplayString());
return true;
}

if (WellKnownAttributes.TypeAttributes.Contains(fullName) &&
if (TypeAttributes.Contains(fullName) &&
context.SemanticModel.GetDeclaredSymbol(possibleType) is { } type)
{
syntaxInfo = new TypeInfo(type.ToDisplayString());
Expand Down
Expand Up @@ -57,7 +57,7 @@ private static bool IsAssemblyAttributeList(SyntaxNode node)
{
for (var i = 0; i < _inspectors.Length; i++)
{
if (_inspectors[i].TryHandle(context, out ISyntaxInfo? syntaxInfo))
if (_inspectors[i].TryHandle(context, out var syntaxInfo))
{
return syntaxInfo;
}
Expand Down Expand Up @@ -87,7 +87,7 @@ private static bool IsAssemblyAttributeList(SyntaxNode node)
}
}

foreach (ISyntaxGenerator syntaxGenerator in _generators)
foreach (var syntaxGenerator in _generators)
{
// capture the current list of infos
current = all;
Expand Down
Expand Up @@ -11,4 +11,3 @@ public class PersonAddress
CancellationToken cancellationToken)
=> dataLoader.LoadAsync("abc", cancellationToken);
}

@@ -0,0 +1,7 @@
namespace HotChocolate.Types;

[ExtendObjectType<Person>]
public class PersonLastName
{
public string LastName { get; } = default!;
}
Expand Up @@ -3,6 +3,7 @@
<PropertyGroup>
<AssemblyName>HotChocolate.Types.Tests</AssemblyName>
<RootNamespace>HotChocolate</RootNamespace>
<LangVersion>Preview</LangVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
Expand Up @@ -619,6 +619,23 @@ public async Task Ensure_Member_And_ResolverMember_Are_Correctly_Set_When_Extend
Assert.Equal("GetFoo1", field.ResolverMember?.Name);
}

#if NET6_0_OR_GREATER
[Fact]
public async Task Ensure_Member_And_ResolverMember_Are_Correctly_Set_When_Extending_Generic()
{
var schema =
await new ServiceCollection()
.AddGraphQL()
.AddQueryType<ObjectField_Test_Query>()
.AddTypeExtension<ObjectField_Test_Query_Extension_Generic>()
.BuildSchemaAsync();

IObjectField field = schema.QueryType.Fields["foo1"];
Assert.Equal("GetFoo", field.Member?.Name);
Assert.Equal("GetFoo1", field.ResolverMember?.Name);
}
#endif

[Fact]
public async Task Ensure_Member_And_ResolverMember_Are_The_Same_When_Not_Extending()
{
Expand Down Expand Up @@ -960,6 +977,15 @@ public class ObjectField_Test_Query_Extension
public string GetFoo1() => null!;
}

#if NET6_0_OR_GREATER
[ExtendObjectType<ObjectField_Test_Query>]
public class ObjectField_Test_Query_Extension_Generic
{
[BindMember(nameof(ObjectField_Test_Query.GetFoo))]
public string GetFoo1() => null!;
}
#endif

public class FooQueryType : ObjectType
{
protected override void Configure(IObjectTypeDescriptor descriptor)
Expand Down

0 comments on commit 4e154dd

Please sign in to comment.