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

[PoC] feat(rest): add type coercion for parseParams #1285

Closed
wants to merge 1 commit into from
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: 2 additions & 1 deletion packages/rest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"http-errors": "^1.6.3",
"js-yaml": "^3.11.0",
"lodash": "^4.17.5",
"path-to-regexp": "^2.2.0"
"path-to-regexp": "^2.2.0",
"tiny-coerce": "^1.0.1"
},
"devDependencies": {
"@loopback/build": "^0.6.0",
Expand Down
13 changes: 12 additions & 1 deletion packages/rest/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import {
PathParameterValues,
} from './internal-types';
import {ResolvedRoute} from './router/routing-table';
const coerce = require('tiny-coerce');
const debug = require('debug')('loopback:rest:parser');
type HttpError = HttpErrors.HttpError;

// tslint:disable-next-line:no-any
Expand Down Expand Up @@ -124,5 +126,14 @@ function buildOperationArguments(
}
}
if (requestBodyIndex > -1) paramArgs.splice(requestBodyIndex, 0, body);
return paramArgs;

debug('Coercing parameters', paramArgs);

const coercedParamArgs: OperationArgs = [];
for (const arg of paramArgs) {
debug('Coercing parameter', arg);
coercedParamArgs.push(typeof arg === 'object' ? arg : coerce(arg));
Copy link
Contributor

Choose a reason for hiding this comment

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

Strictly speaking, it's about deserializing parameters from an http request. We have to respect the parameter type spec as illustrated in #941.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That makes sense, I was working on using the deserializers from @repository and was really wondering why we couldn't use a simple coercer. I'll revert this to what it is in the original PoC

}

return coercedParamArgs;
}
83 changes: 83 additions & 0 deletions packages/rest/test/acceptance/coercion/coercion.acceptance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import {supertest, createClientForHandler, sinon} from '@loopback/testlab';
import {
RestApplication,
RestServer,
get,
param,
post,
requestBody,
} from '../../..';

describe('Coercion', () => {
let app: RestApplication;
let server: RestServer;
let client: supertest.SuperTest<supertest.Test>;

before(givenAnApplication);
before(givenAServer);
before(givenAClient);

after(async () => {
await app.stop();
});

class MyController {
@get('/create-number/{num}')
getNumber(@param.path.number('num') num: number) {
return num;
}

@get('/create-boolean')
getBoolean(@param.query.boolean('bool') bool: boolean) {
return bool;
}

@post('/create-object/')
createObject(@requestBody() obj: object) {}
}

it('coerces a number', async () => {
const spy = sinon.spy(MyController.prototype, 'getNumber');
await client.get('/create-number/13').expect(200);
sinon.assert.calledWithExactly(spy, 13);
sinon.assert.neverCalledWith(spy, '13');
spy.restore();
});

it('coerces "false" into a boolean', async () => {
const spy = sinon.spy(MyController.prototype, 'getBoolean');
await client.get('/create-boolean?bool=false').expect(200);
sinon.assert.calledWithExactly(spy, false);
sinon.assert.neverCalledWith(spy, 'false');
spy.restore();
});

it('coerces "true" into a boolean', async () => {
const spy = sinon.spy(MyController.prototype, 'getBoolean');
await client.get('/create-boolean?bool=true').expect(200);
sinon.assert.calledWithExactly(spy, true);
sinon.assert.neverCalledWith(spy, 'true');
spy.restore();
});

it('works with requestBody', async () => {
await client
.post('/create-object')
.send({foo: 'bar'})
.expect(200);
});

async function givenAnApplication() {
app = new RestApplication();
app.controller(MyController);
await app.start();
}

async function givenAServer() {
server = await app.getServer(RestServer);
}

async function givenAClient() {
client = await createClientForHandler(server.requestHandler);
}
});