Skip to content

Commit

Permalink
Improve Boolean conversion (#2)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
sebbi08 and jkoenig134 authored Jan 16, 2025
1 parent e8132e2 commit 00db68b
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 25 deletions.
49 changes: 27 additions & 22 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nmshd/typescript-rest",
"version": "3.0.5",
"version": "3.1.0",
"description": "A Library to create RESTFul APIs with Typescript",
"keywords": [
"API",
Expand Down Expand Up @@ -34,7 +34,7 @@
"lint:prettier": "prettier --check .",
"lint:tsc": "tsc --noEmit",
"start": "tsc -w",
"pretest": "cross-env NODE_ENV=test npm run build && npm run lint",
"pretest": "cross-env NODE_ENV=test npm run build",
"test": "cross-env NODE_ENV=test jest --config ./test/jest.config.js --coverage --runInBand",
"test:integration": "cross-env NODE_ENV=test jest --config ./test/jest.config-integration.js --runInBand",
"test:unit": "cross-env NODE_ENV=test jest --config ./test/jest.config-unit.js",
Expand Down
5 changes: 4 additions & 1 deletion src/server/parameter-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,10 @@ export class ParameterProcessor {
case 'Number':
return paramValue === undefined ? paramValue : parseFloat(paramValue as string);
case 'Boolean':
return paramValue === undefined ? paramValue : paramValue === 'true' || paramValue === true;
if (paramValue === undefined) return paramValue;
if (typeof paramValue === 'boolean') return paramValue;

return paramValue.toLowerCase() === 'true';
default:
let converter = ServerContainer.get().paramConverters.get(paramType);
if (!converter) {
Expand Down
55 changes: 55 additions & 0 deletions test/integration/datatypes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,23 @@ export class TestParamsService {
): string {
return `limit:${limit}|prefix:${prefix}|expand:${expand}`;
}
@GET
@Path('boolean-casing')
public testBooleanCasing(
@QueryParam('True') True?: boolean,
@QueryParam('TRUE') TRUE?: boolean,
@QueryParam('False') False?: boolean,
@QueryParam('FALSE') FALSE?: boolean
): string {
return `True:${True}|TRUE:${TRUE}|False:${False}|FALSE:${FALSE}`;
}

@POST
@Path('boolean-as-body-param')
@BodyOptions({ strict: false })
public testBooleanAsBodyParam(expand: boolean): string {
return `expand:${expand}`;
}

@POST
@Path('upload')
Expand Down Expand Up @@ -478,6 +495,44 @@ describe('Data Types Tests', () => {
}
);
});

it('should handle boolean parameters with different casings', (done) => {
request(
{
url: 'http://localhost:5674/testparams/boolean-casing?True=True&TRUE=TRUE&False=False&FALSE=FALSE'
},
(error, response, body) => {
expect(body).toEqual('True:true|TRUE:true|False:false|FALSE:false');
done();
}
);
});

it('should handle boolean parameters as undefined', (done) => {
request(
{
url: 'http://localhost:5674/testparams/boolean-casing?True='
},
(error, response, body) => {
expect(body).toEqual('True:false|TRUE:undefined|False:undefined|FALSE:undefined');
done();
}
);
});

it('should handle boolean parameters as param in body', (done) => {
request.post(
{
url: 'http://localhost:5674/testparams/boolean-as-body-param',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(true)
},
(error, response, body) => {
expect(body).toEqual('expand:true');
done();
}
);
});
});
describe('Download Service', () => {
it('should return a file', (done) => {
Expand Down

0 comments on commit 00db68b

Please sign in to comment.