Skip to content

Commit

Permalink
Add multipart fix when does not exist any body (#905)
Browse files Browse the repository at this point in the history
  • Loading branch information
hiuxmaycry committed Feb 21, 2024
1 parent a7d67e7 commit 5c98d17
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 11 deletions.
5 changes: 4 additions & 1 deletion src/middlewares/openapi.request.validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,10 @@ export class RequestValidator {
}

private multipartNested(req, schemaBody) {
if (!req.body) {
return;
}

Object.keys(req.body).forEach((key) => {
const value = req.body[key];
// TODO: Add support for oneOf, anyOf, allOf as the body schema
Expand All @@ -216,7 +220,6 @@ export class RequestValidator {
}
}
});
return null;
}

private discriminatorValidator(req, discriminator) {
Expand Down
17 changes: 10 additions & 7 deletions test/common/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,22 @@ export async function createApp(
port = 3000,
customRoutes = (app) => {},
useRoutes = true,
apiRouter = undefined,
useParsers = true,
) {
var app = express();
(<any>app).basePath = '/v1';

app.use(bodyParser.json());
app.use(bodyParser.json({ type: 'application/*+json' }));
app.use(bodyParser.json({ type: 'application/*+json*' }));
if (useParsers) {
app.use(bodyParser.json());
app.use(bodyParser.json({ type: 'application/*+json' }));
app.use(bodyParser.json({ type: 'application/*+json*' }));

app.use(bodyParser.text());
app.use(bodyParser.text({ type: 'text/html' }));
app.use(bodyParser.text());
app.use(bodyParser.text({ type: 'text/html' }));

app.use(express.urlencoded({ extended: false }));
}
app.use(logger('dev'));
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

Expand Down
40 changes: 39 additions & 1 deletion test/multipart.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as request from 'supertest';
import { createApp } from './common/app';

describe('a multipart request', () => {
let app = null;
let app;
const fileNames = [];
before(async () => {
const apiSpec = path.join('test', 'resources', 'multipart.yaml');
Expand Down Expand Up @@ -151,3 +151,41 @@ describe('a multipart request', () => {
});
});
});

describe('when request does not use parsers', () => {
let app;

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

before(async () => {
const apiSpec = path.join('test', 'resources', 'multipart.yaml');
app = await createApp(
{
apiSpec,
},
3004,
(app) =>
app.use(
`${app.basePath}`,
express
.Router()
.post(`/sample_7`, (req, res) => res.json('ok')),
),
false,
false,
);
});


it('should validate that endpoint exists', async () => {
await request(app)
.post(`${app.basePath}/sample_7`)
.set('Content-Type', 'multipart/form-data')
.expect(200)
.then((r) => {
expect(r.body).to.equal('ok');
});
});
});
1 change: 0 additions & 1 deletion test/nested.routes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ describe(packageJson.name, () => {
})
},
true,
apiRoute
);
});

Expand Down
12 changes: 11 additions & 1 deletion test/resources/multipart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,17 @@ paths:
responses:
201:
description: Created


/sample_7:
post:
description: upload a file that body is not consumed by router parsers
requestBody:
content:
'*/*': {}
responses:
200:
description: Updated

# TODO add test with ImageReqBody ref to Image

/range:
Expand Down

0 comments on commit 5c98d17

Please sign in to comment.