Skip to content

Commit

Permalink
feat: ensuring that exported definitions are sorted by path and method (
Browse files Browse the repository at this point in the history
  • Loading branch information
erunion authored Nov 4, 2021
1 parent df3a24d commit c272295
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 40 deletions.
64 changes: 32 additions & 32 deletions __tests__/__fixtures__/project-openapi/api.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,35 @@
/*
* @api [post] /pets
* description: "Creates a new pet in the store. Duplicates are allowed"
* operationId: "addPet"
* requestBody:
* description: "Pet to add to the store"
* required: true
* content:
* application/json:
* schema:
* $ref: "#/components/schemas/Pet"
* responses:
* "200":
* description: "pet response"
* content:
* application/json:
* schema:
* type: "array"
* items:
* $ref: "#/components/schemas/Pet"
* default:
* description: "unexpected error"
* content:
* application/json:
* schema:
* $ref: "#/components/schemas/ErrorModel"
*/

router.post('/pets', () => {

});

/*
* @api [get] /pets
* description: "Returns all pets from the system that the user has access to"
Expand Down Expand Up @@ -42,38 +74,6 @@ router.get('/pets', () => {

});

/*
* @api [post] /pets
* description: "Creates a new pet in the store. Duplicates are allowed"
* operationId: "addPet"
* requestBody:
* description: "Pet to add to the store"
* required: true
* content:
* application/json:
* schema:
* $ref: "#/components/schemas/Pet"
* responses:
* "200":
* description: "pet response"
* content:
* application/json:
* schema:
* type: "array"
* items:
* $ref: "#/components/schemas/Pet"
* default:
* description: "unexpected error"
* content:
* application/json:
* schema:
* $ref: "#/components/schemas/ErrorModel"
*/

router.post('/pets', () => {

});

/*
* @api [get] /pets/{id}
* description: "Returns a user based on a single ID, if the user does not have access to the pet"
Expand Down
16 changes: 8 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,20 @@ function outputResult(object, options) {
});
}

function sortObj(obj, compare) {
const sorted = compare ? Object.keys(obj).sort(compare) : Object.keys(obj);
return sorted.reduce(function (result, key) {
// eslint-disable-next-line no-param-reassign
result[key] = obj[key];
return result;
}, {});
}

function mergeEndpointsWithBase(swaggerBase = {}, endpoints = []) {
// To ensure consistent sorting of HTTP methods let's universally enforce the following order.
const methodPriority = new Map();
['get', 'post', 'put', 'patch', 'delete', 'options', 'head', 'trace'].forEach((x, i) => methodPriority.set(x, i));

return endpoints.reduce((prev, current) => {
const { method, route, ...operation } = current;
if (!method || !route) {
Expand All @@ -24,6 +37,12 @@ function mergeEndpointsWithBase(swaggerBase = {}, endpoints = []) {
if (!prev.paths[route]) prev.paths[route] = {};

prev.paths[route][method] = operation;

// Resort everything to be alphabetical.
prev.paths = sortObj(prev.paths);
prev.paths[route] = sortObj(prev.paths[route], (a, b) => {
return methodPriority.get(a) - methodPriority.get(b);
});
/* eslint-enable no-param-reassign */

return prev;
Expand All @@ -42,6 +61,7 @@ function mergeSchemasWithBase(swaggerBase = {}, schemas = []) {
if (!prev.components.schemas) prev.components.schemas = {};

prev.components.schemas[name] = schema;
prev.components.schemas = sortObj(prev.components.schemas);
/* eslint-enable no-param-reassign */

return prev;
Expand Down

0 comments on commit c272295

Please sign in to comment.