Skip to content
This repository has been archived by the owner on Nov 28, 2022. It is now read-only.

Commit

Permalink
Prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
Dom Harrington committed Jun 15, 2018
1 parent ad6cda5 commit de1747d
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 57 deletions.
12 changes: 9 additions & 3 deletions packages/api-explorer/__tests__/lib/get-schema.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,17 @@ test('should return if theres a $ref on the top level', () => {

// https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#requestBodyObject
test('should look up the schema if it looks like the first $ref is a request body object', () => {
const $ref = '#/components/schemas/schema'
const $ref = '#/components/schemas/schema';
expect(
getSchema(
{
requestBody: { $ref: '#/components/requestBodies/schema' },
},
{ components: { requestBodies: { schema: { content: { 'application/json': { schema: { $ref } } } } } } },
{
components: {
requestBodies: { schema: { content: { 'application/json': { schema: { $ref } } } } },
},
},
).schema.$ref,
).toEqual($ref);
});
Expand All @@ -65,7 +69,9 @@ test('should return the inline schema from request body object', () => {
{
requestBody: { $ref: '#/components/requestBodies/schema' },
},
{ components: { requestBodies: { schema: { content: { 'application/json': { schema } } } } } },
{
components: { requestBodies: { schema: { content: { 'application/json': { schema } } } } },
},
).schema,
).toEqual(schema);
});
111 changes: 62 additions & 49 deletions packages/api-explorer/__tests__/lib/parameters-to-json-schema.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,19 +76,22 @@ test('it should return with a json schema for each parameter type', () => {

test('it should work for request body inline (json)', () => {
expect(
parametersToJsonSchema({
requestBody: {
description: 'Body description',
content: {
'application/json': {
schema: {
type: 'object',
properties: { a: { type: 'string' } },
parametersToJsonSchema(
{
requestBody: {
description: 'Body description',
content: {
'application/json': {
schema: {
type: 'object',
properties: { a: { type: 'string' } },
},
},
},
},
},
}, {}),
{},
),
).toEqual([
{
label: 'Body Params',
Expand All @@ -105,19 +108,22 @@ test('it should work for request body inline (json)', () => {

test('it should work for request body inline (formData)', () => {
expect(
parametersToJsonSchema({
requestBody: {
description: 'Form data description',
content: {
'application/x-www-form-urlencoded': {
schema: {
type: 'object',
properties: { a: { type: 'string' } },
parametersToJsonSchema(
{
requestBody: {
description: 'Form data description',
content: {
'application/x-www-form-urlencoded': {
schema: {
type: 'object',
properties: { a: { type: 'string' } },
},
},
},
},
},
}, {}),
{},
),
).toEqual([
{
label: 'Form Data',
Expand Down Expand Up @@ -280,22 +286,24 @@ test('it should work for top-level request body $ref', () => {
},
},
),
).toEqual([{
type: 'body',
label: 'Body Params',
schema: {
$ref: '#/components/schemas/Pet',
definitions: {
components: {
schemas: {
Pet: {
type: 'string',
).toEqual([
{
type: 'body',
label: 'Body Params',
schema: {
$ref: '#/components/schemas/Pet',
definitions: {
components: {
schemas: {
Pet: {
type: 'string',
},
},
},
},
},
}
}]);
},
]);
});

test('it should pull out schemas from `components/requestBodies`', () => {
Expand All @@ -306,35 +314,40 @@ test('it should pull out schemas from `components/requestBodies`', () => {
content: {
'application/json': {
schema: {
$ref: '#/components/schemas/Pet'
}
}
}
$ref: '#/components/schemas/Pet',
},
},
},
},
},
schemas: {
Pet: {
type: 'string',
}
}
},
},
},
};
expect(
parametersToJsonSchema({
requestBody: {
$ref: '#/components/requestBodies/Pet',
parametersToJsonSchema(
{
requestBody: {
$ref: '#/components/requestBodies/Pet',
},
},
}, oas),
).toEqual([{
type: 'body',
label: 'Body Params',
schema: {
$ref: '#/components/schemas/Pet',
definitions: {
components: oas.components,
oas,
),
).toEqual([
{
type: 'body',
label: 'Body Params',
schema: {
$ref: '#/components/schemas/Pet',
definitions: {
components: oas.components,
},
},
}
}]);
},
]);
});

test('it should make things required correctly');
2 changes: 1 addition & 1 deletion packages/api-explorer/src/lib/find-schema-definition.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function findSchemaDefinition($ref, definitions = {}) {
// Extract and use the referenced definition if we have it.
const match = /^#\/(.*)$/.exec($ref);
if (match && match[0]) {
const parts = match[1].split("/");
const parts = match[1].split('/');
let current = definitions;

parts.forEach(part => {
Expand Down
7 changes: 5 additions & 2 deletions packages/api-explorer/src/lib/get-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ module.exports = function getSchema(pathOperation, oas) {
return { type, schema: pathOperation.requestBody.content[type].schema };
}

if (pathOperation.requestBody && pathOperation.requestBody.$ref.match(/^#\/components\/requestBodies\/.*$/)) {
if (
pathOperation.requestBody &&
pathOperation.requestBody.$ref.match(/^#\/components\/requestBodies\/.*$/)
) {
return getSchema({ requestBody: findSchemaDefinition(pathOperation.requestBody.$ref, oas) });
}

return { type: 'application/json', schema: pathOperation.requestBody }
return { type: 'application/json', schema: pathOperation.requestBody };
} catch (e) {} // eslint-disable-line no-empty

return undefined;
Expand Down
8 changes: 6 additions & 2 deletions packages/api-explorer/src/lib/parameters-to-json-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ function getBodyParam(pathOperation, oas) {
return {
type,
label: types[type],
schema: oas.components ? { definitions: { components: oas.components }, ...schema.schema } : schema.schema,
schema: oas.components
? { definitions: { components: oas.components }, ...schema.schema }
: schema.schema,
};
}

Expand Down Expand Up @@ -74,7 +76,9 @@ module.exports = (pathOperation, oas) => {

if (!hasParameters && !hasRequestBody) return null;

return [getBodyParam(pathOperation, oas)].concat(...getOtherParams(pathOperation)).filter(Boolean);
return [getBodyParam(pathOperation, oas)]
.concat(...getOtherParams(pathOperation))
.filter(Boolean);
};

// Exported for use in oas-to-har for default values object
Expand Down

0 comments on commit de1747d

Please sign in to comment.