Skip to content

Commit 93073c6

Browse files
committed
fix: update OkNegotiatedContentResult
update OkNegotiatedContentResult to rely on JsonContent when non string content is provided
1 parent 4ba64b2 commit 93073c6

File tree

7 files changed

+74
-28
lines changed

7 files changed

+74
-28
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1212
### Changed
1313
- Updated `BaseMiddleware.handler` to allow async handlers.
1414
- Updated `Middleware` to allow include any `ServiceIdentifier`.
15+
- Updated `JsonContent` with no generic.
1516

1617
### Fixed
18+
- Updated `BaseController.ok` to no longer return `text/plain` responses when non string content is passed.
1719

1820
## [6.4.10]
1921

package-lock.json

Lines changed: 5 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
"@types/supertest": "6.0.2",
5353
"@typescript-eslint/eslint-plugin": "8.20.0",
5454
"@typescript-eslint/parser": "8.20.0",
55+
"body-parser": "1.20.3",
5556
"cookie-parser": "1.4.7",
5657
"eslint": "9.18.0",
5758
"eslint-config-prettier": "10.0.1",

src/results/OkNegotiatedContentResult.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { StatusCodes } from 'http-status-codes';
22

3+
import { JsonContent } from '../content/jsonContent';
34
import { StringContent } from '../content/stringContent';
45
import { HttpResponseMessage } from '../httpResponseMessage';
56
import type { IHttpActionResult } from '../interfaces';
@@ -11,7 +12,12 @@ export class OkNegotiatedContentResult<T> implements IHttpActionResult {
1112
const response: HttpResponseMessage = new HttpResponseMessage(
1213
StatusCodes.OK,
1314
);
14-
response.content = new StringContent(JSON.stringify(this.content));
15+
16+
if (typeof this.content === 'string') {
17+
response.content = new StringContent(this.content);
18+
} else {
19+
response.content = new JsonContent(this.content);
20+
}
1521

1622
return response;
1723
}

src/test/action_result.test.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@ describe('ActionResults', () => {
4141
await actionResult.executeAsync();
4242

4343
expect(responseMessage.statusCode).toBe(StatusCodes.OK);
44-
expect(await responseMessage.content.readAsync()).toBe(
45-
JSON.stringify(content),
46-
);
44+
expect(await responseMessage.content.readAsync()).toStrictEqual(content);
4745
});
4846
});
4947

src/test/content/jsonContent.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { JsonContent } from '../../content/jsonContent';
44

55
describe('JsonContent', () => {
66
it('should have application/json as the default media type', () => {
7-
const content: JsonContent<Record<string, unknown>> = new JsonContent({});
7+
const content: JsonContent = new JsonContent({});
88
expect(content.headers['content-type']).toBe('application/json');
99
});
1010

src/test/issues/issue_420.test.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { beforeEach, describe, expect, it } from '@jest/globals';
2+
import * as bodyParser from 'body-parser';
3+
import { Application, Request } from 'express';
4+
import { Container } from 'inversify';
5+
import supertest from 'supertest';
6+
import TestAgent from 'supertest/lib/agent';
7+
8+
import { BaseHttpController } from '../../base_http_controller';
9+
import { controller, httpPut, request } from '../../decorators';
10+
import { InversifyExpressServer } from '../../server';
11+
import { cleanUpMetadata } from '../../utils';
12+
13+
describe('Issue 420', () => {
14+
beforeEach(() => {
15+
cleanUpMetadata();
16+
});
17+
18+
it('should work with no url params', async () => {
19+
@controller('/controller')
20+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
21+
class TestController extends BaseHttpController {
22+
@httpPut('/test')
23+
public async updateTest(
24+
@request()
25+
req: Request<unknown, unknown, { test: string }, void>,
26+
) {
27+
return this.ok({ message: req.body.test });
28+
}
29+
}
30+
31+
const container: Container = new Container();
32+
33+
const server: InversifyExpressServer = new InversifyExpressServer(
34+
container,
35+
);
36+
37+
server.setConfig((app: Application) => {
38+
app.use(
39+
bodyParser.urlencoded({
40+
extended: true,
41+
}),
42+
);
43+
app.use(bodyParser.json());
44+
});
45+
46+
const agent: TestAgent<supertest.Test> = supertest(server.build());
47+
48+
const response: supertest.Response = await agent
49+
.put('/controller/test')
50+
.send({ test: 'test' })
51+
.set('Content-Type', 'application/json')
52+
.set('Accept', 'application/json');
53+
54+
expect(response.status).toBe(200);
55+
expect(response.body).toStrictEqual({ message: 'test' });
56+
});
57+
});

0 commit comments

Comments
 (0)