jsonapi-converter icon indicating copy to clipboard operation
jsonapi-converter copied to clipboard

Ability to create resource identifier objects

Open Leonti opened this issue 7 years ago • 4 comments

Hi!

Json Api standard specifies the way to update relationships via a PATCH request(http://jsonapi.org/format/#crud-updating-relationships), for example: PATCH /articles/1/relationships/author

{
  "data": { "type": "people", "id": "12" }
}

My entity, which is meant to be a resource identifier object looks like this:

@Type("role")
@JsonIgnoreProperties(ignoreUnknown = true)
public class RoleDto {
  @Id
  public final String id;

  @JsonCreator
  public RoleDto(
    @JsonProperty("id") String id) {
    this.id = id;
  }
}

This is being serialized into:

{
  "data": {
      "type": "role",
      "id": "partner-standard-user-role-id",
      "attributes": {
      }
    }
}

Would it be possible to not include empty attributes field? Maybe with annotation ResourceIdentifierObject - when present it would only include id, type and meta if present. Or serialization option NO_EMPTY_ATTRIBUTES?

Cheers!

Leonti avatar Nov 01 '17 07:11 Leonti

Something like this

if (attributesNode.size() == 0 && SerializationFeature.NO_EMPTY_ATTRIBUTES) {
    dataNode.remove(ATTRIBUTES);
}

would work.

Should i submit a PR for it?

thinh76 avatar Nov 01 '17 10:11 thinh76

deleted my comment... I overlooked you first sentence.

cljk avatar Nov 03 '17 19:11 cljk

Hello @thinh76,

Let me take a look and get back to you.

jasminb avatar Nov 04 '17 01:11 jasminb

Hello @thinh76,

Feel free to submit a PR with a change to support your requirement.

Please make sure that you are using tabs for indentation and that you add unit tests to cover the added changes, also make sure that all tests pass (mvn clean install).

jasminb avatar Nov 22 '17 08:11 jasminb