Skip to content

Commit

Permalink
fix: properly host oneOf inside allOf
Browse files Browse the repository at this point in the history
fixes #507
fixes #528
  • Loading branch information
RomanHotsiy committed Jul 18, 2018
1 parent e0f58dc commit 7e5b6d9
Show file tree
Hide file tree
Showing 4 changed files with 188 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/services/OpenAPIParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@ export class OpenAPIParser {
$ref?: string,
forceCircular: boolean = false,
): MergedOpenAPISchema {
schema = this.hoistOneOfs(schema);

if (schema.allOf === undefined) {
return schema;
}
Expand Down Expand Up @@ -291,4 +293,28 @@ export class OpenAPIParser {
}
return res;
}

private hoistOneOfs(schema: OpenAPISchema) {
if (schema.allOf === undefined) {
return schema;
}

const allOf = schema.allOf;
for (let i = 0; i < allOf.length; i++) {
const sub = allOf[i];
if (Array.isArray(sub.oneOf)) {
const beforeAllOf = allOf.slice(0, i);
const afterAllOf = allOf.slice(i + 1);
return {
oneOf: sub.oneOf.map(part => {
return this.mergeAllOf({
allOf: [...beforeAllOf, part, ...afterAllOf],
});
}),
};
}
}

return schema;
}
}
16 changes: 16 additions & 0 deletions src/services/__tests__/OpenAPIParser.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { OpenAPIParser } from '../OpenAPIParser';
import { RedocNormalizedOptions } from '../RedocNormalizedOptions';

const opts = new RedocNormalizedOptions({});

describe('Models', () => {
describe('Schema', () => {
let parser;

test('should hoist oneOfs when mergin allOf', () => {
const spec = require('./fixtures/oneOfHoist.json');
parser = new OpenAPIParser(spec, undefined, opts);
expect(parser.mergeAllOf(spec.components.schemas.test)).toMatchSnapshot();
});
});
});
84 changes: 84 additions & 0 deletions src/services/__tests__/__snapshots__/OpenAPIParser.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Models Schema should hoist oneOfs when mergin allOf 1`] = `
Object {
"oneOf": Array [
Object {
"oneOf": Array [
Object {
"allOf": undefined,
"parentRefs": Array [],
"properties": Object {
"extra": Object {
"type": "string",
},
"password": Object {
"description": "The user's password",
"type": "string",
},
"username": Object {
"description": "The user's name",
"type": "string",
},
},
},
Object {
"allOf": undefined,
"parentRefs": Array [],
"properties": Object {
"extra": Object {
"type": "string",
},
"mobile": Object {
"description": "The user's mobile",
"type": "string",
},
"username": Object {
"description": "The user's name",
"type": "string",
},
},
},
],
},
Object {
"oneOf": Array [
Object {
"allOf": undefined,
"parentRefs": Array [],
"properties": Object {
"email": Object {
"description": "The user's email",
"type": "string",
},
"extra": Object {
"type": "string",
},
"password": Object {
"description": "The user's password",
"type": "string",
},
},
},
Object {
"allOf": undefined,
"parentRefs": Array [],
"properties": Object {
"email": Object {
"description": "The user's email",
"type": "string",
},
"extra": Object {
"type": "string",
},
"mobile": Object {
"description": "The user's mobile",
"type": "string",
},
},
},
],
},
],
}
`;
62 changes: 62 additions & 0 deletions src/services/__tests__/fixtures/oneOfHoist.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{
"openapi": "3.0.0",
"info": {
"version": "1.0",
"title": "Foo"
},
"components": {
"schemas": {
"test": {
"allOf": [
{
"oneOf": [
{
"properties": {
"username": {
"description": "The user's name",
"type": "string"
}
}
},
{
"properties": {
"email": {
"description": "The user's email",
"type": "string"
}
}
}
]
},
{
"properties": {
"extra": {
"type": "string"
}
}
},
{
"oneOf": [
{
"properties": {
"password": {
"description": "The user's password",
"type": "string"
}
}
},
{
"properties": {
"mobile": {
"description": "The user's mobile",
"type": "string"
}
}
}
]
}
]
}
}
}
}

0 comments on commit 7e5b6d9

Please sign in to comment.