Skip to content

Commit 7cb0853

Browse files
committed
change patternProperties behaviour
1 parent 97d2289 commit 7cb0853

File tree

4 files changed

+151
-21
lines changed

4 files changed

+151
-21
lines changed

index.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
var deepEqual = require('deep-equal');
2+
13
module.exports = convert;
24

35
function convert(schema, options) {
@@ -156,17 +158,14 @@ function patternPropertiesHandler(schema) {
156158
var pattern
157159
, patternsObj = schema.patternProperties
158160
, additProps = schema.additionalProperties
159-
, type
160161
;
161162

162163
if (typeof additProps !== 'object') {
163164
return schema;
164165
}
165166

166167
for (pattern in patternsObj) {
167-
type = patternsObj[pattern].type;
168-
169-
if ((type === additProps.type) && type !== 'object' && type !== 'array') {
168+
if (deepEqual(patternsObj[pattern], additProps)) {
170169
schema.additionalProperties = false;
171170
break;
172171
}

package-lock.json

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
"repository": "github:mikunn/openapi-schema-to-json-schema",
1010
"author": "Mika Kunnas",
1111
"license": "MIT",
12-
"dependencies": {},
12+
"dependencies": {
13+
"deep-equal": "^1.0.1"
14+
},
1315
"devDependencies": {
1416
"tap-spec": "^4.1.1",
1517
"tape": "^4.8.0"

test/pattern_properties.js

Lines changed: 144 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ test('handling additional properties with one of patternProperty types', functio
116116
assert.deepEqual(result, expected, 'additionalProperties set to false');
117117
});
118118

119-
test('keeping additionalProperties with object type', function(assert) {
119+
test('handling additionalProperties with matching objects', function(assert) {
120120
var schema
121121
, result
122122
, expected
@@ -127,14 +127,81 @@ test('keeping additionalProperties with object type', function(assert) {
127127
schema = {
128128
type: 'object',
129129
additionalProperties: {
130-
type: 'object'
130+
type: 'object',
131+
properties: {
132+
test: {
133+
type: 'string'
134+
}
135+
}
131136
},
132137
'x-patternProperties': {
133138
'^[a-z]*$': {
134139
type: 'string'
135140
},
136141
'^[A-Z]*$': {
137-
type: 'object'
142+
type: 'object',
143+
properties: {
144+
test: {
145+
type: 'string'
146+
}
147+
}
148+
}
149+
}
150+
};
151+
152+
result = convert(schema, {supportPatternProperties: true});
153+
154+
expected = {
155+
$schema: 'http://json-schema.org/draft-04/schema#',
156+
type: 'object',
157+
additionalProperties: false,
158+
patternProperties: {
159+
'^[a-z]*$': {
160+
type: 'string'
161+
},
162+
'^[A-Z]*$': {
163+
type: 'object',
164+
properties: {
165+
test: {
166+
type: 'string'
167+
}
168+
}
169+
}
170+
}
171+
};
172+
173+
assert.deepEqual(result, expected, 'additionalProperties set to false');
174+
});
175+
176+
test('handling additionalProperties with non-matching objects', function(assert) {
177+
var schema
178+
, result
179+
, expected
180+
;
181+
182+
assert.plan(1);
183+
184+
schema = {
185+
type: 'object',
186+
additionalProperties: {
187+
type: 'object',
188+
properties: {
189+
test: {
190+
type: 'string'
191+
}
192+
}
193+
},
194+
'x-patternProperties': {
195+
'^[a-z]*$': {
196+
type: 'string'
197+
},
198+
'^[A-Z]*$': {
199+
type: 'object',
200+
properties: {
201+
test: {
202+
type: 'integer'
203+
}
204+
}
138205
}
139206
}
140207
};
@@ -145,22 +212,32 @@ test('keeping additionalProperties with object type', function(assert) {
145212
$schema: 'http://json-schema.org/draft-04/schema#',
146213
type: 'object',
147214
additionalProperties: {
148-
type: 'object'
215+
type: 'object',
216+
properties: {
217+
test: {
218+
type: 'string'
219+
}
220+
}
149221
},
150222
patternProperties: {
151223
'^[a-z]*$': {
152224
type: 'string'
153225
},
154226
'^[A-Z]*$': {
155-
type: 'object'
227+
type: 'object',
228+
properties: {
229+
test: {
230+
type: 'integer'
231+
}
232+
}
156233
}
157234
}
158235
};
159236

160-
assert.deepEqual(result, expected, 'additionalProperties kept unchanged');
237+
assert.deepEqual(result, expected, 'additionalProperties not changed');
161238
});
162239

163-
test('keeping additionalProperties with array type', function(assert) {
240+
test('handling additionalProperties with matching array', function(assert) {
164241
var schema
165242
, result
166243
, expected
@@ -194,12 +271,7 @@ test('keeping additionalProperties with array type', function(assert) {
194271
expected = {
195272
$schema: 'http://json-schema.org/draft-04/schema#',
196273
type: 'object',
197-
additionalProperties: {
198-
type: 'array',
199-
items: {
200-
type: 'string'
201-
}
202-
},
274+
additionalProperties: false,
203275
patternProperties: {
204276
'^[a-z]*$': {
205277
type: 'string'
@@ -213,7 +285,64 @@ test('keeping additionalProperties with array type', function(assert) {
213285
}
214286
};
215287

216-
assert.deepEqual(result, expected, 'additionalProperties kept unchanged');
288+
assert.deepEqual(result, expected, 'additionalProperties set to false');
289+
});
290+
291+
test('handling additionalProperties with composition types', function(assert) {
292+
var schema
293+
, result
294+
, expected
295+
;
296+
297+
assert.plan(1);
298+
299+
schema = {
300+
type: 'object',
301+
additionalProperties: {
302+
oneOf: [
303+
{
304+
type: 'string'
305+
},
306+
{
307+
type: 'integer'
308+
}
309+
]
310+
},
311+
'x-patternProperties': {
312+
'^[a-z]*$': {
313+
oneOf: [
314+
{
315+
type: 'string'
316+
},
317+
{
318+
type: 'integer'
319+
}
320+
]
321+
}
322+
}
323+
};
324+
325+
result = convert(schema, {supportPatternProperties: true});
326+
327+
expected = {
328+
$schema: 'http://json-schema.org/draft-04/schema#',
329+
type: 'object',
330+
additionalProperties: false,
331+
patternProperties: {
332+
'^[a-z]*$': {
333+
oneOf: [
334+
{
335+
type: 'string'
336+
},
337+
{
338+
type: 'integer'
339+
}
340+
]
341+
}
342+
}
343+
};
344+
345+
assert.deepEqual(result, expected, 'additionalProperties set to false');
217346
});
218347

219348
test('not supporting patternProperties', function(assert) {
@@ -369,3 +498,4 @@ test('additionalProperties not modified if set to true', function(assert) {
369498

370499
assert.deepEqual(result, expected, 'additionalProperties not removed');
371500
});
501+

0 commit comments

Comments
 (0)