diff --git a/core/base-service/openapi.js b/core/base-service/openapi.js index d87302a94fff44bb50d502f5a004fa290ba76d9a..d61991c54817f6e9b940a1c143d70758ce3af71a 100644 --- a/core/base-service/openapi.js +++ b/core/base-service/openapi.js @@ -198,7 +198,13 @@ function addGlobalProperties(endpoints) { return paths } -function services2openapi(services) { +function sortPaths(obj) { + const entries = Object.entries(obj) + entries.sort((a, b) => a[1].get.summary.localeCompare(b[1].get.summary)) + return Object.fromEntries(entries) +} + +function services2openapi(services, sort) { const paths = {} for (const service of services) { if (service.openApi) { @@ -221,10 +227,10 @@ function services2openapi(services) { } } } - return paths + return sort ? sortPaths(paths) : paths } -function category2openapi(category, services) { +function category2openapi({ category, services, sort = false }) { const spec = { openapi: '3.0.0', info: { @@ -334,7 +340,7 @@ function category2openapi(category, services) { }, }, }, - paths: services2openapi(services), + paths: services2openapi(services, sort), } return spec diff --git a/core/base-service/openapi.spec.js b/core/base-service/openapi.spec.js index 1ed1c1e9da38ba96dcb4a0e0d690ad6e304da0af..35bfa563b6bd9fc4df48fea312b7620fe1b04681 100644 --- a/core/base-service/openapi.spec.js +++ b/core/base-service/openapi.spec.js @@ -377,10 +377,13 @@ describe('category2openapi', function () { it('generates an Open API spec', function () { expect( clean( - category2openapi({ name: 'build' }, [ - OpenApiService.getDefinition(), - LegacyService.getDefinition(), - ]), + category2openapi({ + category: { name: 'build' }, + services: [ + OpenApiService.getDefinition(), + LegacyService.getDefinition(), + ], + }), ), ).to.deep.equal(expected) }) diff --git a/scripts/export-openapi-cli.js b/scripts/export-openapi-cli.js index f35bc049407e8f679da5c3a7804eb865b0c3bf55..0a1beb8bfa86db53e8421ba20ed88aa2098679f6 100644 --- a/scripts/export-openapi-cli.js +++ b/scripts/export-openapi-cli.js @@ -27,7 +27,7 @@ function writeSpec(filename, spec) { writeSpec( path.join(specsPath, `${category.id}.yaml`), - category2openapi(category, services), + category2openapi({ category, services, sort: true }), ) } @@ -44,6 +44,10 @@ function writeSpec(filename, spec) { ) writeSpec( path.join(specsPath, '1core.yaml'), - category2openapi({ name: 'Core' }, coreServices), + category2openapi({ + category: { name: 'Core' }, + services: coreServices, + sort: false, + }), ) })()