OpenApiSpecGeneratorPlugin

Generates OpenAPI spec in JSON or YAML format from the intercepted requests and responses.

Screenshot of two command prompt windows. One shows Dev Proxy recording API requests. The other shows the generated OpenAPI spec.

Configuration example

{
  "$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v4.0.0/rc.schema.json",
  "plugins": [
    {
      "name": "OpenApiSpecGeneratorPlugin",
      "enabled": true,
      "pluginPath": "~appFolder/plugins/DevProxy.Plugins.dll",
      "configSection": "openApiSpecGeneratorPlugin"
    }
  ],
  "openApiSpecGeneratorPlugin": {
    "$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v4.0.0/openapispecgeneratorplugin.schema.json",
    "includeOptionsRequests": false,
    "ignoreResponseTypes": false,
    "specVersion": "v3_0",
    "specFormat": "Json",
    "includeParameters": [
      "api-version"
    ]
  }
}

Configuration properties

Property Description Default
includeOptionsRequests Determines whether to include OPTIONS requests in the generated OpenAPI spec false
ignoreResponseTypes Determines whether to ignore response types in the generated OpenAPI spec. false
specVersion Determines which version to use for the generated OpenAPI spec. Can be set to v2_0 or v3_0 v3_0
specFormat Determines which format to use for the generated OpenAPI spec. Can be set to Json or Yaml Json
includeParameters Determines the list of query string parameters to mark as required in the generated OpenAPI spec, along with their default values taken from the recorded requests. Dev Proxy includes other query string parameters as optional. []

Command line options

None

Remarks

Parameters, headers, and responses

For each recorded request, the OpenApiSpecGeneratorPlugin documents:

  • Path parameters: path segments that look like identifiers, such as numbers or GUIDs, are replaced with parameters named after the preceding segment, for example /users/{users-id}. Path parameters are required.
  • Query string parameters: parameters from the query string. Parameters listed in includeParameters are required and get the recorded value as their default. All other query string parameters are optional.
  • Request headers: non-standard request headers as optional header parameters. The plugin skips standard HTTP headers and headers that carry credentials.
  • Request body: the schema of the request body, inferred from the recorded request.
  • Responses: the status code and description, non-standard response headers, and the schema of the response body, inferred from the recorded response.

When you call the same operation multiple times, the plugin merges parameters, response headers, and responses from all recorded requests.

Authentication

The OpenApiSpecGeneratorPlugin detects how each recorded request authenticates, based on its headers and query string, and describes it in the generated spec. It adds security schemes to components.securitySchemes (securityDefinitions in OpenAPI v2.0) and a security requirement to each operation.

Recorded credential Security scheme name Security scheme
API key in a request header, such as api-key, x-api-key, or Authorization: Basic ... ApiKeyAuth_header_<name> apiKey in header
API key in the query string, such as api_key, apikey, code, key, sig, or subscription-key ApiKeyAuth_query_<name> apiKey in query
Authorization: Bearer with an opaque token BearerAuth http with the bearer scheme
Authorization: Bearer with a JWT not issued by Microsoft Entra ID BearerAuth http with the bearer scheme and JWT bearer format
Authorization: Bearer with a JWT issued by Microsoft Entra ID EntraOAuth2Auth oauth2

For Microsoft Entra ID tokens, the plugin creates an oauth2 security scheme with:

  • an authorizationCode flow for delegated tokens, which contain scopes (scp claim). For APIs other than Microsoft Graph, the plugin prefixes scopes with the API's resource identifier taken from the token's audience, except for OpenID Connect scopes like openid and offline_access.
  • a clientCredentials flow for app-only tokens, which don't contain scopes. For v2.0 tokens, the plugin uses the {resource}/.default scope. Because app roles can't be requested as scopes, the plugin lists the app roles from the token's roles claim in the security scheme's description.
  • authorization, token, and refresh URLs matching the token version (v1.0 or v2.0) and tenant.

The plugin combines the scopes from all recorded requests to the same operation. If you call an operation using different types of authentication, the plugin lists each of them as an alternative security requirement. If you also call the operation successfully without credentials, the plugin adds an empty security requirement to show that authentication is optional.

The plugin never includes credentials in the generated spec. It doesn't document headers that carry credentials as parameters and it excludes query string parameters that carry credentials, such as api_key, access_token, or sig, even if you list them in includeParameters.

When you generate an OpenAPI v2.0 spec, the plugin describes bearer tokens as an apiKey scheme for the Authorization header, because OpenAPI v2.0 doesn't support the http bearer scheme. Because OpenAPI v2.0 allows only one flow per security scheme, the plugin creates a separate EntraOAuth2Auth_clientCredentials security scheme for app-only tokens.

Use a local language model

To create better OpenAPI specs, consider using a local language model with Dev Proxy. Using a local language model, the OpenApiSpecGeneratorPlugin generates clearer operation IDs and descriptions, giving you a better starting point for your OpenAPI spec. To use a local language model with the OpenApiSpecGeneratorPlugin, enable the language model in the configuration file. For more information, see Use a local language model.

After you create an OpenAPI spec, you can use it to check if your app's requests and the API's responses follow the spec by using the OpenApiValidationPlugin.

Next step