Overview

7 API conformance: Schema testing

The chapter explains why API schema testing remains valuable even when an OpenAPI definition is generated from service code. Real systems can behave in ways that code-generated documentation does not capture, especially when legacy constraints, gateway behavior, or unexpected server errors intervene. Schema testing uses the published OpenAPI document as the source of truth to verify that the actual API response matches the documented structure, headers, and content type, helping ensure that the provider behaves as described for consumers.

It then presents three practical ways to do this. First are code-based schema tests, which can validate either the full request path through the stack or only the controller layer in isolation; these can be precise and fast at the controller level, but end-to-end versions are slower and harder to maintain. Second are generated schema tests, where tools such as Schemathesis and Portman create many tests directly from the OpenAPI definition, including edge cases and fuzzed inputs. These tools can reveal problems such as undocumented response fields, and they benefit from good examples in the API definition to drive meaningful test generation.

Finally, the chapter shows how a validating proxy such as Prism can be inserted between tests and the upstream API to enforce schema conformance without rewriting all existing end-to-end tests. This is an easy way to add response validation, though coverage is only as broad as the scenarios the tests already exercise. The chapter closes by comparing the approaches and distinguishing schema testing from consumer-driven contract testing: schema tests verify that responses conform to the published API shape, while contract tests verify a deeper, example-driven agreement between specific consumers and providers over time.

General structure of a schema test
figure
A schema test can be against the entire stack or just at the controller level of an API service.
figure
Generating schema tests from an OpenAPI definition
figure
Using a validating proxy for API conformance testing
figure
Consumer-driven contract testing
figure

Summary

  • Schema tests are for checking that APIs conform to their API definition in situations where you can’t apply consumer-driven contract tests, for example, for external or public APIs.
  • With code-based schema tests, you write test code that makes assertions which compare the data type of the message returned from the provider with the schema in its published API definition.
  • Code-based schema tests can either be run end to end or at the controller level of an API service. Controller-level tests are faster and avoid the drawbacks of end-to-end tests.
  • Generating schema tests from an OpenAPI definition avoids having developers write code when there is a change to the API. Tools such as Schemathesis and Portman can generate and run happy and sad path scenarios based on the API definition and run these against the API server.
  • A schema validation proxy can sit between your automated test suite and your API server and validate that the response from the server matches the schema. It’s a quick method to get started with schema testing because you can fit the proxy into your existing suite of end-to-end tests.
  • Using a combination of both generating OpenAPI definitions and schema testing helps ensure API conformance for your external APIs.

FAQ

What is schema testing in the context of API conformance?Schema testing checks that an API’s actual requests or responses conform to the OpenAPI schema published in the API definition. It verifies things like data types, required fields, allowed properties, content type, and headers.
Why is schema testing still useful if OpenAPI is generated from the service code?Even when OpenAPI is generated from service code, the running application may behave differently in real conditions. Schema testing adds another verification layer to catch mismatches between the published API reference and actual API behavior.
What kinds of issues can schema tests detect?Schema tests can detect missing required fields, extra undocumented properties, invalid data types, violations of length or format rules, and mismatches in response content type or headers.
How do code-based schema tests differ from generated schema tests?Code-based schema tests are written manually, either with explicit assertions or by validating against the OpenAPI file in test code. Generated schema tests are created automatically from the OpenAPI definition and can cover many happy and sad paths without hand-writing each test.
What is the difference between an end-to-end schema test and a controller schema test?An end-to-end schema test runs against the whole stack, including gateway and deployed service, while a controller schema test runs only against the controller layer with dependencies mocked out. Controller tests are usually much faster, but end-to-end tests cover more of the real system.
Why is additionalProperties: false important in an OpenAPI schema?It prevents the API from returning unexpected fields that are not documented in the schema. This helps detect undocumented data, reduces ambiguity for consumers, and can also help surface security or implementation issues.
What tools are mentioned for code-based schema testing?The chapter uses Atlassian’s Swagger Request Validator library with Rest Assured for Java-based schema tests. It also mentions other options such as Hikaku and jest-openapi.
What is Schemathesis used for in schema testing?Schemathesis is a property-based testing tool that generates API tests from an OpenAPI definition. It can fuzz inputs, run happy and sad path checks, and validate the response schema, status code, content type, and headers.
How does Portman help with API schema testing?Portman generates and executes Postman collections from an OpenAPI definition. It can add conformance checks such as status code, schema, content type, JSON body, and headers, and it can be integrated into CI/CD pipelines.
How are schema tests different from consumer-driven contract tests?Schema tests verify that an API provider conforms to its published OpenAPI schema. Consumer-driven contract tests verify that a provider and consumer share an expected message interaction pattern, usually using concrete examples and collaboration between teams. Contract tests are broader and more interaction-focused.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Automating API Delivery Automating API Delivery ebook for free
Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Automating API Delivery Automating API Delivery ebook for free
Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Automating API Delivery Automating API Delivery ebook for free