Skip to content

Commit

Permalink
Sort HTTP verbs within each path in OpenAPI spec
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bitgopatmcl committed Oct 24, 2024
1 parent 2f7dd3f commit 6187b98
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 2 deletions.
9 changes: 8 additions & 1 deletion packages/openapi-generator/src/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,14 @@ export function convertRoutesToOpenAPI(
.sort((a, b) => a.localeCompare(b))
.reduce(
(acc, key) => {
acc[key] = paths[key]!;
const sortedMethods = Object.keys(paths[key]!)
.sort((a, b) => a.localeCompare(b))
.reduce((methodAcc, methodKey) => {
methodAcc[methodKey] = paths[key]![methodKey]!;
return methodAcc;
}, {} as Record<string, OpenAPIV3.PathItemObject>);

acc[key] = sortedMethods;
return acc;
},
{} as Record<string, OpenAPIV3.PathItemObject>,
Expand Down
134 changes: 133 additions & 1 deletion packages/openapi-generator/test/openapi/base.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -817,11 +817,143 @@ testCase('multiple routes', MULTIPLE_ROUTES, {
},
},
},
},
],

Check notice

Code scanning / CodeQL

Syntax error Note test

Error: Property assignment expected.

Check notice

Code scanning / CodeQL

Syntax error Note test

Error: Property assignment expected.
},
},
'/foo': {
get: {
parameters: [
{
in: 'query',
name: 'foo',
required: true,
schema: {
type: 'string',
},
},
],
responses: {
200: {
description: 'OK',
content: {
'application/json': {
schema: {
type: 'string',
},
},
},
},
],

Check notice

Code scanning / CodeQL

Syntax error Note test

Error: Property assignment expected.

Check notice

Code scanning / CodeQL

Syntax error Note test

Error: Property assignment expected.
},
},
},
components: {
schemas: {},
},
});

Check notice

Code scanning / CodeQL

Syntax error Note test

Error: ',' expected.

const MULTIPLE_ROUTES_WITH_METHODS = `
import * as t from 'io-ts';
import * as h from '@api-ts/io-ts-http';
// Purposefully out of order to test sorting
export const route1 = h.httpRoute({
path: '/foo',
method: 'POST',
request: h.httpRequest({
query: {
foo: t.string,
},
}),
response: {
200: t.string
},
});
export const route2 = h.httpRoute({
path: '/foo',
method: 'GET',
request: h.httpRequest({
query: {
foo: t.string,
},
}),
response: {
200: t.string
},
});
export const route3 = h.httpRoute({
path: '/foo',
method: 'DELETE',
request: h.httpRequest({
query: {
foo: t.string,
},
}),
response: {
200: t.string
},
});
`;

testCase('multiple routes with methods', MULTIPLE_ROUTES_WITH_METHODS, {
openapi: '3.0.3',
info: {
title: 'Test',
version: '1.0.0',
},
paths: {
'/foo': {
delete: {
parameters: [
{
in: 'query',
name: 'foo',
required: true,
schema: {
type: 'string',
},
},
],
responses: {
200: {
description: 'OK',
content: {
'application/json': {
schema: {
type: 'string',
},
},
},
},
},
},
get: {
parameters: [
{
in: 'query',
name: 'foo',
required: true,
schema: {
type: 'string',
},
},
],
responses: {
200: {
description: 'OK',
content: {
'application/json': {
schema: {
type: 'string',
},
},
},
},
],

Check notice

Code scanning / CodeQL

Syntax error Note test

Error: Property assignment expected.

Check notice

Code scanning / CodeQL

Syntax error Note test

Error: Property assignment expected.
},
post: {
parameters: [
{
in: 'query',
Expand Down

0 comments on commit 6187b98

Please sign in to comment.