Skip to content

Commit 233d052

Browse files
authored
perf: shallow copy schema (#14)
1 parent ade526b commit 233d052

File tree

5 files changed

+47
-28
lines changed

5 files changed

+47
-28
lines changed

index.js

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
var deepEqual = require('fast-deep-equal');
22
var convert = require('./lib/convert');
3-
var clone = require('lodash.clonedeep');
43

54
module.exports = openapiSchemaToJsonSchema;
65
module.exports.fromSchema = openapiSchemaToJsonSchema;
@@ -9,21 +8,13 @@ module.exports.fromParameter = openapiParameterToJsonSchema;
98
function openapiSchemaToJsonSchema(schema, options) {
109
options = resolveOptions(options);
1110

12-
if (options.cloneSchema) {
13-
schema = clone(schema);
14-
}
15-
1611
var jsonSchema = convert.fromSchema(schema, options);
1712
return jsonSchema;
1813
}
1914

2015
function openapiParameterToJsonSchema(parameter, options) {
2116
options = resolveOptions(options);
2217

23-
if (options.cloneSchema) {
24-
parameter = clone(parameter);
25-
}
26-
2718
var jsonSchema = convert.fromParameter(parameter, options);
2819
return jsonSchema;
2920
}

lib/converters/schema.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ function convertFromSchema (schema, options) {
1313
}
1414

1515
function convertSchema (schema, options) {
16-
var structs = options._structs;
16+
if (options.cloneSchema) {
17+
schema = Object.assign({}, schema);
18+
}
19+
20+
var structs = options._structs;
1721
var notSupported = options._notSupported;
1822
var strictMode = options.strictMode;
1923
var i = 0;
@@ -24,8 +28,15 @@ function convertSchema (schema, options) {
2428
struct = structs[i]
2529

2630
if (Array.isArray(schema[struct])) {
27-
for (j; j < schema[struct].length; j++) {
31+
var cloned = false;
32+
33+
for (j; j < schema[struct].length; j++) {
2834
if (!isObject(schema[struct][j])) {
35+
if (options.cloneSchema && !cloned) {
36+
cloned = true;
37+
schema[struct] = schema[struct].slice();
38+
}
39+
2940
schema[struct].splice(j, 1)
3041
j--
3142
continue
@@ -92,17 +103,14 @@ function convertProperties (properties, options) {
92103
}
93104

94105
for (key in properties) {
95-
removeProp = false
96106
property = properties[key]
97107

98108
if (!isObject(property)) {
99109
continue
100110
}
101111

102-
options._removeProps.forEach(function (prop) {
103-
if (property[prop] === true) {
104-
removeProp = true
105-
}
112+
removeProp = options._removeProps.some(function (prop) {
113+
return property[prop] === true
106114
})
107115

108116
if (removeProp) {

package-lock.json

Lines changed: 0 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
"author": "OpenAPI Contrib",
1414
"license": "MIT",
1515
"dependencies": {
16-
"fast-deep-equal": "^3.1.3",
17-
"lodash.clonedeep": "^4.5.0"
16+
"fast-deep-equal": "^3.1.3"
1817
},
1918
"devDependencies": {
2019
"eslint": "^6.8.0",

test/clone_schema.test.js

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,49 @@ var convert = require('../');
33

44
test('cloning schema by default', function (assert) {
55
var schema;
6+
var cloned;
67
var result;
78
var expected;
89

910
assert.plan(2)
1011

1112
schema = {
1213
type: 'string',
13-
nullable: true
14+
nullable: true,
15+
properties: {
16+
foo: true,
17+
bar: {
18+
allOf: [
19+
null,
20+
{
21+
type: 'string',
22+
},
23+
null
24+
]
25+
}
26+
}
1427
}
1528

16-
result = convert(schema)
29+
cloned = JSON.parse(JSON.stringify(schema));
30+
31+
result = convert(cloned)
1732

1833
expected = {
1934
$schema: 'http://json-schema.org/draft-04/schema#',
20-
type: ['string', 'null']
35+
type: ['string', 'null'],
36+
properties: {
37+
bar: {
38+
allOf: [
39+
{
40+
type: 'string',
41+
}
42+
]
43+
}
44+
}
2145
}
2246

2347
assert.deepEqual(result, expected, 'converted')
24-
assert.notEqual(result, schema, 'schema cloned')
48+
assert.deepEqual(cloned, schema, 'schema cloned')
2549
})
2650

2751
test('cloning schema with cloneSchema option', function (assert) {
@@ -32,6 +56,8 @@ test('cloning schema with cloneSchema option', function (assert) {
3256
nullable: true
3357
}
3458

59+
var cloned = JSON.parse(JSON.stringify(schema))
60+
3561
var result = convert(schema, { cloneSchema: true })
3662

3763
var expected = {
@@ -40,7 +66,7 @@ test('cloning schema with cloneSchema option', function (assert) {
4066
}
4167

4268
assert.deepEqual(result, expected, 'converted')
43-
assert.notEqual(result, schema, 'schema cloned')
69+
assert.deepEqual(cloned, schema, 'schema cloned')
4470
})
4571

4672
test('handles circular references', function (assert) {

0 commit comments

Comments
 (0)