loopbackio / loopbackio/loopback-next

@property.array ignores json schema definition

Open
#9,807 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
TypeScript
Stars
5.1k
Forks
1.1k
Avg merge
2d 21h
Merged PRs (30d)
27

Description

### Describe the bug

I was attempting to use the property array decorator alongside the jsonschema definition to ensure at least one item existed in the array at request time via validation within `@requestBody`
However when using the `@property.array` decorator it seems to just drop all the `jsonSchema` fields and ignore them

I've made a repro here.
https://codesandbox.io/p/sandbox/infallible-scott-64xtwg?welcome=true

Run this request

```
curl -X 'POST' \
'https://64xtwg-3000.csb.app/ping' \
-H 'accept: */*' \
-H 'Content-Type: application/json' \
-d '{
"subModel": [
{
"childProperty": [
]
}
]
}'
```

This line here https://github.com/loopbackio/loopback-next/blob/master/packages/repository-json-schema/src/build-schema.ts#L269C46-L269C46 seems to be the culprit. If i change that to `result = Object.assign({}, meta.jsonSchema, { type: 'array', items: propDef })` it appears to work as expected (throw a 422 error)

### Logs

_No response_

### Additional information

_No response_

### Reproduction

https://codesandbox.io/p/sandbox/infallible-scott-64xtwg?welcome=true

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at packages/repository-json-schema/src/build-schema.ts around line 269 and reproduce the behavior with the linked CodeSandbox or curl request. Check how @property.array combines its array metadata with the jsonSchema definition; done means the schema retains the definition and the empty childProperty request produces a 422 validation error.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
api
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.