Skip to content

Commit 6187b98

Browse files
committed
Sort HTTP verbs within each path in OpenAPI spec
Sort HTTP verbs within each path in the OpenAPI specification. * Modify `convertRoutesToOpenAPI` function in `packages/openapi-generator/src/openapi.ts` to sort HTTP verbs within each path after sorting the paths. * Add a test case in `packages/openapi-generator/test/openapi/base.test.ts` to verify the sorting of HTTP verbs within each path.
1 parent 2f7dd3f commit 6187b98

File tree

2 files changed

+141
-2
lines changed

2 files changed

+141
-2
lines changed

packages/openapi-generator/src/openapi.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,14 @@ export function convertRoutesToOpenAPI(
435435
.sort((a, b) => a.localeCompare(b))
436436
.reduce(
437437
(acc, key) => {
438-
acc[key] = paths[key]!;
438+
const sortedMethods = Object.keys(paths[key]!)
439+
.sort((a, b) => a.localeCompare(b))
440+
.reduce((methodAcc, methodKey) => {
441+
methodAcc[methodKey] = paths[key]![methodKey]!;
442+
return methodAcc;
443+
}, {} as Record<string, OpenAPIV3.PathItemObject>);
444+
445+
acc[key] = sortedMethods;
439446
return acc;
440447
},
441448
{} as Record<string, OpenAPIV3.PathItemObject>,

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

Lines changed: 133 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -817,11 +817,143 @@ testCase('multiple routes', MULTIPLE_ROUTES, {
817817
},
818818
},
819819
},
820-
},
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+
],
821847
},
822848
},
849+
},
850+
components: {
851+
schemas: {},
852+
},
853+
});
854+
855+
const MULTIPLE_ROUTES_WITH_METHODS = `
856+
import * as t from 'io-ts';
857+
import * as h from '@api-ts/io-ts-http';
858+
859+
// Purposefully out of order to test sorting
860+
export const route1 = h.httpRoute({
861+
path: '/foo',
862+
method: 'POST',
863+
request: h.httpRequest({
864+
query: {
865+
foo: t.string,
866+
},
867+
}),
868+
response: {
869+
200: t.string
870+
},
871+
});
872+
873+
export const route2 = h.httpRoute({
874+
path: '/foo',
875+
method: 'GET',
876+
request: h.httpRequest({
877+
query: {
878+
foo: t.string,
879+
},
880+
}),
881+
response: {
882+
200: t.string
883+
},
884+
});
885+
886+
export const route3 = h.httpRoute({
887+
path: '/foo',
888+
method: 'DELETE',
889+
request: h.httpRequest({
890+
query: {
891+
foo: t.string,
892+
},
893+
}),
894+
response: {
895+
200: t.string
896+
},
897+
});
898+
`;
899+
900+
testCase('multiple routes with methods', MULTIPLE_ROUTES_WITH_METHODS, {
901+
openapi: '3.0.3',
902+
info: {
903+
title: 'Test',
904+
version: '1.0.0',
905+
},
906+
paths: {
823907
'/foo': {
908+
delete: {
909+
parameters: [
910+
{
911+
in: 'query',
912+
name: 'foo',
913+
required: true,
914+
schema: {
915+
type: 'string',
916+
},
917+
},
918+
],
919+
responses: {
920+
200: {
921+
description: 'OK',
922+
content: {
923+
'application/json': {
924+
schema: {
925+
type: 'string',
926+
},
927+
},
928+
},
929+
},
930+
},
931+
},
824932
get: {
933+
parameters: [
934+
{
935+
in: 'query',
936+
name: 'foo',
937+
required: true,
938+
schema: {
939+
type: 'string',
940+
},
941+
},
942+
],
943+
responses: {
944+
200: {
945+
description: 'OK',
946+
content: {
947+
'application/json': {
948+
schema: {
949+
type: 'string',
950+
},
951+
},
952+
},
953+
},
954+
],
955+
},
956+
post: {
825957
parameters: [
826958
{
827959
in: 'query',

0 commit comments

Comments
 (0)