loopbackio / loopbackio/loopback-datasource-juggler
[BUG] create helper function ignores the id property when it's equal to 0.
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 282
- Forks
- 366
- Avg merge
- 17h 4m
- Merged PRs (30d)
- 10
Description
**Description** :
Create call on a loopback model returns a property id as a response, in case where the property id starts from 0 it is removed from response. This is because the create function( dao.js line no 378) which gets the id as a callback on the create call in a loopback model, does not accepts 0 as an id.
Code snippet from dao.js
if id is 0 it is skipping the if block
```
function createCallback(err, id, rev) {
if (id) {
obj.__data[_idName] = id;
defineReadonlyProp(obj, _idName, id);
}
````
We have a scenario where the id starts from 0, and this is causing the id property for our 0th element to be removed from the response.
Steps to reproduce :
* Model which has a property id which starts from 0.
* Call the api to create which will have the id (starting from 0) in response.
* Response will be missing the id property for the first call where id is 0, and it'll add the id property in response starting from id as 1 onwards.
Current Response Behaviour :
id is removed when its value is equal to 0
```
{
"property1" : "property1",
"property2" : "property2"
}
```
Expected Results :
```
{
"id" : 0
"property1" : "property1",
"property2" : "property2"
}
```
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at the createCallback function in dao.js around line 378 and trace how the callback id is assigned to the created object. Reproduce a create call with an id of 0, then add coverage for the expected response and verify that ids of 0 and greater are preserved.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript
- Domain
- databases
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 55/100