jasminb / jsonapi-converter

JSONAPI-Converter is a Java/Android library that provides support for working with JSONAPI spec
Apache License 2.0
272 stars 81 forks source link

Creating a resource on the server - required id field #249

Closed scotzark closed 2 years ago

scotzark commented 3 years ago

Hi-

I have a server implementation that I am talking to that does not allow the 'id' field to be present in the created resource. It returns an error code. The developer of the server has pointed to this part of the JSON API spec:

"Exception: The id member is not required when the resource object originates at the client and represents a new resource to be created on the server."

I do not see a way to tell the ResourceConverter to exclude the ID field on serialization, only on deserialization. Have you run into this issue before?

jasminb commented 3 years ago

Hello @scotzark,

Here is a unit test that demonstrates a working example for your use-case:

    @Test
    public void testSerializeWithoutId() throws DocumentSerializationException {
        User user = new User();
        user.setName("Name");

        byte [] data = converter.writeDocument(new JSONAPIDocument<>(user));

        System.out.println(new String(data));
    }

And it produces:

{
  "data": {
    "type": "users",
    "attributes": {
      "name": "Name"
    },
    "relationships": {
      "statuses": {}
    }
  }
}

If you are producing new resource to be stored, just make sure that the id attribute is null when converting and posting it to the server.

jasminb commented 3 years ago

Hello again @scotzark,

I've addressed the issue in following commit 70e28cab11d9096e80c8ea958b2f4902bd62f32e which allows explicitly disabling id serialization without having to resort to setting it to null.

New release will be out in couple of days, in the meantime you can use SNAPSHOT version.