javabackwards-compatibilityimmutables-library

Backward compatibility when adding a new field in response


I am using Java Immutables for my request/response classes, which looks something like this:

@JsonDeserialize(builder = ImmutableMyRequest.Builder.class)
@Immutable
public interface MyRequest {
  @Nullable String getA();
  @NotNull String getB();

  ...
}

Now i need to add a new field to my request:

@JsonDeserialize(builder = ImmutableMyRequest.Builder.class)
@Immutable
public interface MyRequest {
  @Nullable String getA();
  @NotNull String getB();
  @Nullable String getC(); // Edit

  ...
}

In my understanding, this should not break the compatibility and the older clients should simply ignore the new fields, but when i use the older version with the new request, it fails:

Unexpected status. Expected: 200. Actual: 400. Response: InboundJaxrsResponse{context=ClientResponse{method=POST, uri=http://some.url.here, status=400, reason=Bad Request}}

I am using javax.ws.rs.client.Invocation.Builder to post my requests.

Any help here?


Solution

  • You are seeing this error because the old version isn't ignoring unknown properties. The older server needs to have @JsonIgnoreProperties(ignoreUnknown = true) as an annotation to the interface in order for it to ignore a property that has been added in a subsequent version.

    If you aren't able to change the old version, there is no way around this issue as the older server is designed to not ignore unknown properties.