arangoclient.net icon indicating copy to clipboard operation
arangoclient.net copied to clipboard

Is [DocumentProperty(IgnoreProperty = true)] not supposed to ignore document properties?

Open danludwig opened this issue 9 years ago • 4 comments

Consider the following 4 types:

public abstract class ContactInfo {
  public SomeEnum SomeEnumValue { get; set; }
  [DocumentProperty(IgnoreProperty = true)]
  public abstract string ContactValue { get; }
}

public class ContactEmailInfo : ContactInfo {
  public string EmailAddress { get; set; }
  [DocumentProperty(IgnoreProperty = true)]
  public override string ContactValue => EmailAddress;
}

public class ContactPhoneInfo : ContactInfo {
  public ulong PhoneNumber { get; set; }
  public string Region Code { get; set; }
  [DocumentProperty(IgnoreProperty = true)]
  public override string ContactValue => EmailAddress;
}

public class UserDocument {
  public UserDocument() {
    ContactEmails = new List<ContactEmailInfo>();
    ContactPhones = new List<ContactPhoneInfo>();
  }
  public IList<ContactEmailInfo> ContactEmails { get; }
  public IList<ContactPhoneInfo> ContactPhones { get; }
  [DocumentProperty(IgnoreProperty = true)]
  public IReadOnlyCollection<UserContactInfo> ContactInfos =>
    new ReadOnlyCollection<UserContactInfo>(ContactEmails
      .Cast<UserContactInfo>().Union(ContactPhones).ToArray());
}

When using the client to save a UserDocument, its UserInfos array is being stored in the db json document, essentially duplicating items that are already in the UserEmails and UserPhones arrays. Furthermore each array item has its ContactValue serialized, again duplicating data that is already stored in other differently-named properties.

Am I misunderstanding the API for ignoring CLR properties when serializing to arango via this client, if such a thing is possible?

danludwig avatar Dec 30 '15 22:12 danludwig

@danludwig yes this is a bug, i should look into it. but for now you can use [JsonIgnore] json.net attribute for ignoring members

just be aware you should use embeded json.net [ArangoDB.Client.Common.Newtonsoft.Json.JsonIgnore]

and you dont need JsonIgnore on overrided members

public abstract class ContactInfo
    {
        public SomeEnum SomeEnumValue { get; set; }
        [JsonIgnore]
        public abstract string ContactValue { get; }
    }

    public class ContactEmailInfo : ContactInfo
    {
        public string EmailAddress { get; set; }
        public override string ContactValue => EmailAddress;
    }

    public class ContactPhoneInfo : ContactInfo
    {
        public ulong PhoneNumber { get; set; }
        public string RegionCode { get; set; }
        public override string ContactValue => RegionCode;
    }

    public class UserDocument
    {
        public UserDocument()
        {
            ContactEmails = new List<ContactEmailInfo>();
            ContactPhones = new List<ContactPhoneInfo>();
        }
        public IList<ContactEmailInfo> ContactEmails { get; }
        public IList<ContactPhoneInfo> ContactPhones { get; }

        [JsonIgnore]
        public IReadOnlyCollection<ContactInfo> ContactInfos =>
          new ReadOnlyCollection<ContactInfo>(ContactEmails
            .Cast<ContactInfo>().Union(ContactPhones).ToArray());
    }

ra0o0f avatar Dec 31 '15 06:12 ra0o0f

Perfect, that is what I was looking for. Knowing about the embedded JSON.NET also helped me serialize enum values as string instead of int, using

[ArangoDB.Client.Common.Newtonsoft.Json.JsonConverter(typeof(ArangoDB.Client.Common.Newtonsoft.Json.Converters.StringEnumConverter))]
public SomeEnum SomeEnumValue { get; set; }

danludwig avatar Dec 31 '15 12:12 danludwig

let the issue be open till the bug is resolved

ra0o0f avatar Jan 01 '16 05:01 ra0o0f

In current version (v0.7.60) still having this issue + new problem. [ArangoDB.Client.Common.Newtonsoft.Json.JsonIgnore] can't be used because new ArangoDB has no "Common" namespace so i have used only [Newtonsoft.Json.JsonIgnore] which worked for some properties. New problem: i have following class (for tesing ArangoDB vs MongoDB: `using ArangoDB.Client; //Nuget: ArangoDB.Client by raoof hojat v0.7.60 using MongoDB.Bson; //Nuget: MongoDB.Driver by MongoDB, Inc v2.3.0

public class Person { [DocumentProperty(Identifier = IdentifierType.Key)]
public string Key { get; set; } public string Fullname { get; set; } public int Age { get; set; }

    // MongoDB
    [DocumentProperty(IgnoreProperty = true)]//ignore for ArangoDB - not working - bug https://github.com/ra0o0f/arangoclient.net/issues/34
   [Newtonsoft.Json.JsonIgnore] // proposed workaround for ArangoDB
    public MongoDB.Bson.ObjectId _id { get; set; }

}` and i whanted ArangoDB to ignore "_id"

but after db.Insert<Person>(person); i am catching an exception "Unable to cast object of type 'System.String' to type 'MongoDB.Bson.ObjectId'." Callstack: at lambda_method(Closure , Object , Object ) at ArangoDB.Client.Property.DocumentIdentifierModifier.Modify(Object document, IDocumentIdentifierResult identifiers) at ArangoDB.Client.Collection.ArangoCollection.<InsertAsync>d__10.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at ArangoDB.Client.Collection.ArangoCollection1.<InsertAsync>d__10.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at ArangoDB.Client.Utility.TaskUtils.ResultSynchronizer[T](Task1 task) Also this workaround is not working in all cases. Is there any chance to fix this bug, because i want to give ArangoDB the chance ;) ?

Jenar78 avatar Nov 22 '16 09:11 Jenar78