Plug points
Swagger UI exposes most of its internal logic through the plugin system.
Often, it is beneficial to override the core internals to achieve custom behavior.
Note: Semantic Versioning
Swagger UI’s internal APIs are not part of our public contract, which means that they can change without the major version change.
If your custom plugins wrap, extend, override, or consume any internal core APIs, we recommend specifying a specific minor version of Swagger UI to use in your application, because they will not change between patch versions.
If you’re installing Swagger UI via NPM, for example, you can do this by using a tilde:
fn.opsFilter
When using the filter
option, tag names will be filtered by the user-provided value. If you’d like to customize this behavior, you can override the default opsFilter
function.
For example, you can implement a multiple-phrase filter:
Logo component
While using the Standalone Preset the SwaggerUI logo is rendered in the Top Bar.
The logo can be exchanged by replacing the Logo
component via the plugin api:
JSON Schema components
In swagger there are so called JSON Schema components. These are used to render inputs for parameters and components of request bodies with application/x-www-form-urlencoded
or multipart/*
media-type.
Internally swagger uses following mapping to find the JSON Schema component from OpenAPI Specification schema information:
For each schema’s type(eg. string
, array
, …) and if defined schema’s format (eg. ‘date’, ‘uuid’, …) there is a corresponding component mapping:
If format defined:
Fallback if JsonSchema_${type}_${format}
component does not exist or format not defined:
Default:
With this, one can define custom input components or override existing.
Example Date-Picker plugin
If one would like to input date values you could provide a custom plugin to integrate react-datepicker into swagger-ui. All you need to do is to create a component to wrap react-datepicker accordingly to the format.
There are two cases:
-
The resulting name for mapping to succeed:
JsonSchema_string_date
-
The resulting name for mapping to succeed:
JsonSchema_string_date-time
This creates the need for two components and simple logic to strip any time input in case the format is date:
Request Snippets
SwaggerUI can be configured with the requestSnippetsEnabled: true
option to activate Request Snippets.
Instead of the generic curl that is generated upon doing a request. It gives you more granular options:
- curl for bash
- curl for cmd
- curl for powershell
There might be the case where you want to provide your own snipped generator. This can be done by using the plugin api.
A Request Snipped generator consists of the configuration and a fn
,
which takes the internal request object and transforms it to the desired snippet.
Error handling
SwaggerUI comes with a safe-render
plugin that handles error handling allows plugging into error handling system and modify it.
The plugin accepts a list of component names that should be protected by error boundaries.
Its public API looks like this:
safe-render plugin is automatically utilized by base and standalone SwaggerUI presets and should always be used as the last plugin, after all the components are already known to the SwaggerUI. The plugin defines a default list of components that should be protected by error boundaries:
As demonstrated below, additional components can be protected by utilizing the safe-render plugin with configuration options. This gets really handy if you are a SwaggerUI integrator and you maintain a number of plugins with additional custom components.
componentDidCatch
This static function is invoked after a component has thrown an error.
It receives two parameters:
error
- The error that was thrown.info
- An object with a componentStack key containing information about which component threw the error.
It has precisely the same signature as error boundaries componentDidCatch lifecycle method, except it’s a static function and not a class method.
Default implement of componentDidCatch uses console.error
to display the received error:
To utilize your own error handling logic (e.g. bugsnag), create new SwaggerUI plugin that overrides componentDidCatch:
{% highlight js linenos %} const BugsnagErrorHandlerPlugin = () => { // init bugsnag
return { fn: { componentDidCatch = (error, info) => { Bugsnag.notify(error); Bugsnag.notify(info); }, }, }; }; {% endhighlight %}
withErrorBoundary
This function is HOC (Higher Order Component). It wraps a particular component into the ErrorBoundary
component.
It can be overridden via a plugin system to control how components are wrapped by the ErrorBoundary component.
In 99.9% of situations, you won’t need to override this function, but if you do, please read the source code of this function first.
Fallback
The component is displayed when the error boundary catches an error. It can be overridden via a plugin system. Its default implementation is trivial:
Feel free to override it to match your look & feel:
ErrorBoundary
This is the component that implements React error boundaries. Uses componentDidCatch
and Fallback
under the hood. In 99.9% of situations, you won’t need to override this component, but if you do,
please read the source code of this component first.
Change in behavior
In prior releases of SwaggerUI (before v4.3.0), almost all components have been protected, and when thrown error,
Fallback
component was displayed. This changes with SwaggerUI v4.3.0. Only components defined
by the safe-render
plugin are now protected and display fallback. If a small component somewhere within
SwaggerUI React component tree fails to render and throws an error. The error bubbles up to the closest
error boundary, and that error boundary displays the Fallback
component and invokes componentDidCatch
.