loopbackio / loopbackio/loopback-next
@property.array doesn't work if nulls are allowed
Personne n'a encore pris cette issue.
- Langage dominant
- TypeScript
- Étoiles
- 5.1k
- Forks
- 1.1k
- Merge moyen
- 2 j 21 h
- PR mergées (30 j)
- 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
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Piste de recherche
Lisez packages/repository/src/decorators/model.decorator.ts au niveau de la vérification signalée de la propriété de type tableau, puis comparez-la avec la gestion des tableaux dans loopback-connector-postgresql/lib/postgresql.js. Reproduisez le cas d’un tableau nullable avec le CodeSandbox associé et vérifiez que le decorator l’accepte et que PostgreSQL reçoit un JSON valide sans erreur de syntaxe.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- postgresql, typescript
- Domaine
- backend, databases
- Type d'issue
- Bug
- Difficulté
- 3/5
- Temps estimé
- 1-2 jours
- Activité
- À l'abandon
- Clarté
- Plutôt claire
- Accessibilité débutants
- 35/100