Skip to content

Commit

Permalink
feat!: sort routes by path before writing the output JSON
Browse files Browse the repository at this point in the history
* Modify `convertRoutesToOpenAPI` function in `packages/openapi-generator/src/openapi.ts` to sort routes by path before constructing the `paths` object.
* Add test case in `packages/openapi-generator/test/openapi/base.test.ts` to verify routes are sorted by path in the output JSON.
  • Loading branch information
bitgopatmcl committed Oct 22, 2024
1 parent 65ded53 commit 821537e
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 2 deletions.
12 changes: 11 additions & 1 deletion packages/openapi-generator/src/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -431,11 +431,21 @@ export function convertRoutesToOpenAPI(
{} as Record<string, OpenAPIV3.SchemaObject | OpenAPIV3.ReferenceObject>,
);

const sortedPaths = Object.keys(paths)
.sort()
.reduce(
(acc, key) => {
acc[key] = paths[key]!;
return acc;
},
{} as Record<string, OpenAPIV3.PathItemObject>,
);

return {
openapi: '3.0.3',
info,
...(servers.length > 0 ? { servers } : {}),
paths,
paths: sortedPaths,
components: {
schemas: openapiSchemas,
},
Expand Down
137 changes: 136 additions & 1 deletion packages/openapi-generator/test/openapi/base.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -714,4 +714,139 @@ testCase('route with array union of null and undefined', ROUTE_WITH_ARRAY_UNION_
components: {
schemas: {}
}
});
});

const MULTIPLE_ROUTES = `
import * as t from 'io-ts';
import * as h from '@api-ts/io-ts-http';
export const route1 = h.httpRoute({
path: '/bar',
method: 'GET',
request: h.httpRequest({
query: {
bar: t.string,
},
}),
response: {
200: t.string
},
});
export const route2 = h.httpRoute({
path: '/baz',
method: 'GET',
request: h.httpRequest({
query: {
baz: t.string,
},
}),
response: {
200: t.string
},
});
export const route3 = h.httpRoute({
path: '/foo',
method: 'GET',
request: h.httpRequest({
query: {
foo: t.string,
},
}),
response: {
200: t.string
},
});
`;

testCase('multiple routes', MULTIPLE_ROUTES, {
openapi: '3.0.3',
info: {
title: 'Test',
version: '1.0.0',
},
paths: {
'/bar': {
get: {
parameters: [
{
in: 'query',
name: 'bar',
required: true,
schema: {
type: 'string',
},
},
],
responses: {
200: {
description: 'OK',
content: {
'application/json': {
schema: {
type: 'string',
},
},
},
},
},
},
},
'/baz': {
get: {
parameters: [
{
in: 'query',
name: 'baz',
required: true,
schema: {
type: 'string',
},
},
],
responses: {
200: {
description: 'OK',
content: {
'application/json': {
schema: {
type: 'string',
},
},
},
},
},
},
},
'/foo': {
get: {
parameters: [
{
in: 'query',
name: 'foo',
required: true,
schema: {
type: 'string',
},
},
],
responses: {
200: {
description: 'OK',
content: {
'application/json': {
schema: {
type: 'string',
},
},
},
},
},
},
},
},
components: {
schemas: {},
},
});

0 comments on commit 821537e

Please sign in to comment.