Skip to content

@property.array doesn't work if nulls are allowed #8466

Open

Description

@mgabeler-lee-6rs

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:

@property.array('string', {
	required: false,
})
foo!: string[] | null;

Produces a @property.array can only decorate array properties from here:

throw new Error(ERR_PROP_NOT_ARRAY);

Trying to "hack" it like this:

@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

Activity

  1. added
    Repository
    and removed on Apr 8, 2022
  2. inceserhatabdullah commented on Jan 28, 2025

    @inceserhatabdullah

    @mgabeler-lee-6rs hi. I've been struggling with this issue for a while, and I finally found a solution that works for me.
    I'm using mongoDB so I don't know this solution does work for you.
    Here is the solution.

    @property({
        jsonSchema: {
          type: 'array',
          items: { type: 'string' },
          nullable: true
        },
        required: false
      })
      field1?: string[] | null;

    I can save field1 as null , [] and ["..."]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Repositorybug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions