Skip to content

Commit ebc2d90

Browse files
committed
Fixed validation for relative references
1 parent 0fa3230 commit ebc2d90

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

forward_engineering/api.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const { getServers } = require('./helpers/serversHelper');
99
const getExtensions = require('./helpers/extensionsHelper');
1010
const handleReferencePath = require('./helpers/handleReferencePath');
1111
const mapJsonSchema = require('../reverse_engineering/helpers/adaptJsonSchema/mapJsonSchema');
12+
const path=require('path');
1213

1314
module.exports = {
1415
generateModelScript(data, logger, cb) {
@@ -87,7 +88,7 @@ module.exports = {
8788
parsedScript = JSON.parse(filteredScript);
8889
}
8990

90-
validationHelper.validate(parsedScript)
91+
validationHelper.validate(replaceRelativePathByAbsolute(parsedScript, targetScriptOptions.modelDirectory))
9192
.then((messages) => {
9293
cb(null, messages);
9394
})
@@ -150,6 +151,26 @@ const removeCommentLines = (scriptString) => {
150151
.replace(/(.*?),\s*(\}|])/g, '$1$2');
151152
}
152153

154+
const replaceRelativePathByAbsolute=(script, modelDirectory)=>{
155+
if(!modelDirectory || typeof modelDirectory !== 'string'){
156+
return script;
157+
}
158+
const stringifiedScript=JSON.stringify(script);
159+
const fixedScript= stringifiedScript.replace(/("\$ref":\s*)"(.*?(?<!\\))"/g, (match, refGroup, relativePath)=>{
160+
const isAbsolutePath=relativePath.startsWith('file:');
161+
const isInternetLink=relativePath.startsWith('http:') || relativePath.startsWith('https:');
162+
163+
if(isAbsolutePath || isInternetLink){
164+
return match
165+
}
166+
const absolutePath=path.resolve(modelDirectory, '..',relativePath)
167+
168+
return `${refGroup}"file://${absolutePath}"`
169+
});
170+
171+
return JSON.parse(fixedScript);
172+
}
173+
153174
const handleRefInContainers = (containers, externalDefinitions) => {
154175
return containers.map(container => {
155176
try {

0 commit comments

Comments
 (0)