Notă
Accesul la această pagină necesită autorizare. Puteți încerca să vă conectați sau să modificați directoarele.
Accesul la această pagină necesită autorizare. Puteți încerca să modificați directoarele.
Mocks an API based on its OpenAPI spec.
Configuration example
{
"$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v4.1.0/rc.schema.json",
"plugins": [
{
"name": "OpenApiMockResponsePlugin",
"enabled": true,
"pluginPath": "~appFolder/plugins/DevProxy.Plugins.dll",
"configSection": "openApiMock"
}
],
"urlsToWatch": [
"https://api.contoso.com/*"
],
"openApiMock": {
"$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v4.1.0/openapimockresponseplugin.schema.json",
"specFile": "openapi.yaml"
}
}
Configuration properties
| Property | Description | Default |
|---|---|---|
specFile |
Path (relative to the config file) or URL of the OpenAPI spec (JSON or YAML) to mock responses from. Required. | - |
Command line options
None
Remarks
The OpenApiMockResponsePlugin responds to intercepted requests with responses based on the OpenAPI spec that you specify in the specFile property. You don't need to write mocks: the plugin builds responses from the operations defined in the spec.
Spec file
You can load the spec from a local file or from an http or https URL. The plugin supports specs in JSON and YAML. When you use a local file, the plugin watches it for changes and reloads the spec when you update it. If you don't specify specFile, the plugin logs an error and is disabled.
Before parsing the spec, Dev Proxy replaces {name} placeholders in the spec with the values of variables defined in the env setting or passed using the --env option.
Matching requests
The plugin matches requests against the servers and paths defined in the spec. Path parameters, like {id}, match any value. Concrete paths, like /users/me, take precedence over templated paths, like /users/{id}. After matching the path, the plugin matches the request's HTTP method to an operation on that path.
The plugin passes through to the real API requests that match urlsToWatch but don't match a path and operation in the spec. If the spec doesn't define any servers, the plugin logs a warning and can't match any request.
Response status code
The plugin responds with the lowest 2xx status code defined for the operation. If the operation doesn't define a 2xx response, the plugin uses the default response with 200 OK. If the operation doesn't define any responses, the plugin responds with 200 OK.
Response body
For JSON responses, the plugin uses the first available source for the response body:
- Example defined in the spec for the response's media type (
exampleor the first ofexamples). - Response generated by the local language model, if you enabled it. The plugin prompts the language model with the request, the operation's description, and the response schema, so that the response contains realistic values, for example, names in
givenNameand email addresses inmail. The plugin validates the JSON returned by the language model against the response schema. If the language model doesn't return valid JSON, or the JSON doesn't match the schema, the plugin logs a warning with the validation errors and generates the response from the schema. The validation checks types, required properties, properties, additional properties, array items, enums, andallOf,anyOf, andoneOf. It doesn't check other constraints, such asminimum,pattern,format, or string and array lengths. - Response generated from the schema. For each property, the plugin uses the schema's
example,default, or firstenumvalue. Otherwise, it generates a value based on the property'sformatandtype. The plugin follows$ref,allOf,oneOf, andanyOf, generates one item for arrays, and stops at recursive schemas.
The plugin prefers JSON media types when an operation defines several. For text responses, like text/plain, the plugin uses a string example from the spec or generates a value from a string schema. For other media types, like XML, the plugin can only use string examples defined in the spec. If the plugin can't generate a response body, it doesn't mock the request and passes it through to the real API.
To keep responses consistent and fast, the plugin caches generated responses in memory for each operation (method and path). Subsequent requests to the same operation return the cached response without calling the language model again. The plugin clears the cache when the spec changes or when you restart Dev Proxy.
Important
The quality of responses generated by the language model depends on the model that you use. Responses generated from the schema contain placeholder values. To control the exact data that your API returns, define examples in the spec or use the MockResponsePlugin.
Response size limit
To protect Dev Proxy from schemas that produce very large responses, for example because of large minItems or minLength values, the plugin limits responses generated from the schema to 100,000 units. Each JSON value counts as one unit, and string values also count their characters. When a response exceeds the limit, the plugin logs a warning and passes the request through to the real API.
Headers
The plugin sets the Content-Type header to the media type of the response. If the request contains an Origin header, the plugin adds the Access-Control-Allow-Origin: * header. If you use the RateLimitingPlugin, the plugin includes its rate limiting headers in the response.