Skip to main content

oas-validator

Validates requests against an OpenAPI document; responses are not checked.

Category TrafficAdd it to Route, Service, Consumer, Consumer group, Global rule, Plugin configEngine plugin oas-validatorPriority 512

What it does​

oas-validator reads each request on the route against an OpenAPI document before it is passed on. The document is Written here, under OpenAPI document, or Fetched from a URL, under Document URL. A request the document does not allow is answered with Rejection status code, 400 unless you change it.

Only the request is checked, never what the backend answers. A request to a path the document does not list is refused too, so the document has to describe every path the route takes.

When to use it​

  • An orders API that already publishes its OpenAPI description, where a call to /orders without a customer_id should stop at the gateway.
  • A description the backend team keeps and serves at an address of its own, fetched under Document URL so the route follows each new version.
  • Partners who build against your published description and should be told which parameter they got wrong, with Say why the request was refused on.
  • A backend that fails on calls it was never designed for, kept behind the same description its clients are given.

Not for checking the body or the headers against a JSON Schema of your own — see request-validation (Validates request headers and body against a JSON Schema).

Set it up​

Refuse requests that do not match the API description written here​

Under Basics choose Written here and set OpenAPI document to { "openapi": "3.0.0", "info": { "title": "Orders", "version": "1.0.0" }, "paths": { "/orders": { "get": { "parameters": [ { "name": "customer_id", "in": "query", "required": true, "schema": { "type": "string" } } ], "responses": { "200": { "description": "the customer's orders" } } } } } }. Under Behavior set Rejection status code to 422 and turn on Say why the request was refused.

oas-validator · as the console saves it
{
"spec": "{\n \"openapi\": \"3.0.0\",\n \"info\": {\n \"title\": \"Orders\",\n \"version\": \"1.0.0\"\n },\n \"paths\": {\n \"/orders\": {\n \"get\": {\n \"parameters\": [\n {\n \"name\": \"customer_id\",\n \"in\": \"query\",\n \"required\": true,\n \"schema\": {\n \"type\": \"string\"\n }\n }\n ],\n \"responses\": {\n \"200\": {\n \"description\": \"the customer's orders\"\n }\n }\n }\n }\n }\n}",
"rejection_status_code": 422,
"verbose_errors": true
}

More scenarios​

Fetch the API description from a URL and refuse requests that do not match it

Under Basics choose Fetched from a URL and set Document URL to https://specs.example.com/orders/openapi.json. Under Behavior set Rejection status code to 422 and turn on SSL verify.

oas-validator · as the console saves it
{
"spec_url": "https://specs.example.com/orders/openapi.json",
"ssl_verify": true,
"rejection_status_code": 422
}
Fetch the description again every ten minutesenvironment-wide settings

Set How long a fetched document is kept to 600 seconds.

oas-validator environment-wide settings · as the console saves it
{
"spec_url_ttl": 600
}

Things to know​

  • OpenAPI document — In the example the document makes customer_id a required query parameter of /orders. A request without it is answered 422 naming the missing parameter, and /orders?customer_id=42 is answered 200.
  • OpenAPI document — Stored as JSON and read as an OpenAPI document only at the first request: one that is not a document of that shape is stored without a word and answers 500.
  • Document URL — A document fetched from an address is applied the same way. In the example a request without customer_id is answered 422, and one that carries it is answered 200.
  • A configuration that carries both OpenAPI document and Document URL is refused when you save.
  • Refuse a request the document does not allow — Turned off, a request the document does not allow is sent to the upstream anyway and the status below is never used.
  • Rejection status code — A status below 400, 399 for example, is refused when you save.
  • Say why the request was refused — Off, the refusal carries only failed to validate request, whatever failed.
  • AdviceCheck the document with the OpenAPI tools your team already uses before you save it, and turn on SSL verify when it is fetched over https.

Settings​

The fields as the console shows them, section by section.

Basics​

Choose one:

  • Written hereKeep the OpenAPI document in this configuration.
  • Fetched from a URLRead the OpenAPI document from an address the gateway can reach.

Written here​

OpenAPI documentspecstringRequired

The OpenAPI document, written as JSON (YAML is refused). A request to a path it does not list is refused.

at least 1 character

Fetched from a URL​

Document URLspec_urlstringRequired

The address the OpenAPI document is fetched from. It is cached for the time set in the plugin's metadata.

an http or https address
pattern^https?://
· example http://specs.internal/orders/openapi.json

Behavior​

Refuse a request the document does not allowreject_if_not_matchbooleandefault true
Rejection status coderejection_status_codeintegerdefault 400

The status a refused request is answered with.

400 to 599
Skip body validationskip_request_body_validationbooleandefault false
Skip header validationskip_request_header_validationbooleandefault false
Skip query parameter validationskip_query_param_validationbooleandefault false
Skip path parameter validationskip_path_params_validationbooleandefault false
Say why the request was refusedverbose_errorsbooleandefault false

On, the refusal names the parameter or property that failed. Off, it only says the request did not validate.

Headers sent with the fetchspec_url_request_headersobject

Headers sent when the document is fetched, such as an Authorization for a protected address. Stored on the engine and shown only to roles that manage this object. An object whose keys are any name, each with a string as its value.

Timeouttimeoutintegerdefault 10000

How long the fetch above may take. A fetch that does not finish leaves the request answered with 500.

1000 to 60000 · in milliseconds
SSL verifyssl_verifybooleandefault false

Used only while the document is fetched. Off, an https document is taken whatever certificate the server shows.

Advanced​

Request body limitmax_req_body_sizeintegerdefault 67108864

Bytes of the request body read before it is checked. A longer body is answered with 500 and never reaches the upstream.

at least 1

Environment-wide settings​

Set once per environment, under Plugins › oas-validator. Every oas-validator in the environment uses these.

How long a fetched document is keptspec_url_ttlintegerdefault 3600

One setting for the whole gateway: every route that fetches its document from a URL re-fetches it once this much time has passed.

at least 1 · in seconds

Every plugin also has Enabled, Priority, Filter, Error response and Pre-function: see Plugin execution settings.

G8 dev · Apache APISIX® 3.19.0
Apache, Apache APISIX and APISIX are trademarks of The Apache Software Foundation.
Facts
Category
Traffic
Add it to
Route, Service, Consumer, Consumer group, Global rule, Plugin config
Engine plugin
oas-validator
Priority
512