← All Exercises

Detect Breaking Changes Between API Contracts

intermediatejavascript~25min

Background

Consumers break on removals and unexpected requirements, not on additive fields. Encoding those three rules makes a compatibility gate possible in CI.

Problem

Compare an old and a new contract and return breaking changes: removed paths, removed response fields, and newly required request parameters.

Input

JSON object: { oldSpec: { paths, fields, required }, newSpec: { paths, fields, required } }

Output

JSON array of "<kind>:<name>" strings, sorted

Sample tests

Sample 1

Input: {"oldSpec":{"paths":["/users","/users/{id}"],"fields":["id","name"],"required":[]},"newSpec":{"paths":["/users"],"fields":["id"],"required":["tenantId"]}}
Expected: ["field-removed:name","path-removed:/users/{id}","required-added:tenantId"]

Sample 2

Input: {"oldSpec":{"paths":["/users"],"fields":["id"],"required":[]},"newSpec":{"paths":["/users","/teams"],"fields":["id","email"],"required":[]}}
Expected: []

Your solution

Learning objective

Gate contract changes on consumer-visible breakage.