Skip to content

Commit 5ed6340

Browse files
Merge pull request #927 from BitGo/bitgopatmcl/sort-http-verbs
Sort HTTP verbs within each path in OpenAPI spec
2 parents 4a0bba1 + cf10220 commit 5ed6340

File tree

2 files changed

+143
-1
lines changed

2 files changed

+143
-1
lines changed

packages/openapi-generator/src/openapi.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,17 @@ 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(
441+
(methodAcc, methodKey) => {
442+
methodAcc[methodKey] = paths[key]![methodKey]!;
443+
return methodAcc;
444+
},
445+
{} as Record<string, OpenAPIV3.PathItemObject>,
446+
);
447+
448+
acc[key] = sortedMethods;
439449
return acc;
440450
},
441451
{} as Record<string, OpenAPIV3.PathItemObject>,

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

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -851,3 +851,135 @@ testCase('multiple routes', MULTIPLE_ROUTES, {
851851
schemas: {},
852852
},
853853
});
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: {
907+
'/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+
},
932+
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: {
957+
parameters: [
958+
{
959+
in: 'query',
960+
name: 'foo',
961+
required: true,
962+
schema: {
963+
type: 'string',
964+
},
965+
},
966+
],
967+
responses: {
968+
200: {
969+
description: 'OK',
970+
content: {
971+
'application/json': {
972+
schema: {
973+
type: 'string',
974+
},
975+
},
976+
},
977+
},
978+
},
979+
},
980+
},
981+
},
982+
components: {
983+
schemas: {},
984+
},
985+
});

0 commit comments

Comments
 (0)