Skip to content

Commit 820027a

Browse files
authored
Merge pull request #489 from postmanlabs/feature/fix-readonly-required-resolution
Fixed an issue where readOnly and writeOnly properties were not removed from required in resolved schema.
2 parents 298f731 + 4408ee4 commit 820027a

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

lib/deref.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,9 +258,17 @@ module.exports = {
258258
}
259259

260260
if (property.readOnly && parameterSourceOption === PARAMETER_SOURCE.REQUEST) {
261+
// remove property from required as it'll not be present in resolved schema
262+
if (_.includes(tempSchema.required, prop)) {
263+
_.remove(tempSchema.required, _.matches(prop));
264+
}
261265
continue;
262266
}
263267
else if (property.writeOnly && parameterSourceOption === PARAMETER_SOURCE.RESPONSE) {
268+
// remove property from required as it'll not be present in resolved schema
269+
if (_.includes(tempSchema.required, prop)) {
270+
_.remove(tempSchema.required, _.matches(prop));
271+
}
264272
continue;
265273
}
266274
/* eslint-enable */

test/unit/deref.test.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,66 @@ describe('DEREF FUNCTION TESTS ', function() {
255255
expect(output.pattern).to.eql(schema.pattern);
256256
done();
257257
});
258+
259+
it('should not contain readOnly properties in resolved schema if they are not contained' +
260+
' in resolved schema', function(done) {
261+
var schema = {
262+
type: 'object',
263+
required: ['id', 'name'],
264+
properties: {
265+
id: {
266+
type: 'integer',
267+
format: 'int64',
268+
readOnly: true
269+
},
270+
name: {
271+
type: 'string'
272+
},
273+
tag: {
274+
type: 'string',
275+
writeOnly: true
276+
}
277+
}
278+
},
279+
parameterSource = 'REQUEST',
280+
output;
281+
282+
output = deref.resolveRefs(schema, parameterSource, { concreteUtils: schemaUtils30X });
283+
expect(output.type).to.equal('object');
284+
expect(output.properties).to.not.haveOwnProperty('id');
285+
expect(output.required).to.not.include('id');
286+
done();
287+
});
288+
289+
it('should not contain writeOnly properties in resolved schema if they are not contained' +
290+
' in resolved schema', function(done) {
291+
var schema = {
292+
type: 'object',
293+
required: ['id', 'tag'],
294+
properties: {
295+
id: {
296+
type: 'integer',
297+
format: 'int64',
298+
readOnly: true
299+
},
300+
name: {
301+
type: 'string'
302+
},
303+
tag: {
304+
type: 'string',
305+
writeOnly: true
306+
}
307+
}
308+
},
309+
parameterSource = 'RESPONSE',
310+
output;
311+
312+
output = deref.resolveRefs(schema, parameterSource, { concreteUtils: schemaUtils30X });
313+
expect(output.type).to.equal('object');
314+
expect(output.properties).to.not.haveOwnProperty('tag');
315+
expect(output.required).to.not.include('tag');
316+
done();
317+
});
258318
});
259319

260320
describe('resolveAllOf Function', function () {

0 commit comments

Comments
 (0)