Skip to content

Commit 3f0f1d9

Browse files
committed
fix tests
1 parent 7cca503 commit 3f0f1d9

File tree

3 files changed

+18
-10
lines changed

3 files changed

+18
-10
lines changed

Diff for: src/JsonApiDotNetCore/Builders/ContextGraphBuilder.cs

+14-7
Original file line numberDiff line numberDiff line change
@@ -174,20 +174,27 @@ public IContextGraphBuilder AddDbContext<T>() where T : DbContext
174174
var (isJsonApiResource, idType) = GetIdType(entityType);
175175

176176
if (isJsonApiResource)
177-
_entities.Add(GetEntity(GetResourceName(property), entityType, idType));
177+
_entities.Add(GetEntity(GetResourceName(property, entityType), entityType, idType));
178178
}
179179
}
180180

181181
return this;
182182
}
183183

184-
private string GetResourceName(PropertyInfo property)
184+
private string GetResourceName(PropertyInfo property, Type resourceType)
185185
{
186-
var resourceAttribute = property.GetCustomAttribute(typeof(ResourceAttribute));
187-
if (resourceAttribute == null)
188-
return property.Name.Dasherize();
189-
190-
return ((ResourceAttribute)resourceAttribute).ResourceName;
186+
// check the class definition first
187+
// [Resource("models"] public class Model : Identifiable { /* ... */ }
188+
if (resourceType.GetCustomAttribute(typeof(ResourceAttribute)) is ResourceAttribute classResourceAttribute)
189+
return classResourceAttribute.ResourceName;
190+
191+
// check the DbContext member next
192+
// [Resource("models")] public DbSet<Model> Models { get; set; }
193+
if (property.GetCustomAttribute(typeof(ResourceAttribute)) is ResourceAttribute resourceAttribute)
194+
return resourceAttribute.ResourceName;
195+
196+
// fallback to dsherized...this should actually check for a custom IResourceNameFormatter
197+
return property.Name.Dasherize();
191198
}
192199

193200
private (bool isJsonApiResource, Type idType) GetIdType(Type resourceType)

Diff for: src/JsonApiDotNetCore/Graph/IResourceNameFormatter.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ public string FormatResourceName(Type type)
3535
{
3636
try
3737
{
38-
var attribute = type.GetCustomAttributes(typeof(ResourceAttribute)).SingleOrDefault() as ResourceAttribute;
39-
if (attribute != null)
38+
if (type.GetCustomAttribute(typeof(ResourceAttribute)) is ResourceAttribute attribute)
4039
return attribute.ResourceName;
4140

4241
return str.Dasherize(type.Name.Pluralize());

Diff for: src/JsonApiDotNetCore/Internal/ContextEntity.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ public class ContextEntity
99
/// <summary>
1010
/// The exposed resource name
1111
/// </summary>
12-
public string EntityName { get; set; }
12+
public string EntityName {
13+
get;
14+
set; }
1315

1416
/// <summary>
1517
/// The data model type

0 commit comments

Comments
 (0)