Skip to content

Custom Visitors

Sandor edited this page Jan 7, 2018 · 3 revisions

It is possible to pass custom swagger visitors. It has to be in the form of an array of factories. the factories can be plain js (in gulp) or string in cli json file.

It can be particularly useful when presented a malformed swagger file (duplicated operationId, wrong basePath, parameterName conflicting with language keywords).

The following example will drop the basePath (heaps of swagger schema have a slash there which is a mistake according to the spec). It will also look form some other mistakes and fix them accordingly.

"customSchemaVisitors" : [
                `() => { 
                    return {
                        "visitRoot" : (api) => api.basePath = undefined,
                        "visitOperation" : (verb, operation) => {
         
                            if (operation.operationId === 'devices_getUpdateDevicesStatus') {
                                    if (operation.parameters[0].name === 'release_id') {
                                        operation.operationId = operation.operationId + '_byRelease';
                                        console.warn('renaming devices_getUpdateDevicesStatus', operation);
                                    }
                                }
                        },
                        "visitOperationParameter" : (parameter, index) => {
                            if (parameter.name === 'params') {
                                parameter.name = 'someParams';
                                console.warn('renaming params to someParams', parameter)
                            }
                        }
                    }
                }`
            ],
Clone this wiki locally