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

Add multipart fix when does not exist any body #905

Merged
merged 1 commit into from
Feb 21, 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
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;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

redundant return 🙂

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

function return it is not used

}

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,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Parameter was not used

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
Loading