Senior
What to do if the API returns 200 OK, but the response body does not match the contract?
sobes.tech AI
Answer from AI
If the API returns a status 200 OK, but the response body does not conform to the contract, the following steps should be taken:
- Document the discrepancy: record exactly what does not match the contract (structure, data types, required fields, etc.).
- Notify the API developers: communicate the issue, include examples of responses and expectations.
- Automate contract validation: use contract testing tools (e.g., Pact, Postman tests) to detect such errors early.
- Temporarily handle deviations on the client side: if possible, add validation and error handling to prevent failures.
- Review the contract: it may be outdated or need clarification.
Example of response validation against the contract in a JavaScript test using Jest:
expect(response.status).toBe(200);
expect(response.body).toHaveProperty('userId');
expect(typeof response.body.userId).toBe('number');
// Additional structure and type checks
Thus, it is important not only to check the status but also to validate the response content according to the contract.