Skip to main content

One post tagged with "openapi"

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")
.perform();

api.assertThatResponse().statusCode().isEqualTo(400).perform();
}

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