Skip to content

Commit

Permalink
fix: magically resolving objects that are typod as arrays (#132)
Browse files Browse the repository at this point in the history
  • Loading branch information
erunion authored Mar 20, 2020
1 parent 17e5aed commit 94c523b
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 7 deletions.
80 changes: 80 additions & 0 deletions packages/tooling/__tests__/lib/parameters-to-json-schema.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down
32 changes: 25 additions & 7 deletions packages/tooling/src/lib/parameters-to-json-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
}
});

Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 94c523b

Please sign in to comment.