fix allOf with readOnly/writeOnly

resets the readOnly/writeOnly to default (undefined)
before merging it with next subschema

ref: https://github.com/Rebilly/ReDoc/issues/532
rel: https://github.com/Rebilly/ReDoc/issues/422
This commit is contained in:
Frode Aannevik 2018-06-16 01:34:19 +02:00
parent d38f2f2a5a
commit 88c4e35da2
2 changed files with 113 additions and 0 deletions

View File

@ -225,6 +225,10 @@ export class OpenAPIParser {
receiver.properties[prop] = subSchema.properties[prop];
} else {
// merge inner properties
// let readOnly/writeOnly be overridden if property is redefined
// i.e. fallback on default value of readOnly/writeOnly when merging
delete receiver.properties[prop].readOnly;
delete receiver.properties[prop].writeOnly;
receiver.properties[prop] = this.mergeAllOf(
{ allOf: [receiver.properties[prop], subSchema.properties[prop]] },
$ref + '/properties/' + prop,

View File

@ -0,0 +1,109 @@
import { convertObj } from 'swagger2openapi';
import { OpenAPIParser, RedocNormalizedOptions } from '../';
const options = new RedocNormalizedOptions({});
describe('OpenAPIParser mergeAllOf', async () => {
const parser = new OpenAPIParser(
{ openapi: '3.0', info: { title: 'test', version: '0' }, paths: {} },
undefined,
options,
);
it('merge readOnly from allOf', () => {
expect(parser.mergeAllOf({
allOf: [
{
properties: {
id: { type: 'string', readOnly: true },
name: { type: 'string' },
},
},
{ properties: { name: { type: 'string', readOnly: true } } },
],
}, '', false).properties).toEqual({
id: {
type: 'string',
readOnly: true,
},
name: {
type: 'string',
allOf: undefined,
parentRefs: [],
readOnly: true,
},
});
});
it('merge writeOnly from allOf', () => {
expect(parser.mergeAllOf({
allOf: [
{
properties: {
id: { type: 'string', readOnly: true },
name: { type: 'string' },
},
},
{ properties: { name: { type: 'string', writeOnly: true } } },
],
}, '', false).properties).toEqual({
id: {
type: 'string',
readOnly: true,
},
name: {
type: 'string',
allOf: undefined,
parentRefs: [],
writeOnly: true,
},
});
});
it('reset readOnly before merge', () => {
expect(parser.mergeAllOf({
allOf: [
{
properties: {
id: { type: 'string', readOnly: true },
name: { type: 'string' },
},
},
{ properties: { id: { type: 'string', readOnly: true } } },
{ properties: { id: { type: 'string' } } },
],
}, '', false).properties).toEqual({
id: {
allOf: undefined,
parentRefs: [],
type: 'string',
},
name: {
type: 'string',
},
});
});
it('reset writeOnly before merge', () => {
expect(parser.mergeAllOf({
allOf: [
{
properties: {
id: { type: 'string', readOnly: true },
name: { type: 'string' },
},
},
{ properties: { id: { type: 'string', writeOnly: true } } },
{ properties: { id: { type: 'string' } } },
],
}, '', false).properties).toEqual({
id: {
allOf: undefined,
parentRefs: [],
type: 'string',
},
name: {
type: 'string',
},
});
});
});