loopbackio / loopbackio/loopback-next

Empty relation should return empty array

Open
#8,910 0 comments 0 reactions 0 assignees View on GitHub
bug
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

Open the contributing guide

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.