Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion packages/openapi-generator/src/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,17 @@ 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
132 changes: 132 additions & 0 deletions packages/openapi-generator/test/openapi/base.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -851,3 +851,135 @@ testCase('multiple routes', MULTIPLE_ROUTES, {
schemas: {},
},
});

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',
},
},
},
},
},
},
post: {
parameters: [
{
in: 'query',
name: 'foo',
required: true,
schema: {
type: 'string',
},
},
],
responses: {
200: {
description: 'OK',
content: {
'application/json': {
schema: {
type: 'string',
},
},
},
},
},
},
},
},
components: {
schemas: {},
},
});