Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: array decorator #5559

Merged
merged 1 commit into from
May 28, 2020
Merged

fix: array decorator #5559

merged 1 commit into from
May 28, 2020

Conversation

jannyHou
Copy link
Contributor

@jannyHou jannyHou commented May 25, 2020

Fixes #4754

To define a nested array property with @property.array(), you must provide a json schema to describe the sub array item:

@model()
class TestModel {
  // alternatively use @property.array('array')
  @property.array(Array, {
    jsonSchema: {
      type: 'array',
      items: {type: 'string'},
    },
  })
  nestedArr: Array<Array<string>>;
}

A few issues this PR addresses:

Checklist

👉 Read and sign the CLA (Contributor License Agreement) 👈

  • npm test passes on your machine
  • New tests added or existing tests modified to cover all changes
  • Code conforms with the style guide
  • API Documentation in code was updated
  • Documentation in /docs/site was updated
  • Affected artifact templates in packages/cli were updated
  • Affected example projects in examples/* were updated

👉 Check out how to submit a PR 👈

@jannyHou jannyHou self-assigned this May 25, 2020
@@ -251,6 +251,34 @@ describe('build-schema', () => {
expectValidJsonSchema(jsonSchema);
});

it('properly converts nested array property when json schema provided', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have any tests on querying/filtering the nested arrays?

Copy link
Contributor Author

@jannyHou jannyHou May 26, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@agnes512 @loopback/repository-json-schema doesn't have it. If we add tests, need to do them in the acceptance tests for connectors.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mentioned it is because when I was triaging loopbackio/loopback-connector-postgresql#441, I realized that not all connectors support arrays/nested arrays. It'd be good if we can have them documented and tested. Just some thoughts 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I realized that not all connectors support arrays/nested arrays.

Yeah that's the problem, the original issue #4754 is not trying to support the full feature, it's a fix for the misuse of decorator.

It'd be good if we can have them documented and tested.

It's not a trivial thing to test across connectors, let's do it in a separate story.

Comment on lines +862 to +864
jsonSchema: {
type: 'array',
items: {type: 'string'},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have another question of the design. Do we need to specify these two fields? if so, why do we need type: 'array' as we already use the decorator?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@agnes512 the jsonSchema here is for the sub array property,
e.g. For Array<Array<string>>, the json schema describes Array<string>

If the current doc confuses you:

To define a nested array property, you must provide the jsonSchema field to describe the sub-array property.

Any suggestion on how to make it more clear?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see, then I am good with it 👍

Copy link
Contributor

@agnes512 agnes512 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hopefully we can have some tests for different connectors in the future :D

@jannyHou
Copy link
Contributor Author

@agnes512 sure 👍 created story in #5570

@jannyHou jannyHou merged commit 08ba68d into master May 28, 2020
@jannyHou jannyHou deleted the array-dec branch May 28, 2020 13:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

defining a nested array model property using @property.array decorator crashes the app
3 participants