loopbackio / loopbackio/loopback-next
Empty relation should return empty array
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 5.1k
- Forks
- 1.1k
- Avg merge
- 2d 21h
- Merged PRs (30d)
- 27
Description
Describe the bug
Hi,
I'm migrating a project from loopback 3 to loopback 4 and I found inconsistencies between the two regarding relations.
I am querying a model which have a HasMany relation on another model like this:
export class User extends Entity {
...
@hasMany(() => Order)
orders: Order[];
...
}
The query looks like this:
/users?filter={"include": ["orders"]}
In the answer, I got an orders property for users that have orders but no orders property for users that don't. Here is an API response for example:
[
...
{
"id": "__",
"email": "admin@example.com",
},
{
"id": "__",
"email": "support@example.com",
"orders": [
{...}
]
},
...
]
Is this an intended behavior in loopback 4 ? I find this much harder to deal with undefined property instead of empty array when querying model with relation.
Best regards
Logs
No response
Additional information
No response
Reproduction
https://codesandbox.io/s/falling-frost-qn03xh?file=/src/models/user.model.ts
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 with the CodeSandbox reproduction linked in the issue and inspect how an included HasMany relation is represented in the users response. Compare users with and without orders, then trace the include handling to determine the expected response shape. Done means the behavior is defined and covered for both populated and empty relations.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- api, backend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100