Skip to content

Commit

Permalink
fix: edgecase where allOfs would receive a type when one wasn't pre…
Browse files Browse the repository at this point in the history
…sent (#374)
  • Loading branch information
erunion authored Feb 18, 2021
1 parent 4fd8b42 commit 07cd8ef
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 3 deletions.
50 changes: 50 additions & 0 deletions __tests__/tooling/operation/get-parameters-as-json-schema.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,56 @@ describe('request bodies', () => {
});

describe('polymorphism / inheritance', () => {
describe('adding missing `type` properties', () => {
it("should not add a `type` to a shapeless-description that's part of an `allOf`", () => {
const oas = createOas({
requestBody: {
content: {
'application/json': {
schema: {
type: 'object',
properties: {
petIds: {
allOf: [{ type: 'array', items: { type: 'string' } }, { description: 'Parameter description' }],
},
},
},
},
},
},
});

const schema = oas.operation('/', 'get').getParametersAsJsonSchema();

expect(schema[0].schema.properties.petIds.allOf[1].type).toBeUndefined();
});

it.each([['anyOf'], ['oneOf']])("should add a `type` to a shapeless-description that's part of an `%s`", prop => {
const oas = createOas({
requestBody: {
content: {
'application/json': {
schema: {
type: 'object',
properties: {
petIds: {
[prop]: [{ type: 'array', items: { type: 'string' } }, { description: 'Parameter description' }],
},
},
},
},
},
},
});

const schema = oas.operation('/', 'get').getParametersAsJsonSchema();
expect(schema[0].schema.properties.petIds[prop][1]).toStrictEqual({
description: 'Parameter description',
type: 'string',
});
});
});

it.each([['allOf'], ['anyOf'], ['oneOf']])('should support nested %s', prop => {
const oas = createOas({
requestBody: {
Expand Down
28 changes: 25 additions & 3 deletions tooling/operation/get-parameters-as-json-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,17 @@ function searchForExampleByPointer(pointer, examples = []) {
* @link https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.3.md
* @param {Object} data
* @param {Object[]} prevSchemas
* @param {string} currentLocation
* @param {String} currentLocation
* @param {Object} globalDefaults
* @param {Boolean} isPolymorphicAllOfChild
*/
function constructSchema(data, prevSchemas = [], currentLocation = '', globalDefaults) {
function constructSchema(
data,
prevSchemas = [],
currentLocation = '',
globalDefaults,
isPolymorphicAllOfChild = false
) {
const schema = { ...data };

// If this schema contains a `$ref`, it's circular and we shouldn't try to resolve it. Just return and move along.
Expand All @@ -197,6 +204,15 @@ function constructSchema(data, prevSchemas = [], currentLocation = '', globalDef
schema.type = 'object';
} else if ('items' in schema) {
schema.type = 'array';
} else if (isPolymorphicAllOfChild) {
// If this schema is immediate child of a polymorphic schema and is neither an array or an object, we should
// leave it alone. Cases like this are common where somebody might use `allOf` in order to dynamically add a
// `description` onto another schema, like such:
//
// allOf: [
// { type: 'array', items: { type: 'string' },
// { description: 'This is the description for the `array`.' }
// ]
} else {
// If we're processing a schema that has no types, no refs, and is just a lone schema, we should treat it at the
// bare minimum as a simple string so we make an attempt to generate valid JSON Schema.
Expand Down Expand Up @@ -330,7 +346,13 @@ function constructSchema(data, prevSchemas = [], currentLocation = '', globalDef
['allOf', 'anyOf', 'oneOf'].forEach(polyType => {
if (polyType in schema && Array.isArray(schema[polyType])) {
schema[polyType].forEach((item, idx) => {
schema[polyType][idx] = constructSchema(item, prevSchemas, `${currentLocation}/${idx}`, globalDefaults);
schema[polyType][idx] = constructSchema(
item,
prevSchemas,
`${currentLocation}/${idx}`,
globalDefaults,
polyType === 'allOf'
);
});
}
});
Expand Down

0 comments on commit 07cd8ef

Please sign in to comment.