Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: magically resolving objects that are typod as arrays #132

Merged
merged 1 commit into from
Mar 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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