loopbackio / loopbackio/loopback-next
@property.array doesn't work if nulls are allowed
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
Trying to make a model that maps to a nullable JSON or array column in PostgreSQL, so the entity property needs to permit nulls.
A property declared like this:
```typescript
@property.array('string', {
required: false,
})
foo!: string[] | null;
```
Produces a `@property.array can only decorate array properties` from here: https://github.com/loopbackio/loopback-next/blob/a60ae8e43a1ec9d65929f19bbd364e332f28503b/packages/repository/src/decorators/model.decorator.ts#L155
Trying to "hack" it like this:
```typescript
@property({
required: false,
type: Array,
itemType: 'string',
jsonSchema: {
type: 'array',
nullable: true,
items: { type: 'string' },
},
})
```
Results in incorrect data being sent to PostgreSQL (get JSON syntax errors because it's sending the object as an array to the PG driver instead of the `JSON.stringify` it should be using, because the array check in `loopback-connector-postgresql` (https://github.com/loopbackio/loopback-connector-postgresql/blob/61b5029275487fc7812d774ab4be16dc8aacd726/lib/postgresql.js#L788) fails
### Logs
_No response_
### Additional information
_No response_
### Reproduction
https://codesandbox.io/s/zen-thompson-keo2is
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
Read packages/repository/src/decorators/model.decorator.ts at the reported array-property check, then compare it with the array handling in loopback-connector-postgresql/lib/postgresql.js. Reproduce the nullable array case with the linked CodeSandbox and verify that the decorator accepts it and PostgreSQL receives valid JSON without syntax errors.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- postgresql, typescript
- Domain
- backend, databases
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100