Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Validates intercepted requests and responses against local OpenAPI specs and reports any discrepancies, such as undocumented operations, missing required parameters, and request or response bodies that don't match their schemas.
Plugin instance definition
{
"name": "OpenApiValidationPlugin",
"enabled": true,
"pluginPath": "~appFolder/plugins/DevProxy.Plugins.dll",
"configSection": "openApiValidationPlugin"
}
Configuration example
{
"$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v4.1.0/rc.schema.json",
"plugins": [
{
"name": "OpenApiValidationPlugin",
"enabled": true,
"pluginPath": "~appFolder/plugins/DevProxy.Plugins.dll",
"configSection": "openApiValidationPlugin"
},
{
"name": "MarkdownReporter",
"enabled": true,
"pluginPath": "~appFolder/plugins/DevProxy.Plugins.dll"
}
],
"urlsToWatch": [
"https://api.contoso.com/*"
],
"openApiValidationPlugin": {
"$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v4.1.0/openapivalidationplugin.schema.json",
"specFiles": [
"api.yaml"
],
"specsFolder": "specs",
"reportUnmatchedRequests": false
}
}
Configuration properties
| Property | Description | Default |
|---|---|---|
specFiles |
List of relative or absolute paths to OpenAPI spec files (JSON or YAML) to validate requests and responses against | None |
specsFolder |
Relative or absolute path to a folder with OpenAPI spec files. Dev Proxy scans the folder recursively for .json, .yaml, and .yml files |
None |
reportUnmatchedRequests |
Set to true to list in the report the requests that don't match any of the OpenAPI specs |
false |
Specify at least one of specFiles or specsFolder. You can use both. Dev Proxy resolves relative paths against the location of the configuration file. If a spec file or the specs folder doesn't exist, Dev Proxy shows an error and disables the plugin.
Command line options
None
Remarks
The OpenApiValidationPlugin checks if your app and the API it calls follow the API's OpenAPI spec. The plugin validates the requests that your app sends and the responses that the API returns, including responses that Dev Proxy mocks or simulates, for example by using the MockResponsePlugin. This way, you can check that your mocks match the API spec too.
Supported specs
The plugin supports OpenAPI 3.x specs in JSON and YAML format. To match requests to specs, the plugin uses the absolute server URLs defined in the specs. It skips specs that don't define any absolute server URLs. The plugin supports server URLs defined on the document, path, and operation level. Server variables match their documented values, or any value within a single URL path segment.
The plugin replaces variables in the spec contents with the values that you pass to Dev Proxy using the --env option. For example, to replace {tenant} in the spec with contoso, start Dev Proxy with:
devproxy --env tenant=contoso
What the plugin checks
For each request that matches one of the URLs to watch and the server URL of one of the specs, the plugin checks:
- Operations: if the path and HTTP method are documented in the spec. If a path matches multiple path templates, the plugin prefers literal paths over templated paths, for example
/users/meover/users/{id}. - Parameters: if the request includes all required path, query, and header parameters, if the parameter values match their schema type and allowed values, and if the request includes query parameters that aren't documented.
- Request body: if the operation documents a request body, if the request includes a body when it's required, if the content type is documented, and if the body matches its schema.
- Response: if the status code is documented, either explicitly, as a range like
2XX, or asdefault, if the response includes all required headers and their values match their schema, if the content type is documented, and if the body matches its schema.
The plugin validates bodies only for JSON content types (application/json and types ending with +json). For other content types, it only checks that the content type is documented.
To validate JSON bodies, the plugin uses a built-in schema validator that supports the type, nullable, required, properties, additionalProperties, items, enum, allOf, anyOf, oneOf (including discriminator), and $ref keywords. The validator ignores other keywords, like format, minimum, maximum, minLength, and pattern. The validator takes into account the direction of the message: it doesn't require readOnly properties in requests and writeOnly properties in responses.
The plugin skips:
- CORS preflight requests
Accept,Content-Type, andAuthorizationheader parameters- Cookie parameters
- Values of
deepObjectand explodedformobject query parameters - Query parameters that are API keys defined by
apiKeysecurity schemes that apply to the operation - Bodies larger than 5 MB and bodies that Dev Proxy didn't capture, like large streamed responses
- Responses that didn't finish before you stopped recording
The plugin lists skipped bodies and responses in the report under Skipped validations.
Validate oneOf with a discriminator
When a schema with oneOf defines a discriminator, and the value is an object, the plugin uses the value of the discriminator property to select the schema to validate the value against. This way, the plugin doesn't report errors when the value matches more than one of the oneOf schemas.
To select the schema, the plugin uses:
- The schema defined for the discriminator value in
mapping. - The schema in
oneOfwhose component name matches the discriminator value, for exampleCatfor#/components/schemas/Cat. - The schema defined in
defaultMapping(OpenAPI 3.2), when the discriminator property is missing or its value doesn't map to any schema.
Consider the following schema:
components:
schemas:
Pet:
oneOf:
- $ref: '#/components/schemas/Cat'
- $ref: '#/components/schemas/Dog'
discriminator:
propertyName: petType
mapping:
dog: '#/components/schemas/Dog'
For an object with petType set to dog, the plugin validates the object against the Dog schema using the explicit mapping. For an object with petType set to Cat, the plugin validates the object against the Cat schema using its component name.
The plugin reports an error when the discriminator property is missing, when its value isn't a string, or when its value doesn't map to any of the schemas in oneOf. For unknown values, the error lists the allowed values. If oneOf doesn't define a discriminator, the value must match exactly one of the schemas.
Output
While you use your app, Dev Proxy logs the result of validating each request. For a request with issues, Dev Proxy shows a warning for each issue, for example:
OpenAPI response validation: $: Missing required property 'email'
For a request that matches its spec, Dev Proxy shows a message with the matched operation, for example:
Request and response match GET /users/{id}
When you stop recording, the plugin creates a report with the number of validated requests and the issues it found for each request. Each issue shows whether it applies to the request or the response, its location, like a parameter name or a JSON path in the body, and a description. If you set reportUnmatchedRequests to true, the report also lists the requests that don't match any of the specs.
To save the report to a file, add a reporter to your configuration. The plugin supports the MarkdownReporter, PlainTextReporter, and JsonReporter. Following is an example of a report created using the MarkdownReporter:
# OpenAPI validation report
- Validated requests: 3
- Requests with issues: 1
## GET https://api.contoso.com/users/42
- Status code: 200
- Operation: `GET /users/{id}`
- Spec: /Users/user/my-app/api.yaml
|Location|Path|Issue|
|--------|----|-----|
|request|`expand`|Query parameter 'expand' isn't documented|
|response|`$`|Missing required property 'email'|