Skip to content

Commit

Permalink
feat: pulling over more oas tooling from @readme/api-explorer (#104)
Browse files Browse the repository at this point in the history
* chore(deps-dev): upgrading @readme/eslint-config to 1.11.0

* feat: pulling over flattenArray and flattenSchema from @readme/api-explorer

* chore: re-adding .prettierrc, prettier doesn't pick it up from eslint

* docs: adding some comments to clarify ambiguous code
  • Loading branch information
erunion authored Jan 28, 2020
1 parent 4f3edc8 commit 6374fa2
Show file tree
Hide file tree
Showing 8 changed files with 289 additions and 529 deletions.
6 changes: 3 additions & 3 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"printWidth": 120,
"singleQuote": true,
"trailingComma": "es5"
printWidth: 120,
singleQuote: true,
trailingComma: 'es5',
}
6 changes: 6 additions & 0 deletions __tests__/lib/flatten-array.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const flattenArray = require('../../src/lib/flatten-array');

test('should flatten array', () => {
const array = [[1], [2, 3], [[4, 5]]];
expect(flattenArray(array)).toStrictEqual([1, 2, 3, 4, 5]);
});
89 changes: 89 additions & 0 deletions __tests__/lib/flatten-schema.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
const flattenSchema = require('../../src/lib/flatten-schema');

const petstore = require('../fixtures/petstore');

test('should flatten schema to an array', () => {
const schema = {
type: 'object',
properties: {
category: {
type: 'array',
items: {
type: 'string',
},
},
},
};

expect(flattenSchema(schema)).toStrictEqual([
{
name: 'category',
type: '[String]',
description: undefined,
},
]);
});

test('should flatten a component schema to an array', () => {
const schema = petstore.components.schemas.Pet;

expect(flattenSchema(schema, petstore)).toStrictEqual([
{ name: 'id', type: 'Integer', description: undefined },
{ name: 'category', type: 'Object', description: undefined },
{ name: 'category.id', type: 'Integer', description: undefined },
{ name: 'category.name', type: 'String', description: undefined },
{ name: 'name', type: 'String', description: undefined },
{ name: 'photoUrls', type: '[String]', description: undefined },
{ name: 'tags', type: '[Object]', description: undefined },
{ name: 'tags[].id', type: 'Integer', description: undefined },
{ name: 'tags[].name', type: 'String', description: undefined },
{ name: 'status', type: 'String', description: 'pet status in the store' },
]);
});

describe('$ref usages', () => {
const schema = {
properties: {
responses: {
type: 'array',
items: {
$ref: '#/components/schemas/ApiResponse',
},
},
tag: {
$ref: '#/components/schemas/Tag',
},
},
};

const expected = [
{ name: 'responses', type: '[Object]', description: undefined },
{ name: 'responses[].code', type: 'Integer', description: undefined },
{ name: 'responses[].type', type: 'String', description: undefined },
{ name: 'responses[].message', type: 'String', description: undefined },
{ name: 'tag', type: 'Object', description: undefined },
{ name: 'tag.id', type: 'Integer', description: undefined },
{ name: 'tag.name', type: 'String', description: undefined },
];

it('should flatten a schema that is a mix of objects and arrays', () => {
expect(flattenSchema(schema, petstore)).toStrictEqual(expected);
});

it('should flatten a schema that is a mix of objects and arrays but without explicit `type` properties set`', () => {
const oas = {
components: {
schemas: {
ApiResponse: {
properties: petstore.components.schemas.ApiResponse.properties,
},
Tag: {
properties: petstore.components.schemas.Tag.properties,
},
},
},
};

expect(flattenSchema(schema, oas)).toStrictEqual(expected);
});
});
Loading

0 comments on commit 6374fa2

Please sign in to comment.