Skip to content
This repository has been archived by the owner on Jul 16, 2020. It is now read-only.

fixed error messages of JSON parsing is failed #12

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions __test__/index.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
var ramlToSwagger = require('../src');

describe('converting', () => {
it('should fail fast if json is wrong', () => {
const testJson = '{"some": "json" "that-is": "wrong"}';
const testData = {
title: 'test',
version: '2.0.0',
securitySchemes: [],
resources: [],
schemas: [
{
TestSchema: testJson
}
]
};

expect(() => {
ramlToSwagger.convert(testData);
}).toThrowError([
'Unexpected string in JSON at position 16',
'1: {"some": "json" "that-is": "wrong"}',
'------------------^'
].join('\n'));
});
});
7 changes: 7 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
"version": "1.1.0",
"description": "Script for converting RAML format into Swagger 2.0",
"main": "src/index.js",
"scripts": {
"test": "jest"
},
"repository": {
"type": "git",
"url": "git://github.com/APIs-guru/raml-to-swagger.git"
Expand All @@ -27,5 +30,9 @@
"jsonpath": "^0.2.5",
"lodash": "^4.1.0",
"urijs": "^1.17.0"
},
"devDependencies": {
"better-error-message-for-json-parse": "^0.1.6",
"jest": "^22.4.2"
}
}
14 changes: 5 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var _ = require('lodash');
var jsonCompat = require('json-schema-compatibility');
var HttpStatus = require('http-status-codes').getStatusText;
var jp = require('jsonpath');
var bemfjp = require('better-error-message-for-json-parse');

exports.convert = function (raml) {
//FIXME:
Expand Down Expand Up @@ -344,18 +345,13 @@ function parseBody(ramlBody, srMethod) {
});
}

function convertSchema(schema) {
if (_.isUndefined(schema))
function convertSchema(rawSchema) {
if (_.isUndefined(rawSchema))
return;

assert(_.isString(schema));
assert(_.isString(rawSchema));

try {
var schema = JSON.parse(schema);
}
catch (e) {
return undefined;
}
var schema = bemfjp.safeJsonParse(rawSchema);

delete schema.id;
delete schema.$schema;
Expand Down