Skip to content

Optional relationships #40

Closed
Closed
@SonicGD

Description

@SonicGD

Hi. I have optional relationships in my models.

class Model : Identifiable<int>
{
  public override int Id {get;set;}
  public int? FooId {get;set;}
  public int? BarId {get;set;}
  public int? BazId {get;set;}

  public virtual A Foo {get;set;}
  public virtual B Bar {get;set;}
  public virtual C Baz {get;set;}
}

For every model only one of this relationships can exists. When i do query like this

http://localhost:5000/models?include=foo,bar,baz

i catch exception:

System.NullReferenceException: Object reference not set to an instance of an object.
   at System.Object.GetType()
   at JsonApiDotNetCore.Builders.DocumentBuilder._getRelationship(Object entity, String relationshipName)
   at JsonApiDotNetCore.Builders.DocumentBuilder.<>c__DisplayClass8_0.<_addRelationships>b__0(Relationship r)
   at System.Collections.Generic.List`1.ForEach(Action`1 action)
   at JsonApiDotNetCore.Builders.DocumentBuilder._getData(ContextEntity contextEntity, IIdentifiable entity)
   at JsonApiDotNetCore.Builders.DocumentBuilder.Build(IEnumerable`1 entities)
   at JsonApiDotNetCore.Serialization.JsonApiSerializer._serializeDocuments(Object entity, IJsonApiContext jsonApiContext)
   at JsonApiDotNetCore.Formatters.JsonApiOutputFormatter.<WriteAsync>d__1.MoveNext()

As i understand, problem is here - https://github.com/Research-Institute/json-api-dotnet-core/blob/master/src/JsonApiDotNetCore/Builders/DocumentBuilder.cs#L209 cause entity is null.

Should there be check for null? I can make PR if needed.

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