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

Don't attempt to json encode string types #1479

Closed
Closed
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
3 changes: 3 additions & 0 deletions packages/cli/src/routeGeneration/templates/express.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,9 @@ export function RegisterRoutes(app: Router) {
if (data && typeof data.pipe === 'function' && data.readable && typeof data._read === 'function') {
response.status(statusCode || 200)
data.pipe(response);
} else if (data && typeof data === 'string') {
Copy link
Collaborator

@WoH WoH Oct 4, 2023

Choose a reason for hiding this comment

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

data could be '' which is nullish, do we want this to fall through to 204 no content?

response.type('txt');
response.status(statusCode || 200).send(data);
} else if (data !== null && data !== undefined) {
response.status(statusCode || 200).json(data);
} else {
Expand Down
5 changes: 4 additions & 1 deletion packages/cli/src/routeGeneration/templates/hapi.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,6 @@ export function RegisterRoutes(server: any) {
if (h.__isTsoaResponded) {
return h.__isTsoaResponded;
}

let response = data !== null && data !== undefined
? h.response(data).code(200)
: h.response("").code(204);
Expand All @@ -307,6 +306,10 @@ export function RegisterRoutes(server: any) {
response.header(name, headers[name]);
});

if (data && typeof data == 'string') {
response.header('content-type', 'text/plain');
}

if (statusCode) {
response.code(statusCode);
}
Expand Down
5 changes: 5 additions & 0 deletions tests/fixtures/controllers/getController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,11 @@ export class GetTestController extends Controller {
return new ModelService().getModel();
}

@Get('StringValue')
public async getStringValue(): Promise<string> {
return 'FOO';
}

@Get('IndexedValue')
public async getIndexedValue(): Promise<IndexedValue> {
return 'FOO';
Expand Down
9 changes: 9 additions & 0 deletions tests/integration/express-server.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,15 @@ describe('Express Server', () => {
});
});

it('returns string responses', () => {
return verifyGetRequest(`${basePath}/GetTest/StringValue`, (_err, res) => {
expect(res.status).to.equal(200);
expect(res.headers['content-type']).to.equal('text/plain; charset=utf-8');
expect(res.text).to.equal('FOO');
return;
});
});

it('should reject invalid additionalProperties', () => {
const invalidValues = ['invalid', null, [], 1, { foo: null }, { foo: 1 }, { foo: [] }, { foo: {} }, { foo: { foo: 'bar' } }];

Expand Down
8 changes: 8 additions & 0 deletions tests/integration/hapi-server.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ describe('Hapi Server', () => {
});
});

it('returns string responses', () => {
return verifyGetRequest(`${basePath}/GetTest/StringValue`, (_err, res) => {
expect(res.get('content-type')).to.eq('text/plain; charset=utf-8');
expect(res.text).to.equal('FOO');
return;
});
});

it('can handle get request with collection return value', () => {
return verifyGetRequest(basePath + '/GetTest/Multi', (_err, res) => {
const models = res.body as TestModel[];
Expand Down
8 changes: 8 additions & 0 deletions tests/integration/koa-server.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ describe('Koa Server', () => {
});
});

it('returns string responses', () => {
return verifyGetRequest(`${basePath}/GetTest/StringValue`, (_err, res) => {
expect(res.header['content-type']).to.equal('text/plain; charset=utf-8');
expect(res.text).to.equal('FOO');
return;
});
});

it('can handle get request with path and query parameters', () => {
return verifyGetRequest(basePath + `/GetTest/${1}/true/test?booleanParam=true&stringParam=test1234&numberParam=1234`, (_err, res) => {
const model = res.body as TestModel;
Expand Down
Loading