codecutout / JsonApiSerializer

JsonApiSerializer supports configurationless serializing and deserializing objects into the json:api format (http://jsonapi.org).
MIT License
113 stars 46 forks source link

JsonApiSerializer.Exceptions.JsonApiFormatException #103

Open stesvis opened 5 years ago

stesvis commented 5 years ago

Hello, after updating from v1.7.0 to v1.7.2 I get this exception when I execute JsonConvert.SerializeObject(postData, new JsonApiSerializerSettings()):

JsonApiSerializer.Exceptions.JsonApiFormatException: Expected Id property to be a string or primitive but found it to be ''. The values of the id member MUST be a string

My postData is a class that inherits from a BaseDTO that looks like this:

public class BaseDTO : BindableBase
{
    [JsonIgnore]
    public int Id
    {
        get
        {
            if (int.TryParse(sId, out int id))
            {
                return id;
            }

            return 0;
        }
        set { sId = value.ToString(); }
    }

    /// <summary>
    /// Workaround: json:api only works if id is a string
    /// </summary>
    [JsonProperty("id")]
    internal string sId { get; set; }
}

It always worked up until v1.7.0. What changed, shouldn't that keep working as before at least?

alex-davies commented 5 years ago

😨 Yes that should be working.

1.7.2 was a minor change to fix a bug where int->string conversions on Ids were not occurring on relationships, but it included some refactoring of Id processing code. There must be tests missing around null ids

Will fix this ASAP!

stesvis commented 5 years ago

Thanks @alex-davies ! Yes it has to do with null Ids because it happens when I serialize objects during creation, so the Id is null.

stesvis commented 5 years ago

It also seems to happen in v1.7.0 but only in iOS...

alex-davies commented 5 years ago

Issue should be solved in 1.7.3 (https://github.com/codecutout/JsonApiSerializer/pull/104)

Not sure about your iOS issue, are you using the serializer in xamarin?

alex-davies commented 5 years ago

Also with the later versions of the library you no longer need to convert int ids to string, it will now output ints as strings when serialising

stesvis commented 5 years ago

Thank you! After a "clean solution" i rebuilt it and it worked in iOS on v1.7.0 So i can just leave public int Id {get; set;} ?

stesvis commented 5 years ago

Btw, this is an awesome package, you did a great job! I'll wait for v1.7.3 (still can't see the update)

stesvis commented 5 years ago

Not sure if v1.7.0 was supposed to work with Id as an int only, but i tried and i got this exception:

{System.InvalidCastException: Specified cast is not valid. at (wrapper castclass) System.Object.__castclass_with_cache(object,intptr,intptr) at JsonApiSerializer.Util.WriterUtil.ShouldWriteProperty[T] (System.Object value, Newtonsoft.Json.Serialization.Json…}

I'll try again once i can install v1.7.3