Skip to content

Commit 5c98d17

Browse files
authored
Add multipart fix when does not exist any body (#905)
1 parent a7d67e7 commit 5c98d17

File tree

5 files changed

+64
-11
lines changed

5 files changed

+64
-11
lines changed

src/middlewares/openapi.request.validator.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,10 @@ export class RequestValidator {
204204
}
205205

206206
private multipartNested(req, schemaBody) {
207+
if (!req.body) {
208+
return;
209+
}
210+
207211
Object.keys(req.body).forEach((key) => {
208212
const value = req.body[key];
209213
// TODO: Add support for oneOf, anyOf, allOf as the body schema
@@ -216,7 +220,6 @@ export class RequestValidator {
216220
}
217221
}
218222
});
219-
return null;
220223
}
221224

222225
private discriminatorValidator(req, discriminator) {

test/common/app.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,22 @@ export async function createApp(
1313
port = 3000,
1414
customRoutes = (app) => {},
1515
useRoutes = true,
16-
apiRouter = undefined,
16+
useParsers = true,
1717
) {
1818
var app = express();
1919
(<any>app).basePath = '/v1';
2020

21-
app.use(bodyParser.json());
22-
app.use(bodyParser.json({ type: 'application/*+json' }));
23-
app.use(bodyParser.json({ type: 'application/*+json*' }));
21+
if (useParsers) {
22+
app.use(bodyParser.json());
23+
app.use(bodyParser.json({ type: 'application/*+json' }));
24+
app.use(bodyParser.json({ type: 'application/*+json*' }));
2425

25-
app.use(bodyParser.text());
26-
app.use(bodyParser.text({ type: 'text/html' }));
26+
app.use(bodyParser.text());
27+
app.use(bodyParser.text({ type: 'text/html' }));
28+
29+
app.use(express.urlencoded({ extended: false }));
30+
}
2731
app.use(logger('dev'));
28-
app.use(express.urlencoded({ extended: false }));
2932
app.use(cookieParser());
3033
app.use(express.static(path.join(__dirname, 'public')));
3134

test/multipart.spec.ts

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import * as request from 'supertest';
66
import { createApp } from './common/app';
77

88
describe('a multipart request', () => {
9-
let app = null;
9+
let app;
1010
const fileNames = [];
1111
before(async () => {
1212
const apiSpec = path.join('test', 'resources', 'multipart.yaml');
@@ -151,3 +151,41 @@ describe('a multipart request', () => {
151151
});
152152
});
153153
});
154+
155+
describe('when request does not use parsers', () => {
156+
let app;
157+
158+
after(() => {
159+
(<any>app).server.close();
160+
});
161+
162+
before(async () => {
163+
const apiSpec = path.join('test', 'resources', 'multipart.yaml');
164+
app = await createApp(
165+
{
166+
apiSpec,
167+
},
168+
3004,
169+
(app) =>
170+
app.use(
171+
`${app.basePath}`,
172+
express
173+
.Router()
174+
.post(`/sample_7`, (req, res) => res.json('ok')),
175+
),
176+
false,
177+
false,
178+
);
179+
});
180+
181+
182+
it('should validate that endpoint exists', async () => {
183+
await request(app)
184+
.post(`${app.basePath}/sample_7`)
185+
.set('Content-Type', 'multipart/form-data')
186+
.expect(200)
187+
.then((r) => {
188+
expect(r.body).to.equal('ok');
189+
});
190+
});
191+
});

test/nested.routes.spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ describe(packageJson.name, () => {
3737
})
3838
},
3939
true,
40-
apiRoute
4140
);
4241
});
4342

test/resources/multipart.yaml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,17 @@ paths:
119119
responses:
120120
201:
121121
description: Created
122-
122+
123+
/sample_7:
124+
post:
125+
description: upload a file that body is not consumed by router parsers
126+
requestBody:
127+
content:
128+
'*/*': {}
129+
responses:
130+
200:
131+
description: Updated
132+
123133
# TODO add test with ImageReqBody ref to Image
124134

125135
/range:

0 commit comments

Comments
 (0)