Skip to content

Commit

Permalink
fix: merge allOf in correct order (#2020)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexVarchuk authored May 26, 2022
1 parent 82712c5 commit 1e4ea03
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 4 deletions.
13 changes: 12 additions & 1 deletion src/services/OpenAPIParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,9 @@ export class OpenAPIParser {
properties,
items,
required,
oneOf,
anyOf,
title,
...otherConstraints
} = subSchema;

Expand Down Expand Up @@ -324,9 +327,17 @@ export class OpenAPIParser {
receiver.required = (receiver.required || []).concat(required);
}

if (oneOf !== undefined) {
receiver.oneOf = oneOf;
}

if (anyOf !== undefined) {
receiver.anyOf = anyOf;
}

// merge rest of constraints
// TODO: do more intelligent merge
receiver = { ...receiver, ...otherConstraints };
receiver = { ...receiver, title: receiver.title || title, ...otherConstraints };

if (subSchemaRef) {
receiver.parentRefs!.push(subSchemaRef);
Expand Down
41 changes: 38 additions & 3 deletions src/services/__tests__/OpenAPIParser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,50 @@ describe('Models', () => {
let parser;

test('should hoist oneOfs when mergin allOf', () => {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const spec = require('./fixtures/oneOfHoist.json');
parser = new OpenAPIParser(spec, undefined, opts);
expect(parser.mergeAllOf(spec.components.schemas.test)).toMatchSnapshot();
});

test('should get schema name from named schema', () => {
const spec = require('./fixtures/mergeAllOf.json');
parser = new OpenAPIParser(spec, undefined, opts);
const schema = parser.mergeAllOf(spec.components.schemas.Case1, '#/components/schemas/Case1');
expect(schema.title).toEqual('Case1');
});

test('should get schema name from first allOf', () => {
const spec = require('./fixtures/mergeAllOf.json');
parser = new OpenAPIParser(spec, undefined, opts);
const schema = parser.mergeAllOf(
spec.components.schemas.Case2.properties.a,
'#components/schemas/Case2/properties/a',
);
expect(schema.title).toEqual('Bar');
});

test('should get schema name from named schema', () => {
const spec = require('./fixtures/mergeAllOf.json');
parser = new OpenAPIParser(spec, undefined, opts);
const schema = parser.mergeAllOf(
spec.components.schemas.Case3.schemas.Foo,
'#components/schemas/Case3/schemas/Foo',
);
expect(schema.title).toEqual('Foo');
});

test('should merge oneOff to inside allOff', () => {
// TODO: should hoist
const spec = require('./fixtures/mergeAllOf.json');
parser = new OpenAPIParser(spec, undefined, opts);
const schema = parser.mergeAllOf(spec.components.schemas.Case4);
expect(schema.title).toEqual('Foo');
expect(schema.parentRefs).toHaveLength(1);
expect(schema.parentRefs[0]).toEqual('#/components/schemas/Ref');
expect(schema.oneOf).toEqual([{ title: 'Bar' }, { title: 'Baz' }]);
});

test('should override description from $ref of the referenced component, when sibling description exists ', () => {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const spec = require('./fixtures/siblingRefDescription.json');
parser = new OpenAPIParser(spec, undefined, opts);
const schemaOrRef: Referenced<OpenAPIParameter> = {
Expand All @@ -28,7 +64,6 @@ describe('Models', () => {
});

test('should correct resolve double $ref if no need sibling', () => {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const spec = require('./fixtures/3.1/schemaDefinition.json');
parser = new OpenAPIParser(spec, undefined, opts);
const schemaOrRef: Referenced<OpenAPIParameter> = {
Expand Down
70 changes: 70 additions & 0 deletions src/services/__tests__/fixtures/mergeAllOf.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
{
"openapi": "3.0.0",
"info": {
"version": "1.0",
"title": "Foo"
},
"components": {
"schemas": {
"Case1": {
"allOf": [
{
"title": "Bar"
},
{
"title": "Baz"
}
]
},
"Case2": {
"properties": {
"a": {
"allOf": [
{
"title": "Bar"
},
{
"title": "Baz"
}
]
}
}
},
"Case3": {
"schemas": {
"Foo": {
"title": "Foo",
"allOf": [
{
"title": "Bar"
},
{
"title": "Baz"
}
]
}
}
},
"Case4": {
"allOf": [
{
"title": "Foo"
},
{
"$ref": "#/components/schemas/Ref"
}
]
},
"Ref": {
"oneOf": [
{
"title": "Bar"
},
{
"title": "Baz"
}
]
}
}
}
}

0 comments on commit 1e4ea03

Please sign in to comment.