Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes method res with path params #1032

Merged
merged 2 commits into from
Dec 27, 2024
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
2 changes: 2 additions & 0 deletions src/framework/openapi.spec.loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ interface DiscoveredRoutes {
export const sortRoutes = (r1, r2) => {
const e1 = r1.expressRoute.replace(/\/:/g, '/~');
const e2 = r2.expressRoute.replace(/\/:/g, '/~');
if (e1.startsWith(e2)) return -1;
else if (e2.startsWith(e1)) return 1;
return e1 > e2 ? 1 : -1;
};

Expand Down
130 changes: 130 additions & 0 deletions test/1022.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
import * as express from 'express';
import * as request from 'supertest';
import { createApp } from './common/app';
import * as packageJson from '../package.json';
import { OpenAPIV3 } from '../src/framework/types';
import {expect} from "chai";

describe(packageJson.name, () => {
let app = null;
before(async () => {
// Set up the express app
const apiSpec: OpenAPIV3.DocumentV3 = {
openapi: '3.0.0',
info: { title: 'Api test', version: '1.0.0' },
servers: [{ url: '/api' }],
paths: {
'/test/{id}': {
description: 'Description',
parameters: [
{
name: 'id',
in: 'path',
required: true,
schema: { type: 'string' },
},
],
get: {
responses: {
'200': {
description: 'response',
content: {
'application/json': {
schema: {
type: 'object',
properties: {
id: { type: 'string' },
label: { type: 'string' },
},
},
},
},
},
},
},
},
'/test/{id}:clone': {
description: 'Description',
parameters: [
{
name: 'id',
in: 'path',
required: true,
schema: { type: 'string' },
},
],

post: {
requestBody: {
content: {
'application/json': {
schema: {
type: 'object',
required: ['id'],
properties: {
id: {
type: 'integer'
}
}
}
}
}
},
responses: {
'200': {
description: 'response',
content: {
'application/json': {
schema: {
type: 'object',
properties: {
id: { type: 'string' },
label: { type: 'string' },
},
},
},
},
},
},
},
},
},
};

app = await createApp(
{
apiSpec,
validateRequests: true,
validateResponses: true,
},
3005,
(app) =>
app.use(
express
.Router()
.get(`/api/test/:id`, (req, res) => res.status(200).json({ id: 'id-test', label: 'label'}))
.post(`/api/test/:id:clone`, (req, res) => res.status(200).json({...req.body, id: 'id-test'})),
),
);
});

after(() => {
app.server.close();
});

it('get /test/{id} should return 200', async () =>
request(app).get(`/api/test/abc123`).expect(200));

it('POST /test/{id}:clone should return 200', async () =>
request(app).post(`/api/test/abc123:clone`)
.send({ id: 10 })
.expect(200));

it('POST /test/{id}:clone should return 400', async () =>
request(app).post(`/api/test/abc123:clone`)
.send({ id: 'abc123' })
.expect(400)
.then(r => {
expect(r.body.message).to.include('id must be integer');
}));
});
Loading