SAP Commerce automatically creates Swagger documentation for all endpoints, including both OOTB and custom ones, as well as those from OOTB extensions like “b2bocc”. The extensive number of OOTB endpoints can lead to a crowded Swagger interface, making it hard to navigate and find particular endpoint.
Problem Description
To improve navigation in Swagger there is a sense to disable unused endpoints and also disable endpoints itself in OCC, so no one can hit it and use undocumented functionality. Unfortunately, there is no OOTB way to remove endpoints from Swagger documentation.
The first idea would be to write a BeanFactoryPostProcessor to stop the creation of Controller
class beans, what would disable endpoints in both OCC and Swagger. While this approach worked, it only allowed disabling the entire Controller
, not specific endpoints.
The next idea would be to create a custom RequestMappingHandlerMapping. However, the OCC layer already has a custom CommerceHandlerMapping
implementation and does not provide an easy way to replace it. The next logical step would be to use HandlerInterceptor, what leads to the discovery of an existing OOTB BaseEndpointRestrictionsInterceptor
implementation of HandlerInterceptor
.
That OOTB implementation allows to disable OCC endpoints via properties. However, the disabled endpoints are still visible in Swagger.
Technical Solution
To solve the problem of unnecessary endpoints in Swagger, two steps need to be taken:
- Disable OCC Endpoints by Operation ID utilizing OOTB implementation
- Remove Endpoints from Swagger with custom implementation of
OpenApiCustomiser
, which filters out endpoints based on OOTB implementation of OCC endpoints disabling
Implementation Details
Disable OCC Endpoints
The main idea of OOTB implementation is to use properties *.api.restrictions.disabled.endpoints
with a list of Operation IDs for disabling (API Endpoint Deactivation documentation).
|
|
The first part of property commercewebservices
is dynamic part and should match the name of the web extension, in which endpoint is defined. However, in modern OCC implementations, all endpoints are combined under the commercewebservices
web extension.
In case of usage of a deprecated OCC web services implementation, the prefix should be replaced with corresponding OCC web service extension.
The disabling itself occurs at the request handling level in Spring MVC, that’s why such endpoints are still present in Swagger.
Remove Endpoints from Swagger Documentation
The UnusedEndpointsFilterOpenApiCustomiser
class is designed to post process OpenAPI documentation and remove specified endpoints from documentation.
|
|
To manage disabled endpoints, the class uses the WEB_EXTENSIONS_PREFIXES
constant, which lists prefixes for different web extensions. It relies on the OOTB endpointRestrictionsConfigStrategy#isEndpointDisabled
method to determine if specific Operation ID is disabled for each of web extension prefix. In case of usage of a deprecated OCC web services implementation, list should be adjusted to include all relevant web extensions.
List could be extended with not existing web extension, which would allow to create custom properties to remove endpoints from Swagger only.
After removing endpoints there could be left empty tags in Swagger(collapsible headers), which should be also removed. This is done by resetting tags with openApi.setTags
and findNonEmptyTags
method, which identifies and retains tags that has at least one operation assigned.