Skip to content

Commit 2f7dd3f

Browse files
Merge pull request #925 from BitGo/bitgopatmcl/sort-routes-by-path
Sort routes by path before writing the output JSON in openapi-generator
2 parents 65ded53 + b014517 commit 2f7dd3f

File tree

2 files changed

+152
-6
lines changed

2 files changed

+152
-6
lines changed

packages/openapi-generator/src/openapi.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,11 +431,21 @@ export function convertRoutesToOpenAPI(
431431
{} as Record<string, OpenAPIV3.SchemaObject | OpenAPIV3.ReferenceObject>,
432432
);
433433

434+
const sortedPaths = Object.keys(paths)
435+
.sort((a, b) => a.localeCompare(b))
436+
.reduce(
437+
(acc, key) => {
438+
acc[key] = paths[key]!;
439+
return acc;
440+
},
441+
{} as Record<string, OpenAPIV3.PathItemObject>,
442+
);
443+
434444
return {
435445
openapi: '3.0.3',
436446
info,
437447
...(servers.length > 0 ? { servers } : {}),
438-
paths,
448+
paths: sortedPaths,
439449
components: {
440450
schemas: openapiSchemas,
441451
},

packages/openapi-generator/test/openapi/base.test.ts

Lines changed: 141 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -552,8 +552,8 @@ export const route = h.httpRoute({
552552
method: 'GET',
553553
request: h.httpRequest({
554554
query: {
555-
/**
556-
* This is a foo description.
555+
/**
556+
* This is a foo description.
557557
* @example abc
558558
* @pattern ^[a-z]+$
559559
*/
@@ -642,8 +642,8 @@ export const route = h.httpRoute({
642642
method: 'GET',
643643
request: h.httpRequest({
644644
query: {
645-
/**
646-
* This is a foo description.
645+
/**
646+
* This is a foo description.
647647
* @example abc
648648
* @pattern ^[a-z]+$
649649
*/
@@ -714,4 +714,140 @@ testCase('route with array union of null and undefined', ROUTE_WITH_ARRAY_UNION_
714714
components: {
715715
schemas: {}
716716
}
717-
});
717+
});
718+
719+
const MULTIPLE_ROUTES = `
720+
import * as t from 'io-ts';
721+
import * as h from '@api-ts/io-ts-http';
722+
723+
// Purposefully out of order to test sorting
724+
export const route1 = h.httpRoute({
725+
path: '/foo',
726+
method: 'GET',
727+
request: h.httpRequest({
728+
query: {
729+
foo: t.string,
730+
},
731+
}),
732+
response: {
733+
200: t.string
734+
},
735+
});
736+
737+
export const route2 = h.httpRoute({
738+
path: '/bar',
739+
method: 'GET',
740+
request: h.httpRequest({
741+
query: {
742+
bar: t.string,
743+
},
744+
}),
745+
response: {
746+
200: t.string
747+
},
748+
});
749+
750+
export const route3 = h.httpRoute({
751+
path: '/baz',
752+
method: 'GET',
753+
request: h.httpRequest({
754+
query: {
755+
baz: t.string,
756+
},
757+
}),
758+
response: {
759+
200: t.string
760+
},
761+
});
762+
`;
763+
764+
testCase('multiple routes', MULTIPLE_ROUTES, {
765+
openapi: '3.0.3',
766+
info: {
767+
title: 'Test',
768+
version: '1.0.0',
769+
},
770+
paths: {
771+
'/bar': {
772+
get: {
773+
parameters: [
774+
{
775+
in: 'query',
776+
name: 'bar',
777+
required: true,
778+
schema: {
779+
type: 'string',
780+
},
781+
},
782+
],
783+
responses: {
784+
200: {
785+
description: 'OK',
786+
content: {
787+
'application/json': {
788+
schema: {
789+
type: 'string',
790+
},
791+
},
792+
},
793+
},
794+
},
795+
},
796+
},
797+
'/baz': {
798+
get: {
799+
parameters: [
800+
{
801+
in: 'query',
802+
name: 'baz',
803+
required: true,
804+
schema: {
805+
type: 'string',
806+
},
807+
},
808+
],
809+
responses: {
810+
200: {
811+
description: 'OK',
812+
content: {
813+
'application/json': {
814+
schema: {
815+
type: 'string',
816+
},
817+
},
818+
},
819+
},
820+
},
821+
},
822+
},
823+
'/foo': {
824+
get: {
825+
parameters: [
826+
{
827+
in: 'query',
828+
name: 'foo',
829+
required: true,
830+
schema: {
831+
type: 'string',
832+
},
833+
},
834+
],
835+
responses: {
836+
200: {
837+
description: 'OK',
838+
content: {
839+
'application/json': {
840+
schema: {
841+
type: 'string',
842+
},
843+
},
844+
},
845+
},
846+
},
847+
},
848+
},
849+
},
850+
components: {
851+
schemas: {},
852+
},
853+
});

0 commit comments

Comments
 (0)