diff --git a/README.md b/README.md index d5a042e..a1033aa 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,8 @@ swaggerui_blueprint = get_swaggerui_blueprint( SWAGGER_URL, # Swagger UI static files will be mapped to '{SWAGGER_URL}/dist/' API_URL, config={ # Swagger UI config overrides - 'app_name': "Test application" + 'app_name': "Test application", + 'requestInterceptor' : 'function requestInterceptor(request) {return request;}' }, # oauth_config={ # OAuth config. See https://github.com/swagger-api/swagger-ui#oauth2-configuration . # 'clientId': "your-client-id", @@ -52,6 +53,9 @@ app.run() The blueprint supports overloading all Swagger UI configuration options that can be JSON serialized. See https://github.com/swagger-api/swagger-ui#parameters for options. -Plugins and function parameters are not supported at this time. +Function parameters are supported in the form of strings that are evaluated as javascript. They must +define a function that is of the same name as the parameter. + +Plugins are not supported at this time. OAuth2 parameters can be found at https://github.com/swagger-api/swagger-ui#oauth2-configuration . diff --git a/flask_swagger_ui/templates/index.template.html b/flask_swagger_ui/templates/index.template.html index 127ef4c..0e0e0b8 100644 --- a/flask_swagger_ui/templates/index.template.html +++ b/flask_swagger_ui/templates/index.template.html @@ -48,6 +48,33 @@ var user_config = {{config_json|safe}}; // User config options provided from Python code for (var attrname in user_config) { config[attrname] = user_config[attrname]; } +// Parse a string to a javascript function. +// If it's not possible to parse it to a function, return the string. +function parse_function(function_name, value) { + try { + eval(value); + var evaluated_value = eval(function_name); + if (typeof(evaluated_value) === 'function') { + return evaluated_value; + } else { + console.error(`error evaluating function for config attribute ${attrname}. Not a function (${typeof(evaluated_value)}).`); + } + } + catch(e) { + console.error(`error evaluating function "${user_config[attrname]}" for config attribute ${attrname}. Exception: `, e); + } + + return value; +} + +// For all the function attributes, parse them if they're present. +var function_attrs = ["requestInterceptor", "responseInterceptor", "operationsSorter", "tagsSorter", "onComplete"]; +function_attrs.forEach(function f(function_attr) { + if (function_attr in config) { + config[function_attr] = parse_function(function_attr, config[function_attr]) + } +}); + window.onload = function() { // Build a system const ui = SwaggerUIBundle(config)