Skip to content

Exception thrown if relationship name doesn't match navigation property name #354

Closed
@jaredcnance

Description

@jaredcnance

Discovered in v2.4.0-beta3

JsonApiException: N2JsonApi.Models.Item does not contain a relationship named ...
JsonApiDotNetCore.Internal.ContextGraph in GetRelationship

public object GetRelationship<TParent>(TParent entity, string relationshipName)
{
var parentEntityType = entity.GetType();
var navigationProperty = parentEntityType
.GetProperties()
.SingleOrDefault(p => string.Equals(p.Name, relationshipName, StringComparison.OrdinalIgnoreCase));
if (navigationProperty == null)
throw new JsonApiException(400, $"{parentEntityType} does not contain a relationship named {relationshipName}");
return navigationProperty.GetValue(entity);
}

Update

Caused by: https://github.com/json-api-dotnet/JsonApiDotNetCore/pull/352/files#diff-967b2ba5f610a691c8d3f1b7ffbafe72L87

Need to improve API documentation so it is more clear what these methods do.

Also, need to add tests with compound (or just different) attribute names.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions