Gremlin.Net.CosmosDb
Gremlin.Net.CosmosDb copied to clipboard
Vertices containing arrays of objects - part 2
Unfortunately, (closed) issue #26 is still seems to be a problem, sorry.
Exceptions are still thrown when you try to save a vertex where a property is an array of objects. This seems fine if it's an array of simple types like integers or strings. But if you write your own class and try to store an array (or collection) of those objects it will fail with an exception.
For repo steps, see issue #26, the code posted there will still fail.
Thanks.
Out of interest, I noticed that this is a problem even when the property is not an array or list. Consider the following:
[Label("person")]
public class PersonVertex : VertexBase
{
public int[] Ages { get; set; }
public string Name { get; set; }
public PersonLink Link { get; set; }
}
public class PersonLink
{
public int Reference { get; set; }
public string Name { get; set; }
}
So ... in this case, the Link
property is just a single PersonLink
object. If we now store a PersonVertex
in the graph, like this:
var personV = new PersonVertex
{
Ages = new[] { 4, 6, 23 },
Id = "person-12345",
Label = "some-label",
Name = "my name",
Link = new PersonLink { Name = "ExternalLink", Reference = 1234 }
};
var test = g.AddV(personV).As("person");
var resp = await graphClient.QueryAsync(test);
...this is interesting, because the vertex will be stored in the graph, but the code will also throw an exception:
Error converting value "{"Reference":1234,"Name":"ExternalLink"}" to type 'GremlinSample.Schema.PersonLink'. Path 'Link'.
...and there is an inner exception:
Could not cast or convert from System.String to GremlinSample.Schema.PersonLink.
Despite the exception, if you look in the database, the vertex will be there. So I'm guessing there is a serialization issue when you try to store properties other than the built in types. I thought this information might help. Thanks!
Hmm. I'm not exactly sure how this should be solved. My team and I are just handling the serialization/de-serialization of complex properties on our own before submitting the vertices we're saving to the graph, so this isn't a feature I'll be able to invest time into at the moment.
My best guess is that you'll need to update the Gremlin.Net.CosmosDb.Serializaton.VertexBaseJsonConverter
(and probably the EdgeBaseJsonConverter
) to correctly convert serialized string property values to its object/array de-serialized equivalent if/when the property it's saturating is not a string or string-like property (date/time, url, GUID, etc.). I would do this work within the ConvertPropertiesObject
method where you have access to the object contract.
Sorry I can't help more than that at the moment. It would be really cool to get this fully working if you can spare any time!
Thanks. I totally understand. You have been very helpful already. I would love to spend more time on this, but I have limited time to invest as well. Let me see what I can do. Our dev team is currently trying to decide if we're happy to use CosmosDB in production, if we go ahead then I will probably be able to put more effort into this. It would be a very cool feature.