Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use Guids as id's #96

Merged
merged 2 commits into from
Apr 24, 2016
Merged

use Guids as id's #96

merged 2 commits into from
Apr 24, 2016

Conversation

eltjo-k
Copy link
Contributor

@eltjo-k eltjo-k commented Apr 24, 2016

The use of Guids as id's leads to the following types of exception when id's are converted to strings:

{System.InvalidCastException: Object must implement IConvertible.
   at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
   at Newtonsoft.Json.Linq.Extensions.Convert[T,U](T token)
   at Newtonsoft.Json.Linq.Extensions.Value[T,U](IEnumerable`1 value)
   at Newtonsoft.Json.Linq.Extensions.Value[U](IEnumerable`1 value)
   at Saule.Serialization.ResourceSerializer.<>c__DisplayClass23_0.<GetRelationshipData>b__0(IDictionary`2 props)
   at Saule.Serialization.ResourceSerializer.SerializeArrayOrObject(JToken token, Func`2 serializeObj)
   at Saule.Serialization.ResourceSerializer.GetRelationshipData(ResourceRelationship relationship, JToken relationshipValues)
   at Saule.Serialization.ResourceSerializer.SerializeRelationship(ResourceRelationship relationship, IDictionary`2 properties)
   at Saule.Serialization.ResourceSerializer.SerializeRelationships(IDictionary`2 properties)
   at Saule.Serialization.ResourceSerializer.SerializeData(IDictionary`2 properties)
   at Saule.Serialization.ResourceSerializer.SerializeArrayOrObject(JToken token, Func`2 serializeObj)
   at Saule.Serialization.ResourceSerializer.Serialize(JsonSerializer serializer)
   at Saule.JsonApiSerializer.Serialize(PreprocessResult result)
   at Saule.JsonApiSerializer`1.Serialize(Object object, Uri requestUri)
   at ...

This happens with:

  • collections of objects with Guid-ids
  • single objects with belongsTo-relations to objects with Guid-ids
  • single objects with hasMany-relations to objects with Guid-ids.

Unit tests for these cases have been added.

These exceptions can be solved by replacing occurences of JToken.Value<string>() by cast operators that are explicitly defined for JToken's (see here). This cast operator throws exceptions if the JToken does not contain the proper datatypes as defined in this list, which seems to contain every type that might be used as an index. Finally it uses Convert.ToString(...) which first tries to convert through IConvertible, just like the JToken.Value<string>()-conversion method does, so this change should not break any existing working behaviour.

@joukevandermaas
Copy link
Owner

Awesome work, thanks! I will create 1.4.2 with this fix in it.

@joukevandermaas joukevandermaas merged commit 2f27039 into joukevandermaas:master Apr 24, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants