|
| 1 | +/* |
| 2 | + * Copyright IBM Corporation 2017 |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +'use strict' |
| 17 | +let debug = require('debug')('arf:swaggerize:index') |
| 18 | +let utils = require('./utils') |
| 19 | +let swaggerParser = require('swagger-parser') |
| 20 | +let builderUtils = require('swaggerize-routes/lib/utils') |
| 21 | + |
| 22 | +function ensureValidAsync (loadedSwagger) { |
| 23 | + debug('in ensureValidAsync') |
| 24 | + return swaggerParser.validate(loadedSwagger) |
| 25 | + .catch(function (err) { |
| 26 | + debug(err) |
| 27 | + throw new Error('does not conform to swagger specification') |
| 28 | + }) |
| 29 | +} |
| 30 | + |
| 31 | +function parseSwagger (api, formatters) { |
| 32 | + debug('in parseSwagger') |
| 33 | + // walk the api, extract the schemas from the definitions, the parameters and the responses. |
| 34 | + let resources = {} |
| 35 | + let refs = [] |
| 36 | + let basePath = api.basePath || undefined |
| 37 | + |
| 38 | + formatters = formatters === undefined ? {} : formatters |
| 39 | + |
| 40 | + let pathFormatter = formatters['pathFormatter'] || function (path) { return path } |
| 41 | + let resourceFormatter = formatters['resourceFormatter'] || function (route) { return route } |
| 42 | + |
| 43 | + Object.keys(api.paths).forEach(function (path) { |
| 44 | + let resource = resourceFormatter(path) |
| 45 | + |
| 46 | + debug('path:', path, 'becomes resource: "' + resource + '" with route: "' + pathFormatter(path) + '"') |
| 47 | + // for each path, walk the method verbs |
| 48 | + builderUtils.verbs.forEach(function (verb) { |
| 49 | + if (api.paths[path][verb]) { |
| 50 | + if (!resources[resource]) { |
| 51 | + resources[resource] = [] |
| 52 | + } |
| 53 | + |
| 54 | + debug('parsing verb:', verb) |
| 55 | + // save the method and the path in the resources list. |
| 56 | + resources[resource].push({method: verb, route: pathFormatter(path)}) |
| 57 | + // process the parameters |
| 58 | + if (api.paths[path][verb].parameters) { |
| 59 | + let parameters = api.paths[path][verb].parameters |
| 60 | + |
| 61 | + parameters.forEach(function (parameter) { |
| 62 | + if (parameter.schema) { |
| 63 | + if (parameter.schema.$ref) { |
| 64 | + // handle the schema ref |
| 65 | + let ref = utils.getRefName(parameter.schema.$ref) |
| 66 | + refs[ref] = api.definitions[ref] |
| 67 | + } else if (parameter.schema.items) { |
| 68 | + // handle array of schema items |
| 69 | + if (parameter.schema.items.$ref) { |
| 70 | + let ref = utils.getRefName(parameter.schema.items.$ref) |
| 71 | + // handle the schema ref |
| 72 | + refs[ref] = api.definitions[ref] |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + }) |
| 77 | + } |
| 78 | + |
| 79 | + // process the responses. 200 and default are probably the only ones that make any sense. |
| 80 | + ['200', 'default'].forEach(function (responseType) { |
| 81 | + if (api.paths[path][verb].responses && api.paths[path][verb].responses[responseType]) { |
| 82 | + let responses = api.paths[path][verb].responses |
| 83 | + if (responses[responseType] && responses[responseType].schema) { |
| 84 | + let ref |
| 85 | + if (responses[responseType].schema.$ref) { |
| 86 | + // handle the schema ref |
| 87 | + ref = utils.getRefName(responses[responseType].schema.$ref) |
| 88 | + refs[ref] = api.definitions[ref] |
| 89 | + } else if (responses[responseType].schema.type && responses[responseType].schema.type === 'array') { |
| 90 | + if (responses[responseType].schema.items && responses[responseType].schema.items.$ref) { |
| 91 | + ref = utils.getRefName(responses[responseType].schema.items.$ref) |
| 92 | + refs[ref] = api.definitions[ref] |
| 93 | + if (responses[responseType].schema.items) { |
| 94 | + // handle array of schema items |
| 95 | + if (responses[responseType].schema.items.$ref) { |
| 96 | + // handle the schema ref |
| 97 | + ref = utils.getRefName(responses[responseType].schema.items.$ref) |
| 98 | + refs[ref] = api.definitions[ref] |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + }) |
| 106 | + } |
| 107 | + }) |
| 108 | + }) |
| 109 | + |
| 110 | + let foundNewRef |
| 111 | + do { |
| 112 | + foundNewRef = false |
| 113 | + // now parse the schemas for child references. |
| 114 | + Object.keys(refs).forEach(function (schema) { |
| 115 | + if (refs[schema] && refs[schema].properties) { |
| 116 | + let properties = refs[schema].properties |
| 117 | + Object.keys(properties).forEach(function (property) { |
| 118 | + let name |
| 119 | + if (properties[property].$ref) { |
| 120 | + // this property contains a definition reference. |
| 121 | + name = utils.getRefName(properties[property].$ref) |
| 122 | + if (!refs[name]) { |
| 123 | + refs[name] = api.definitions[name] |
| 124 | + foundNewRef = true |
| 125 | + } |
| 126 | + } else if (properties[property].items && properties[property].items.$ref) { |
| 127 | + // this property contains a definition reference. |
| 128 | + name = utils.getRefName(properties[property].items.$ref) |
| 129 | + if (!refs[name]) { |
| 130 | + refs[name] = api.definitions[name] |
| 131 | + foundNewRef = true |
| 132 | + } |
| 133 | + } |
| 134 | + }) |
| 135 | + } |
| 136 | + }) |
| 137 | + } while (foundNewRef) |
| 138 | + |
| 139 | + let parsed = {basepath: basePath, resources: resources, refs: refs} |
| 140 | + return parsed |
| 141 | +} |
| 142 | + |
| 143 | +exports.parse = function (swaggerStr, formatters) { |
| 144 | + debug('in parse') |
| 145 | + let loaded = JSON.parse(swaggerStr) |
| 146 | + return ensureValidAsync(loaded) |
| 147 | + .then(function () { |
| 148 | + debug('successfully validated against schema') |
| 149 | + // restore the original swagger as the call to ensureValidAsync modifies the original loaded object. |
| 150 | + loaded = JSON.parse(swaggerStr) |
| 151 | + return { loaded: loaded, parsed: parseSwagger(loaded, formatters) } |
| 152 | + }) |
| 153 | +} |
0 commit comments