Skip to content

Commit f5e2311

Browse files
Merge pull request #108 from leancodepl/fix/generic-authorize-when
Handle generic `AuthorizeWhenAttribute`
2 parents aafbe24 + 4e423d3 commit f5e2311

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

examples/attributes/authorize_when.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using LeanCode.Contracts;
2+
using LeanCode.Contracts.Security;
3+
4+
[AuthorizeWhenCustomCtor]
5+
public class A : ICommand { }
6+
7+
[AuthorizeWhenCustomGeneric]
8+
public class B : ICommand { }
9+
10+
public class AuthorizeWhenCustomCtorAttribute : AuthorizeWhenAttribute
11+
{
12+
public AuthorizeWhenCustomCtorAttribute()
13+
: base(typeof(A))
14+
{ }
15+
}
16+
17+
public class AuthorizeWhenCustomGenericAttribute : AuthorizeWhenAttribute<B> { }

src/LeanCode.ContractsGenerator.Tests/ExampleBased/Attributes.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,19 @@ public void AllowUnauthorized_attribute_is_correctly_propagated()
5858
.WithCommand("A")
5959
.WithAttribute("LeanCode.Contracts.Security.AllowUnauthorizedAttribute");
6060
}
61+
62+
[Fact]
63+
public void AuthorizeWhen_attribute_is_correctly_propagated()
64+
{
65+
"attributes/authorize_when.cs"
66+
.Compiles()
67+
.WithCommand("A")
68+
.WithAttribute("AuthorizeWhenCustomCtorAttribute")
69+
.WithCommand("B")
70+
.WithAttribute("AuthorizeWhenCustomGenericAttribute")
71+
.WithDto("AuthorizeWhenCustomCtorAttribute")
72+
.ThatExtends(Known(KnownType.AuthorizeWhenAttribute))
73+
.WithDto("AuthorizeWhenCustomGenericAttribute")
74+
.ThatExtends(Known(KnownType.AuthorizeWhenAttribute));
75+
}
6176
}

src/LeanCode.ContractsGenerator/Compilation/ContractTypes.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public sealed class ContractTypes
1414
private HashSet<INamedTypeSymbol> OperationType { get; }
1515

1616
private HashSet<INamedTypeSymbol> AuthorizeWhenAttribute { get; }
17+
private HashSet<INamedTypeSymbol> GenericAuthorizeWhenAttribute { get; }
1718
private HashSet<INamedTypeSymbol> AuthorizeWhenHasAnyOfAttribute { get; }
1819
private HashSet<INamedTypeSymbol> QueryCacheAttribute { get; }
1920

@@ -31,6 +32,7 @@ public ContractTypes(IReadOnlyCollection<CSharpCompilation> compilations)
3132
OperationType = GetUnboundTypeSymbols(compilations, typeof(IOperation<>));
3233

3334
AuthorizeWhenAttribute = GetTypeSymbols<AuthorizeWhenAttribute>(compilations);
35+
GenericAuthorizeWhenAttribute = GetUnboundTypeSymbols(compilations, typeof(AuthorizeWhenAttribute<>));
3436
AuthorizeWhenHasAnyOfAttribute = GetTypeSymbols<AuthorizeWhenHasAnyOfAttribute>(compilations);
3537
QueryCacheAttribute = GetTypeSymbols<QueryCacheAttribute>(compilations);
3638
ExcludeFromContractsGenerationAttribute = GetTypeSymbols<ExcludeFromContractsGenerationAttribute>(compilations);
@@ -150,7 +152,7 @@ public bool IsOperationType(ITypeSymbol i) =>
150152
i is INamedTypeSymbol ns && ns.IsGenericType && OperationType.Contains(ns.ConstructUnboundGenericType());
151153

152154
public bool IsAuthorizeWhenType(ITypeSymbol i) =>
153-
AuthorizeWhenAttribute.Contains(i);
155+
AuthorizeWhenAttribute.Contains(i) || (i is INamedTypeSymbol ns && ns.IsGenericType && GenericAuthorizeWhenAttribute.Contains(ns.ConstructUnboundGenericType()));
154156

155157
public bool IsAuthorizeWhenHasAnyOfType(ITypeSymbol i) =>
156158
AuthorizeWhenHasAnyOfAttribute.Contains(i);

0 commit comments

Comments
 (0)