openapi-sampler
Advanced tools
Comparing version 1.0.0-beta.16 to 1.0.0-beta.17
@@ -289,3 +289,3 @@ (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.OpenAPISampler = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){ | ||
var _traverse = (0, _traverse2.traverse)(_objectSpread({ | ||
type: type | ||
type: res.type | ||
}, subSchema), options, spec), | ||
@@ -750,2 +750,4 @@ type = _traverse.type, | ||
var _utils = require("./utils"); | ||
var _jsonPointer = _interopRequireDefault(require("json-pointer")); | ||
@@ -761,9 +763,19 @@ | ||
var $refCache = {}; | ||
var $refCache = {}; // for circular JS references we use additional array and not object as we need to compare entire schemas and not strings | ||
var seenSchemasStack = []; | ||
function clearCache() { | ||
$refCache = {}; | ||
seenSchemasStack = []; | ||
} | ||
function traverse(schema, options, spec, context) { | ||
// checking circular JS references by checking context | ||
// because context is passed only when traversing through nested objects happens | ||
if (context) { | ||
if (seenSchemasStack.includes(schema)) return (0, _utils.getResultForCircular)((0, _infer.inferType)(schema)); | ||
seenSchemasStack.push(schema); | ||
} | ||
if (schema.$ref) { | ||
@@ -790,7 +802,6 @@ if (!spec) { | ||
var referencedType = (0, _infer.inferType)(referenced); | ||
result = { | ||
value: referencedType === 'object' ? {} : referencedType === 'array' ? [] : undefined | ||
}; | ||
result = (0, _utils.getResultForCircular)(referencedType); | ||
} | ||
(0, _utils.popSchemaStack)(seenSchemasStack, context); | ||
return result; | ||
@@ -800,2 +811,3 @@ } | ||
if (schema.example !== undefined) { | ||
(0, _utils.popSchemaStack)(seenSchemasStack, context); | ||
return { | ||
@@ -810,2 +822,3 @@ value: schema.example, | ||
if (schema.allOf !== undefined) { | ||
(0, _utils.popSchemaStack)(seenSchemasStack, context); | ||
return (0, _allOf.allOfSample)(_objectSpread({}, schema, { | ||
@@ -821,2 +834,3 @@ allOf: undefined | ||
(0, _utils.popSchemaStack)(seenSchemasStack, context); | ||
return traverse(schema.oneOf[0], options, spec); | ||
@@ -826,2 +840,3 @@ } | ||
if (schema.anyOf && schema.anyOf.length) { | ||
(0, _utils.popSchemaStack)(seenSchemasStack, context); | ||
return traverse(schema.anyOf[0], options, spec); | ||
@@ -855,2 +870,3 @@ } | ||
(0, _utils.popSchemaStack)(seenSchemasStack, context); | ||
return { | ||
@@ -864,3 +880,3 @@ value: example, | ||
},{"./allOf":3,"./infer":4,"./openapi-sampler":5,"json-pointer":2}],13:[function(require,module,exports){ | ||
},{"./allOf":3,"./infer":4,"./openapi-sampler":5,"./utils":13,"json-pointer":2}],13:[function(require,module,exports){ | ||
'use strict'; | ||
@@ -875,2 +891,4 @@ | ||
exports.uuid = uuid; | ||
exports.getResultForCircular = getResultForCircular; | ||
exports.popSchemaStack = popSchemaStack; | ||
@@ -942,2 +960,12 @@ function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } | ||
function getResultForCircular(type) { | ||
return { | ||
value: type === 'object' ? {} : type === 'array' ? [] : undefined | ||
}; | ||
} | ||
function popSchemaStack(seenSchemasStack, context) { | ||
if (context) seenSchemasStack.pop(); | ||
} | ||
function hashCode(str) { | ||
@@ -944,0 +972,0 @@ var hash = 0; |
{ | ||
"name": "openapi-sampler", | ||
"version": "1.0.0-beta.16", | ||
"version": "1.0.0-beta.17", | ||
"description": "Tool for generation samples based on OpenAPI payload/response schema", | ||
"main": "dist/openapi-sampler.js", | ||
"jsnext:main": "src/openapi-sampler.js", | ||
"module": "src/openapi-sampler.js", | ||
"scripts": { | ||
@@ -8,0 +8,0 @@ "test": "gulp", |
@@ -9,3 +9,3 @@ import { traverse } from './traverse'; | ||
for (let subSchema of children) { | ||
const { type, readOnly, writeOnly, value } = traverse({ type, ...subSchema }, options, spec); | ||
const { type, readOnly, writeOnly, value } = traverse({ type: res.type, ...subSchema }, options, spec); | ||
if (res.type && type && type !== res.type) { | ||
@@ -12,0 +12,0 @@ console.warn('allOf: schemas with different types can\'t be merged'); |
import { _samplers } from './openapi-sampler'; | ||
import { allOfSample } from './allOf'; | ||
import { inferType } from './infer'; | ||
import { getResultForCircular, popSchemaStack } from './utils'; | ||
import JsonPointer from 'json-pointer'; | ||
let $refCache = {}; | ||
// for circular JS references we use additional array and not object as we need to compare entire schemas and not strings | ||
let seenSchemasStack = []; | ||
export function clearCache() { | ||
$refCache = {}; | ||
seenSchemasStack = []; | ||
} | ||
export function traverse(schema, options, spec, context) { | ||
// checking circular JS references by checking context | ||
// because context is passed only when traversing through nested objects happens | ||
if (context) { | ||
if (seenSchemasStack.includes(schema)) return getResultForCircular(inferType(schema)); | ||
seenSchemasStack.push(schema); | ||
} | ||
if (schema.$ref) { | ||
@@ -32,9 +43,5 @@ if (!spec) { | ||
const referencedType = inferType(referenced); | ||
result = { | ||
value: referencedType === 'object' ? | ||
{} | ||
: referencedType === 'array' ? [] : undefined | ||
}; | ||
result = getResultForCircular(referencedType); | ||
} | ||
popSchemaStack(seenSchemasStack, context); | ||
return result; | ||
@@ -44,2 +51,3 @@ } | ||
if (schema.example !== undefined) { | ||
popSchemaStack(seenSchemasStack, context); | ||
return { | ||
@@ -54,2 +62,3 @@ value: schema.example, | ||
if (schema.allOf !== undefined) { | ||
popSchemaStack(seenSchemasStack, context); | ||
return allOfSample( | ||
@@ -67,2 +76,3 @@ { ...schema, allOf: undefined }, | ||
} | ||
popSchemaStack(seenSchemasStack, context); | ||
return traverse(schema.oneOf[0], options, spec); | ||
@@ -72,2 +82,3 @@ } | ||
if (schema.anyOf && schema.anyOf.length) { | ||
popSchemaStack(seenSchemasStack, context); | ||
return traverse(schema.anyOf[0], options, spec); | ||
@@ -96,3 +107,4 @@ } | ||
} | ||
popSchemaStack(seenSchemasStack, context); | ||
return { | ||
@@ -99,0 +111,0 @@ value: example, |
@@ -62,2 +62,14 @@ 'use strict'; | ||
export function getResultForCircular(type) { | ||
return { | ||
value: type === 'object' ? | ||
{} | ||
: type === 'array' ? [] : undefined | ||
}; | ||
} | ||
export function popSchemaStack(seenSchemasStack, context) { | ||
if (context) seenSchemasStack.pop(); | ||
} | ||
function hashCode(str) { | ||
@@ -64,0 +76,0 @@ var hash = 0; |
@@ -606,2 +606,147 @@ 'use strict'; | ||
}); | ||
describe('circular references in JS object', function() { | ||
let result, schema, expected; | ||
it('should not follow circular references in JS object', function() { | ||
const someType = { | ||
type: 'string' | ||
}; | ||
const circularSchema = { | ||
type: 'object', | ||
properties: { | ||
a: someType | ||
} | ||
} | ||
circularSchema.properties.b = circularSchema; | ||
schema = circularSchema; | ||
result = OpenAPISampler.sample(schema); | ||
expected = { | ||
a: 'string', | ||
b: { | ||
a: 'string', | ||
b: {} | ||
} | ||
}; | ||
expect(result).to.deep.equal(expected); | ||
}); | ||
it('should not detect false-positive circular references in JS object', function() { | ||
const a = { | ||
type: 'string', | ||
example: 'test' | ||
}; | ||
const b = { | ||
type: 'integer', | ||
example: 1 | ||
}; | ||
const c = { | ||
type: 'object', | ||
properties: { | ||
test: { | ||
'type': 'string' | ||
} | ||
} | ||
}; | ||
const d = { | ||
type: 'array', | ||
items: { | ||
'type': 'string', | ||
} | ||
}; | ||
const e = { | ||
allOf: [ c, c ] | ||
}; | ||
const f = { | ||
oneOf: [d, d ] | ||
}; | ||
const g = { | ||
anyOf: [ c, c ] | ||
}; | ||
const h = { $ref: '#/a' }; | ||
const nonCircularSchema = { | ||
type: 'object', | ||
properties: { | ||
a: a, | ||
aa: a, | ||
b: b, | ||
bb: b, | ||
c: c, | ||
cc: c, | ||
d: d, | ||
dd: d, | ||
e: e, | ||
ee: e, | ||
f: f, | ||
ff: f, | ||
g: g, | ||
gg: g, | ||
h: h, | ||
hh: h | ||
} | ||
} | ||
const spec = { | ||
nonCircularSchema, | ||
a: a | ||
} | ||
result = OpenAPISampler.sample(nonCircularSchema, {}, spec); | ||
expected = { | ||
a: 'test', | ||
aa: 'test', | ||
b: 1, | ||
bb: 1, | ||
c: {'test': 'string'}, | ||
cc: {'test': 'string'}, | ||
d: ['string'], | ||
dd: ['string'], | ||
e: {'test': 'string'}, | ||
ee: {'test': 'string'}, | ||
f: ['string'], | ||
ff: ['string'], | ||
g: {'test': 'string'}, | ||
gg: {'test': 'string'}, | ||
h: 'test', | ||
hh: 'test' | ||
}; | ||
expect(result).to.deep.equal(expected); | ||
}); | ||
it('should not follow circular references in JS object when more that one circular reference present', function() { | ||
const circularSchema = { | ||
type: 'object', | ||
properties: {} | ||
} | ||
circularSchema.properties.a = circularSchema; | ||
circularSchema.properties.b = circularSchema; | ||
schema = circularSchema; | ||
result = OpenAPISampler.sample(schema); | ||
expected = { | ||
a: { | ||
a: {}, | ||
b: {} | ||
}, | ||
b: { | ||
a: {}, | ||
b: {} | ||
} | ||
}; | ||
expect(result).to.deep.equal(expected); | ||
}); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
86371
2433