Skip to main content

One post tagged with "swagger"

View All Tags

New Feature Announcement - Swagger Contract Validation

ยท 2 min read
Kyrillos Nageh
SHAFT_Engine maintainer
SHAFT_Engine

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โ€‹

BenefitDescription
๐Ÿงช Test reliabilityEnsure tests align with backend changes
๐Ÿ” Catch regressionsCI/CD-ready contract enforcement
โŒ Reduce flakinessEliminate schema mismatch failures
๐Ÿ” API governanceHold your APIs to their contract