Skip to content

Commit b6df7c6

Browse files
committed
add additional parameter to relationshipattribute to allow for specifying the internal name instead of only inferring it from the dto variable name, as that may not match the entity relationship name
1 parent afee0fe commit b6df7c6

File tree

6 files changed

+13
-8
lines changed

6 files changed

+13
-8
lines changed

src/Examples/JsonApiDotNetCoreExample/Models/Person.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class Person : Identifiable, IHasMeta
2121
[HasMany("todo-collections")]
2222
public virtual List<TodoItemCollection> TodoItemCollections { get; set; }
2323

24-
[HasOne("unincludeable-item", Link.All, canInclude: false)]
24+
[HasOne("unincludeable-item", documentLinks: Link.All, canInclude: false)]
2525
public virtual TodoItem UnIncludeableItem { get; set; }
2626

2727
public Dictionary<string, object> GetMeta(IJsonApiContext context)

src/JsonApiDotNetCore/Builders/ContextGraphBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ protected virtual List<RelationshipAttribute> GetRelationships(Type entityType)
125125
{
126126
var attribute = (RelationshipAttribute)prop.GetCustomAttribute(typeof(RelationshipAttribute));
127127
if (attribute == null) continue;
128-
attribute.InternalRelationshipName = prop.Name;
128+
if (attribute.InternalRelationshipName == null) attribute.InternalRelationshipName = prop.Name;
129129
attribute.Type = GetRelationshipType(attribute, prop);
130130
attributes.Add(attribute);
131131
}

src/JsonApiDotNetCore/Models/HasManyAttribute.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ public class HasManyAttribute : RelationshipAttribute
77
/// </summary>
88
///
99
/// <param name="publicName">The relationship name as exposed by the API</param>
10+
/// <param name="internalName">The relationship name as defined in the entity layer, if not provided defaults to the variable name</param>
1011
/// <param name="documentLinks">Which links are available. Defaults to <see cref="Link.All"/></param>
1112
/// <param name="canInclude">Whether or not this relationship can be included using the <c>?include=public-name</c> query string</param>
1213
///
@@ -21,8 +22,9 @@ public class HasManyAttribute : RelationshipAttribute
2122
/// </code>
2223
///
2324
/// </example>
24-
public HasManyAttribute(string publicName, Link documentLinks = Link.All, bool canInclude = true)
25-
: base(publicName, documentLinks, canInclude)
25+
public HasManyAttribute(string publicName, string internalName = null, Link documentLinks = Link.All,
26+
bool canInclude = true)
27+
: base(publicName, internalName, documentLinks, canInclude)
2628
{ }
2729

2830
/// <summary>

src/JsonApiDotNetCore/Models/HasOneAttribute.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ public class HasOneAttribute : RelationshipAttribute
77
/// </summary>
88
///
99
/// <param name="publicName">The relationship name as exposed by the API</param>
10+
/// <param name="internalName">The relationship name as defined in the entity layer, if not provided defaults to the variable name</param>
1011
/// <param name="documentLinks">Which links are available. Defaults to <see cref="Link.All"/></param>
1112
/// <param name="canInclude">Whether or not this relationship can be included using the <c>?include=public-name</c> query string</param>
1213
/// <param name="withForiegnKey">The foreign key property name. Defaults to <c>"{RelationshipName}Id"</c></param>
@@ -24,8 +25,9 @@ public class HasOneAttribute : RelationshipAttribute
2425
/// </code>
2526
///
2627
/// </example>
27-
public HasOneAttribute(string publicName, Link documentLinks = Link.All, bool canInclude = true, string withForiegnKey = null)
28-
: base(publicName, documentLinks, canInclude)
28+
public HasOneAttribute(string publicName, string internalName = null, Link documentLinks = Link.All,
29+
bool canInclude = true, string withForiegnKey = null)
30+
: base(publicName, internalName, documentLinks, canInclude)
2931
{
3032
_explicitIdentifiablePropertyName = withForiegnKey;
3133
}

src/JsonApiDotNetCore/Models/RelationshipAttribute.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ namespace JsonApiDotNetCore.Models
44
{
55
public abstract class RelationshipAttribute : Attribute
66
{
7-
protected RelationshipAttribute(string publicName, Link documentLinks, bool canInclude)
7+
protected RelationshipAttribute(string publicName, string internalName, Link documentLinks, bool canInclude)
88
{
99
PublicRelationshipName = publicName;
1010
DocumentLinks = documentLinks;
1111
CanInclude = canInclude;
12+
InternalRelationshipName = internalName;
1213
}
1314

1415
public string PublicRelationshipName { get; }

test/UnitTests/Builders/DocumentBuilder_Tests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ private class Model : Identifiable
233233
{
234234
[Attr("StringProperty")] public string StringProperty { get; set; }
235235

236-
[HasOne("related-model", Link.None)]
236+
[HasOne("related-model", documentLinks: Link.None)]
237237
public RelatedModel RelatedModel { get; set; }
238238
public int RelatedModelId { get; set; }
239239
}

0 commit comments

Comments
 (0)