Video in progress

Staff 🧪 Testing

How do you automatically detect that backward compatibility of a public API has been broken?

I generate the OpenAPI specification from the code, diff it against the version on main in every PR, and stop the build on breaking changes (removing a field, adding a required parameter, changing a type). I add contract tests that work with old client versions, and serialization snapshot tests.

📖 Answer

Someone renames a field in a DTO or adds @JsonInclude(NON_NULL): the code compiles, the tests are green, and the old version of the mobile app crashes. Mobile clients aren't updated for months, so on a public API this is serious.

The automatic layers of protection:

  1. Spec diff. springdoc produces openapi.json at build time, and CI compares it with the version on main using a tool like oasdiff or openapi-diff. Breaking changes: removing an endpoint or a response field, adding a required field to a request, narrowing a type, removing an enum value. They fail the build unless the PR has a deliberate "breaking" mark and a new version.

  2. What the spec doesn't capture. The spec doesn't fully reflect how a null value is serialized, the date format or number precision. For that, JSON snapshot tests for the main DTOs:

@Test
void courseResponseShapeIsStable() throws Exception {
    String json = mapper.writeValueAsString(CourseResponses.sample());
    assertThatJson(json).isEqualTo(readResource("snapshots/course-response-v1.json"));
}

If the snapshot changes, the diff shows up in the PR and the reviewer is asked for a deliberate approval.

  1. Consumer contracts. Pact for internal consumers; for public clients, the test suite of old client SDK versions run against the new server.

  2. Policy. Adding is safe; removing happens only with a deprecation period and a version. A usage metric for deprecated fields (which client is still asking for them) makes the removal decision data-driven.

One subtle point: "clients ignore unknown fields" is only an assumption. Some clients parse strictly. An extra field can sometimes be breaking too, and contract tests show that as well.