New Feature Announcement - Swagger Contract Validation
ยท 2 min read
Say goodbye to manual schema checksโcontract testing is now automated and built right into SHAFT!โ
๐ก๏ธ What is Contract Validation?โ
Contract testing ensures your API requests and responses follow the defined structure (contract), helping prevent:
- Unexpected field changes
- Data type mismatches
- Missing or extra fields
- Runtime errors in API consumers
With the latest release, SHAFT now integrates Swagger/OpenAPI schema validation for all API tests. It will fail your test automatically if the request or response doesnโt match the OpenAPI spec you provide. ๐ฅ
๐ง How to Enable Itโ
๐ Via custom.propertiesโ
src/main/resources/properties/custom.properties
swagger.validation.enabled=true
swagger.validation.url=https://petstore.swagger.io/v2/swagger.json
๐งช Or via Codeโ
SHAFT.Properties.api.set().swaggerValidationEnabled(true);
SHAFT.Properties.api.set().swaggerValidationUrl("https://petstore.swagger.io/v2/swagger.json");
You can toggle validation dynamically per test or test class.
โ What Gets Validated?โ
- Request structure (body, headers, parameters)
- Response structure (status, body schema)
- Alignment with your OpenAPI/Swagger definition
๐ Sample Testโ
@Test
public void testCreateUserWithContractValidation() {
SHAFT.API api = new SHAFT.API("https://petstore.swagger.io/v2");
String invalidPayload = "[{\"id\":\"INVALID_ID\"}]";
api.post("/user/createWithList")
.setRequestBody(invalidPayload)
.setContentType("application/json");
api.assertThatResponse().statusCode().isEqualTo(400);
}
SHAFT will automatically validate the above request and response against the Swagger schema. โ If anything is off, your test will fail and report the contract violation.
๐ง Why It Mattersโ
| Benefit | Description |
|---|---|
| ๐งช Test reliability | Ensure tests align with backend changes |
| ๐ Catch regressions | CI/CD-ready contract enforcement |
| โ Reduce flakiness | Eliminate schema mismatch failures |
| ๐ API governance | Hold your APIs to their contract |
