Skip to content

Commit 6a9310a

Browse files
Merge branch 'master' of github.com:mtseluiko/OpenAPI
2 parents 3c51fd8 + e35248d commit 6a9310a

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

forward_engineering/helpers/typeHelper.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ function getTypeProps(data, key) {
7171
function getRef({ $ref: ref }) {
7272
if (ref.startsWith('#')) {
7373
ref = ref.replace('#model/definitions', '#/components');
74+
7475
return { $ref: prepareReferenceName(ref) };
7576
}
7677

reverse_engineering/api.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,11 @@ module.exports = {
6262
} catch(e) {
6363
callback(commonHelper.handleErrorObject(e, 'Adapt JSON Schema'), data);
6464
}
65-
}
65+
},
66+
67+
resolveExternalDefinitionPath(data, logger, callback) {
68+
resolveExternalDefinitionPathHelper.resolvePath(data, callback);
69+
}
6670
};
6771

6872
const convertOpenAPISchemaToHackolade = (openAPISchema, fieldOrder) => {
@@ -75,7 +79,7 @@ const convertOpenAPISchemaToHackolade = (openAPISchema, fieldOrder) => {
7579
};
7680

7781
const getOpenAPISchema = (data, filePath) => new Promise((resolve, reject) => {
78-
const { extension, fileName } = commonHelper.getPathData(filePath);
82+
const { extension, fileName } = commonHelper.getPathData(data, filePath);
7983

8084
try {
8185
const openAPISchemaWithModelName = dataHelper.getOpenAPIJsonSchema(data, fileName, extension);

reverse_engineering/helpers/commonHelper.js

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const yaml = require('js-yaml');
66
const errorHelper = require('./errorHelper');
77

88
const CHOICES = ['allOf', 'oneOf', 'anyOf', 'not'];
9+
const ALLOWED_EXTENSIONS = ['.json', '.yaml'];
910

1011
const getFileData = (filePath) => new Promise((resolve, reject) => {
1112
fs.readFile(filePath, 'utf-8', (error, content) => {
@@ -17,10 +18,43 @@ const getFileData = (filePath) => new Promise((resolve, reject) => {
1718
});
1819
});
1920

20-
const getPathData = (filePath) => {
21+
const isJson = data => {
22+
try {
23+
JSON.parse(data);
24+
25+
return true;
26+
} catch (err) {
27+
return false;
28+
}
29+
};
30+
31+
const isYaml = data => {
32+
try {
33+
yaml.load(data);
34+
35+
return true;
36+
} catch (err) {
37+
return false;
38+
}
39+
};
40+
41+
const getPathData = (data, filePath) => {
2142
const extension = path.extname(filePath);
2243
const fileName = path.basename(filePath, extension);
23-
return {extension, fileName };
44+
45+
if (ALLOWED_EXTENSIONS.includes(extension)) {
46+
return { extension, fileName };
47+
}
48+
49+
if (isJson(data)) {
50+
return { extension: '.json', fileName };
51+
}
52+
53+
if (isYaml(data)) {
54+
return { extension: '.yaml', fileName };
55+
}
56+
57+
return { extension, fileName };
2458
};
2559

2660
const handleErrorObject = (error, title) => {

0 commit comments

Comments
 (0)