Skip to content

Commit 00db68b

Browse files
sebbi08jkoenig134
andauthored
Improve Boolean conversion (#2)
* feat: improve boolean conversion * chore: bumb version * core: prettier and eslint * chore: audit fix * fix: codestyle * chore: fix tests * chore: add tests --------- Co-authored-by: Julian König <[email protected]>
1 parent e8132e2 commit 00db68b

File tree

4 files changed

+88
-25
lines changed

4 files changed

+88
-25
lines changed

package-lock.json

+27-22
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@nmshd/typescript-rest",
3-
"version": "3.0.5",
3+
"version": "3.1.0",
44
"description": "A Library to create RESTFul APIs with Typescript",
55
"keywords": [
66
"API",
@@ -34,7 +34,7 @@
3434
"lint:prettier": "prettier --check .",
3535
"lint:tsc": "tsc --noEmit",
3636
"start": "tsc -w",
37-
"pretest": "cross-env NODE_ENV=test npm run build && npm run lint",
37+
"pretest": "cross-env NODE_ENV=test npm run build",
3838
"test": "cross-env NODE_ENV=test jest --config ./test/jest.config.js --coverage --runInBand",
3939
"test:integration": "cross-env NODE_ENV=test jest --config ./test/jest.config-integration.js --runInBand",
4040
"test:unit": "cross-env NODE_ENV=test jest --config ./test/jest.config-unit.js",

src/server/parameter-processor.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,10 @@ export class ParameterProcessor {
9292
case 'Number':
9393
return paramValue === undefined ? paramValue : parseFloat(paramValue as string);
9494
case 'Boolean':
95-
return paramValue === undefined ? paramValue : paramValue === 'true' || paramValue === true;
95+
if (paramValue === undefined) return paramValue;
96+
if (typeof paramValue === 'boolean') return paramValue;
97+
98+
return paramValue.toLowerCase() === 'true';
9699
default:
97100
let converter = ServerContainer.get().paramConverters.get(paramType);
98101
if (!converter) {

test/integration/datatypes.spec.ts

+55
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,23 @@ export class TestParamsService {
157157
): string {
158158
return `limit:${limit}|prefix:${prefix}|expand:${expand}`;
159159
}
160+
@GET
161+
@Path('boolean-casing')
162+
public testBooleanCasing(
163+
@QueryParam('True') True?: boolean,
164+
@QueryParam('TRUE') TRUE?: boolean,
165+
@QueryParam('False') False?: boolean,
166+
@QueryParam('FALSE') FALSE?: boolean
167+
): string {
168+
return `True:${True}|TRUE:${TRUE}|False:${False}|FALSE:${FALSE}`;
169+
}
170+
171+
@POST
172+
@Path('boolean-as-body-param')
173+
@BodyOptions({ strict: false })
174+
public testBooleanAsBodyParam(expand: boolean): string {
175+
return `expand:${expand}`;
176+
}
160177

161178
@POST
162179
@Path('upload')
@@ -478,6 +495,44 @@ describe('Data Types Tests', () => {
478495
}
479496
);
480497
});
498+
499+
it('should handle boolean parameters with different casings', (done) => {
500+
request(
501+
{
502+
url: 'http://localhost:5674/testparams/boolean-casing?True=True&TRUE=TRUE&False=False&FALSE=FALSE'
503+
},
504+
(error, response, body) => {
505+
expect(body).toEqual('True:true|TRUE:true|False:false|FALSE:false');
506+
done();
507+
}
508+
);
509+
});
510+
511+
it('should handle boolean parameters as undefined', (done) => {
512+
request(
513+
{
514+
url: 'http://localhost:5674/testparams/boolean-casing?True='
515+
},
516+
(error, response, body) => {
517+
expect(body).toEqual('True:false|TRUE:undefined|False:undefined|FALSE:undefined');
518+
done();
519+
}
520+
);
521+
});
522+
523+
it('should handle boolean parameters as param in body', (done) => {
524+
request.post(
525+
{
526+
url: 'http://localhost:5674/testparams/boolean-as-body-param',
527+
headers: { 'Content-Type': 'application/json' },
528+
body: JSON.stringify(true)
529+
},
530+
(error, response, body) => {
531+
expect(body).toEqual('expand:true');
532+
done();
533+
}
534+
);
535+
});
481536
});
482537
describe('Download Service', () => {
483538
it('should return a file', (done) => {

0 commit comments

Comments
 (0)