Skip to content

Commit 0042b94

Browse files
author
Bart Koelman
committed
Addressed review feedback
1 parent e68b2a5 commit 0042b94

File tree

4 files changed

+59
-7
lines changed

4 files changed

+59
-7
lines changed

docs/usage/routing.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public class OrderLineController : JsonApiController<OrderLine, int>
8686
}
8787
```
8888

89-
## Advanced usage: Custom routing convention
89+
## Advanced usage: custom routing convention
9090

9191
It is possible to replace the built-in routing convention with a [custom routing convention](https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/application-model?view=aspnetcore-3.1#sample-custom-routing-convention) by registering an implementation of `IJsonApiRoutingConvention`.
9292

src/JsonApiDotNetCore.SourceGenerators/ControllerSourceGenerator.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ public void Execute(GeneratorExecutionContext context)
4545
}
4646

4747
INamedTypeSymbol resourceAttributeType = context.Compilation.GetTypeByMetadataName("JsonApiDotNetCore.Resources.Annotations.ResourceAttribute");
48-
INamedTypeSymbol openIdentifiableInterface = context.Compilation.GetTypeByMetadataName("JsonApiDotNetCore.Resources.IIdentifiable`1");
48+
INamedTypeSymbol identifiableOpenInterface = context.Compilation.GetTypeByMetadataName("JsonApiDotNetCore.Resources.IIdentifiable`1");
4949
INamedTypeSymbol loggerFactoryInterface = context.Compilation.GetTypeByMetadataName("Microsoft.Extensions.Logging.ILoggerFactory");
5050

51-
if (resourceAttributeType == null || openIdentifiableInterface == null || loggerFactoryInterface == null)
51+
if (resourceAttributeType == null || identifiableOpenInterface == null || loggerFactoryInterface == null)
5252
{
5353
return;
5454
}
@@ -90,18 +90,18 @@ public void Execute(GeneratorExecutionContext context)
9090

9191
string controllerNamespace = GetControllerNamespace(controllerNamespaceArgument, resourceType);
9292

93-
INamedTypeSymbol closedIdentifiableInterface = FirstOrDefault(resourceType.AllInterfaces, openIdentifiableInterface,
93+
INamedTypeSymbol identifiableClosedInterface = FirstOrDefault(resourceType.AllInterfaces, identifiableOpenInterface,
9494
(@interface, openInterface) => @interface.IsGenericType &&
9595
SymbolEqualityComparer.Default.Equals(@interface.ConstructedFrom, openInterface));
9696

97-
if (closedIdentifiableInterface == null)
97+
if (identifiableClosedInterface == null)
9898
{
9999
var diagnostic = Diagnostic.Create(MissingInterfaceWarning, typeDeclarationSyntax.GetLocation(), resourceType.Name);
100100
context.ReportDiagnostic(diagnostic);
101101
continue;
102102
}
103103

104-
ITypeSymbol idType = closedIdentifiableInterface.TypeArguments[0];
104+
ITypeSymbol idType = identifiableClosedInterface.TypeArguments[0];
105105
string controllerName = $"{resourceType.Name.Pluralize()}Controller";
106106
JsonApiEndpointsCopy endpointsToGenerate = (JsonApiEndpointsCopy?)(int?)endpointsArgument.Value ?? JsonApiEndpointsCopy.All;
107107

src/JsonApiDotNetCore.SourceGenerators/JsonApiEndpointsCopy.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ namespace JsonApiDotNetCore.SourceGenerators
44
{
55
// IMPORTANT: A copy of this type exists in the JsonApiDotNetCore project. Keep these in sync when making changes.
66
[Flags]
7-
internal enum JsonApiEndpointsCopy
7+
public enum JsonApiEndpointsCopy
88
{
99
None = 0,
1010
GetCollection = 1,
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Reflection;
5+
using FluentAssertions;
6+
using JsonApiDotNetCore.Controllers;
7+
using JsonApiDotNetCore.SourceGenerators;
8+
using Xunit;
9+
10+
namespace SourceGeneratorTests
11+
{
12+
public sealed class JsonApiEndpointsCopyTests
13+
{
14+
[Fact]
15+
public void Enum_underlying_types_are_identical()
16+
{
17+
Type sourceType = Enum.GetUnderlyingType(typeof(JsonApiEndpoints));
18+
Type copyType = Enum.GetUnderlyingType(typeof(JsonApiEndpointsCopy));
19+
20+
copyType.Should().Be(sourceType);
21+
}
22+
23+
[Fact]
24+
public void Enum_attributes_in_order_are_identical()
25+
{
26+
Attribute[] sourceAttributes = typeof(JsonApiEndpoints).GetCustomAttributes().ToArray();
27+
Attribute[] copyAttributes = typeof(JsonApiEndpointsCopy).GetCustomAttributes().ToArray();
28+
29+
copyAttributes.Should().BeEquivalentTo(sourceAttributes, options => options.WithStrictOrdering());
30+
}
31+
32+
[Fact]
33+
public void Enum_member_names_in_order_are_identical()
34+
{
35+
const BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly;
36+
37+
string[] sourceNames = typeof(JsonApiEndpoints).GetMembers(bindingFlags).Select(memberInfo => memberInfo.Name).ToArray();
38+
string[] copyNames = typeof(JsonApiEndpointsCopy).GetMembers(bindingFlags).Select(memberInfo => memberInfo.Name).ToArray();
39+
40+
copyNames.Should().BeEquivalentTo(sourceNames, options => options.WithStrictOrdering());
41+
}
42+
43+
[Fact]
44+
public void Enum_member_values_are_identical()
45+
{
46+
IEnumerable<int> sourceValues = Enum.GetValues<JsonApiEndpoints>().Select(value => (int)value).ToArray();
47+
int[] copyValues = Enum.GetValues<JsonApiEndpointsCopy>().Select(value => (int)value).ToArray();
48+
49+
copyValues.Should().BeEquivalentTo(sourceValues, options => options.WithStrictOrdering());
50+
}
51+
}
52+
}

0 commit comments

Comments
 (0)