From 2ed8e58d6459e188592e50f16290d33c36742a4a Mon Sep 17 00:00:00 2001 From: Jon Ursenbach Date: Fri, 20 Mar 2020 15:38:44 -0700 Subject: [PATCH] fix: magically resolving objects that are typod as arrays --- .../lib/parameters-to-json-schema.test.js | 80 +++++++++++++++++++ .../src/lib/parameters-to-json-schema.js | 32 ++++++-- 2 files changed, 105 insertions(+), 7 deletions(-) diff --git a/packages/tooling/__tests__/lib/parameters-to-json-schema.test.js b/packages/tooling/__tests__/lib/parameters-to-json-schema.test.js index e7e342d3..83fa6f34 100644 --- a/packages/tooling/__tests__/lib/parameters-to-json-schema.test.js +++ b/packages/tooling/__tests__/lib/parameters-to-json-schema.test.js @@ -392,6 +392,86 @@ describe('request bodies', () => { }); }); +describe('type', () => { + describe('parameters', () => { + it('should adjust an object that is typod as an array [README-6R]', () => { + const parameters = [ + { + name: 'param', + in: 'query', + schema: { + type: 'array', + properties: { + type: 'string', + }, + }, + }, + ]; + + expect(parametersToJsonSchema({ parameters })).toStrictEqual([ + { + label: 'Query Params', + schema: { + properties: { + param: { + type: 'array', + }, + }, + required: [], + type: 'object', + }, + type: 'query', + }, + ]); + }); + }); + + describe('request bodies', () => { + it('should adjust an object that is typod as an array [README-6R]', () => { + const oas = { + components: { + schemas: { + updatePets: { + required: ['name'], + type: 'array', + properties: { + name: { + type: 'string', + }, + }, + }, + }, + }, + }; + + const schema = parametersToJsonSchema( + { + requestBody: { + content: { + 'application/json': { + schema: { + type: 'array', + items: { + $ref: '#/components/schemas/updatePets', + }, + description: '', + }, + }, + }, + }, + }, + oas + ); + + expect(schema[0].schema.components.schemas.updatePets).toStrictEqual({ + properties: { name: { type: 'string' } }, + required: ['name'], + type: 'object', + }); + }); + }); +}); + describe('enums', () => { it.todo('should pass through enum on requestBody'); diff --git a/packages/tooling/src/lib/parameters-to-json-schema.js b/packages/tooling/src/lib/parameters-to-json-schema.js index affe68f0..0c03fcaa 100644 --- a/packages/tooling/src/lib/parameters-to-json-schema.js +++ b/packages/tooling/src/lib/parameters-to-json-schema.js @@ -45,6 +45,14 @@ function getBodyParam(pathOperation, oas) { if (obj[prop] === false) { delete obj[prop]; } + } else if (prop === 'type') { + // This is a fix to handle cases where someone may have typod `items` as `properties` on an array. Since + // throwing a complete failure isn't ideal, we can see that they meant for the type to be `object`, so we can do + // our best to shape the data into what they were intendint it to be. + // README-6R + if (obj[prop] === 'array' && !('items' in obj) && 'properties' in obj) { + obj.type = 'object'; + } } }); @@ -103,15 +111,25 @@ function getOtherParams(pathOperation, oas) { if (data.type === 'array') { schema.type = 'array'; - if (Object.keys(data.items).length === 1 && typeof data.items.$ref !== 'undefined') { - schema.items = findSchemaDefinition(data.items.$ref, oas); - } else { - schema.items = data.items; + if ('items' in data) { + if (Object.keys(data.items).length === 1 && typeof data.items.$ref !== 'undefined') { + schema.items = findSchemaDefinition(data.items.$ref, oas); + } else { + schema.items = data.items; + } + + // Run through the arrays contents and clean them up. + schema.items = constructSchema(schema.items); + } else if ('properties' in data || 'additionalProperties' in data) { + // This is a fix to handle cases where someone may have typod `items` as `properties` on an array. Since + // throwing a complete failure isn't ideal, we can see that they meant for the type to be `object`, so we can do + // our best to shape the data into what they were intendint it to be. + // README-6R + schema.type = 'object'; } + } - // Run through the arrays contents and clean them up. - schema.items = constructSchema(schema.items); - } else if (data.type === 'object') { + if (data.type === 'object') { schema.type = 'object'; if ('properties' in data) {