I'm trying to overwrite the GlobalAcceptMimesMiddleware but the original implementation is still getting called.
How do i know?
I Debugged and my Middleware does not throw an Error while the NotAcceptable Error is still beeing thrown (by tsed middleware)
/src/Server.ts
@Configuration({
...
acceptMimes: ['application/json', 'text'],
...
})
...
/src/middlewares/GlobalAcceptMimesMiddlesware.ts
import {Constant, GlobalAcceptMimesMiddleware as TSedGlobalAcceptMimesMiddleware, IMiddleware, OverrideProvider, Req} from '@tsed/common';
import {NotAcceptable} from '@tsed/exceptions';
@OverrideProvider(TSedGlobalAcceptMimesMiddleware)
export default class GlobalAcceptMimesMiddleware implements IMiddleware {
@Constant('acceptMimes')
acceptMimes: string[];
use(@Req() request: Req): void {
const acceptsMimeType = (type: string) => {
return request.accepts(type) || request.header('accept') === type;
};
if (!this.acceptMimes.some((mime) => acceptsMimeType(mime))) {
throw new NotAcceptable('Accepted mimes are: ' + this.acceptMimes.join(', '));
}
}
}
/src/controller/RootController.ts
import {AcceptMime, Controller, Get, Res} from '@tsed/common';
import {Response} from 'express';
import {ContentType} from '@tsed/schema';
@Controller('/')
export default class RootController {
@Get('/ping')
@ContentType('text')
@AcceptMime('text')
public ping(@Res() res: Response) {
res.write('pong');
res.end();
}
}
You have to add in your configuration:
@Configuration({
acceptMimes: ['application/json', ...]
})
The current implementation: https://github.com/TypedProject/tsed/blob/production/packages/common/src/platform/middlewares/GlobalAcceptMimesMiddleware.ts
Why do you need to override this middleware? Maybe there is something wrong in the current middleware?
Romain
i did add those to the server settings (extended my examples above).
i had to rewrite it because the original does not seem to work for me (when using the official swagger ui demo with the provided example above)
the "request.accepts()" function seems to be wrong/not working as it allways returns false even if a "accept" header is present
Yes because you use the raw Res. You shouldn't use the Express.Response except if the usecase isn't covered by Ts.ED itself (rare).
For your usecase, Override GlobalAcceptMimesMiddlesware won't have effect. There are two middlewares:
if you use the Accept decorator, the correct example is:
import {AcceptMime, Controller, Get, Res} from '@tsed/common';
import {Response} from 'express';
import {ContentType} from '@tsed/schema';
@Controller('/')
export default class RootController {
@Get('/ping')
@ContentType('text')
@AcceptMime('text')
public ping() { // don't use Res for this usecase
return "pong";
}
}
I'm not satisfied by the AcceptMimesMiddleware implementation also. There is a conflict between ResponseFilter, GlobalAcceptMimesMiddlesware and the AcceptMimesMiddleware. This is why I ask you about the problem with the middleware.
Romain
i tried your solution.
still getting the error:
{
"name": "NOT_ACCEPTABLE",
"message": "You must accept content-type application/json, text",
"status": 406,
"errors": [],
"stack": "NOT_ACCEPTABLE: You must accept content-type application/json, text\n at GlobalAcceptMimesMiddleware.use (/Users/jappy/code/GamePlay X/tsed-getting-started/node_modules/@tsed/common/src/platform/middlewares/GlobalAcceptMimesMiddleware.ts:17:13)\n at HandlerContext.callHandler (/Users/jappy/code/GamePlay X/tsed-getting-started/node_modules/@tsed/common/src/platform/domain/HandlerContext.ts:119:19)\n at PlatformExpressHandler.onRequest (/Users/jappy/code/GamePlay X/tsed-getting-started/node_modules/@tsed/common/src/platform/services/PlatformHandler.ts:161:15)\n at processTicksAndRejections (internal/process/task_queues.js:97:5)"
}
Ok, This is my point, the problem is on my side with the current implementation and the inconsistency between the Global middleware and the endpoint middleware. Let me fix that :)
:tada: This issue has been resolved in version 6.12.1 :tada:
The release is available on:
v6.12.1Your semantic-release bot :package::rocket:
@jappyjan Can you confirm that the fix released yesterday works for you ?
does not work for me :/
I attached a screenshot of the swagger ui including the failed request.
i am currently on version 6.13.0 for all tsed packages.

i just tried something out and i guess the problem was on my side/my implementation...
i changed the server config to accept only "application/json"
and on the ping route i acceptMime type "text/plain" as well as setting content type to "text/plain" instead of just "text"
and now everything works...
cool :)
Here is the integration test: https://github.com/TypedProject/tsed/blob/9d51626225721d3f3d197dc7be772deae1fa4168/packages/platform-test-utils/src/tests/testAcceptMime.ts
You can see, I implemented and covered your usecase :)