|
| 1 | +const COMPONENTS_SCHEMAS_OBJECT_INDEX = 2; |
| 2 | +const REQUEST_BODY_OBJECT_INDEX = 2; |
| 3 | +const RESPONSE_NAME_INDEX = 2; |
| 4 | +const RESPONSE_OBJECT_INDEX = 3; |
| 5 | +const PATH_START_INDEX = 4; |
| 6 | + |
| 7 | +const { prepareReferenceName } = require('../utils/utils'); |
| 8 | + |
| 9 | +const handleReferencePath = (externalDefinitions, { $ref: ref }) => { |
| 10 | + if (ref.startsWith('#')) { |
| 11 | + ref = ref.replace('#model/definitions', '#/components'); |
| 12 | + |
| 13 | + return { $ref: prepareReferenceName(ref) }; |
| 14 | + } |
| 15 | + |
| 16 | + const [ pathToFile, relativePath] = ref.split('#/'); |
| 17 | + if (!relativePath) { |
| 18 | + return { $ref: prepareReferenceName(ref) }; |
| 19 | + } |
| 20 | + |
| 21 | + const externalDefinition = findExternalDefinition(externalDefinitions, pathToFile, relativePath); |
| 22 | + if (!externalDefinition) { |
| 23 | + return { $ref: prepareReferenceName(ref) }; |
| 24 | + } |
| 25 | + |
| 26 | + if (externalDefinition.fileType === 'targetSchema') { |
| 27 | + return { $ref: updateOpenApiPath(pathToFile, relativePath) }; |
| 28 | + } else if (externalDefinition.fileType === 'hackoladeSchema') { |
| 29 | + return externalDefinition; |
| 30 | + } else if (externalDefinition.fileType === 'jsonSchema') { |
| 31 | + return { $ref: fixJsonSchemaPath(pathToFile, relativePath) };; |
| 32 | + } |
| 33 | + |
| 34 | + return { $ref: prepareReferenceName(ref) }; |
| 35 | +}; |
| 36 | + |
| 37 | +const fixJsonSchemaPath = (pathToFile, relativePath) => { |
| 38 | + const namePath = relativePath.split('/'); |
| 39 | + if (['properties', 'items'].includes(namePath[0])) { |
| 40 | + return `${pathToFile}#/${relativePath}`; |
| 41 | + } |
| 42 | + |
| 43 | + return `${pathToFile}#/${namePath.slice(1).join('/')}`; |
| 44 | +}; |
| 45 | + |
| 46 | +const findExternalDefinition = (externalDefinitions, pathToFile, relativePath) => { |
| 47 | + pathToFile = pathToFile.replace('file://', ''); |
| 48 | + |
| 49 | + const definitionName = Object.keys(externalDefinitions).find(name => { |
| 50 | + const definition = externalDefinitions[name]; |
| 51 | + return ( |
| 52 | + definition.fieldRelativePath === '#/' + relativePath && |
| 53 | + definition.link === pathToFile |
| 54 | + ); |
| 55 | + }); |
| 56 | + |
| 57 | + return externalDefinitions[definitionName]; |
| 58 | +}; |
| 59 | + |
| 60 | +const updateOpenApiPath = (pathToFile, relativePath) => { |
| 61 | + let path = relativePath.split('/'); |
| 62 | + if (path[0] === 'definitions') { |
| 63 | + if (path[COMPONENTS_SCHEMAS_OBJECT_INDEX] === 'schemas') { |
| 64 | + return `${pathToFile}#/components/schemas/${path.slice(PATH_START_INDEX).join('/')}`; |
| 65 | + } |
| 66 | + |
| 67 | + path = ['', ...path]; |
| 68 | + } |
| 69 | + |
| 70 | + const schemaIndex = path.indexOf('schema'); |
| 71 | + const hasSchema = schemaIndex !== -1; |
| 72 | + const isComponents = (path[1] === 'definitions'); |
| 73 | + const schemaPath = !hasSchema ? [] : path.slice(schemaIndex); |
| 74 | + const pathWithoutProperties = (hasSchema ? path.slice(0, schemaIndex) : path).filter(item => item !== 'properties'); |
| 75 | + const bucketWithRequest = isComponents ? ['components'] : pathWithoutProperties.slice(0,2); |
| 76 | + const parentElementName = isComponents ? 'components' : 'paths'; |
| 77 | + const isResponse = pathWithoutProperties[RESPONSE_OBJECT_INDEX] === 'response'; |
| 78 | + const isRequestBody = pathWithoutProperties[REQUEST_BODY_OBJECT_INDEX] === 'requestBody' |
| 79 | + |
| 80 | + if (!isResponse) { |
| 81 | + if (!isRequestBody) { |
| 82 | + if (isComponents) { |
| 83 | + return `${pathToFile}#/${parentElementName}/${[ ...pathWithoutProperties.slice(2), ...schemaPath].join('/')}`; |
| 84 | + } |
| 85 | + |
| 86 | + return `${pathToFile}#/${parentElementName}/${[ ...pathWithoutProperties , ...schemaPath].join('/')}`; |
| 87 | + } |
| 88 | + |
| 89 | + return `${pathToFile}#/${parentElementName}/${[ ...bucketWithRequest, 'requestBody', 'content', ...pathWithoutProperties.slice(3), ...schemaPath].join('/')}`; |
| 90 | + } |
| 91 | + |
| 92 | + const response = pathWithoutProperties[RESPONSE_NAME_INDEX]; |
| 93 | + const pathToItem = pathWithoutProperties.slice(PATH_START_INDEX) |
| 94 | + |
| 95 | + const pathWithResponses = [ ...bucketWithRequest, 'responses', response, ...pathToItem, ...schemaPath ]; |
| 96 | + |
| 97 | + return `${pathToFile}#/paths/${pathWithResponses.join('/')}`; |
| 98 | +}; |
| 99 | + |
| 100 | +module.exports = handleReferencePath; |
0 commit comments