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: return undefined for first level undefined props #138

Merged
merged 1 commit into from
Feb 25, 2019

Conversation

b-admike
Copy link
Contributor

Description

Keep backward-compatilibity from #136 so that for first level properties, we return what we did before.

Fixes loopbackio/loopback-connector-postgresql#364

Checklist

  • New tests added or existing tests modified to cover all changes
  • Code conforms with the style
    guide

@b-admike b-admike self-assigned this Feb 19, 2019
Copy link
Contributor

@jannyHou jannyHou left a comment

Choose a reason for hiding this comment

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

@b-admike Nice. Your change LGTM and test is good.
PS: Please fix the commit message error before merging.
Verified the cloudant/couchdb2/mongodb failures are not related.

@b-admike
Copy link
Contributor Author

@pierissimo What is your take on this change? I'm not entirely convinced that this is the right direction.

@pierissimo
Copy link
Contributor

I'm not familiar with the postgres connector, and why is trying to access non-defined property definition.
The solution proposed looks good, I wonder if we should return undefined even for nested properties, for consistency.

PS: Honestly I feel that throwing an exception in this case is correct, but I understand that it's not backward compatible.

@b-admike
Copy link
Contributor Author

I'm not familiar with the postgres connector, and why is trying to access non-defined property definition.

For trying to access undefined property definitions, there is a bug in the connector which I think I have a fix at loopbackio/loopback-connector-postgresql#365. But, we also returned the property definition as is before (even if it was undefined), so in some cases it might be working as expected.

The solution proposed looks good, I wonder if we should return undefined even for nested properties, for consistency.

Yes that would be better.

PS: Honestly I feel that throwing an exception in this case is correct, but I understand that it's not backward compatible.

I agree with this. I have pulled the released version 4.6.0 back so we can assess how to proceed with #136 for release. I think we should release it as a semver major since it has the breaking changes (I feel we can improve on the error message depending on the condition, but that can be done in a follow-up PR). If we decide to go ahead with this patch, then we can release a new version which is a semver minor and still backward-compatible. @strongloop/ch-lb-db-connectors @strongloop/sq-lb-apex Thoughts?

it('should preserve backward-compatibility for non-existing property', () => {
const definition = connector.getPropertyDefinition('MyModel', 'idontexist');
// eslint-disable-next-line no-unused-expressions
expect(definition).to.be.undefined;
Copy link
Contributor

Choose a reason for hiding this comment

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

Does it allow expect(definition).to.be.undefined();?

Copy link
Contributor Author

@b-admike b-admike Feb 20, 2019

Choose a reason for hiding this comment

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

I tried, but it doesn't according to the docs. Another option is to do expect(definition).to.be.an('undefined');

Copy link
Member

Choose a reason for hiding this comment

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

@b-admike Have you considered the following?

expect(definition).to.equal(undefined);

Beware of libraries that assert on property access!

In the past, I have had good experience with using dirty-chai plugin for chai, see how we are using it in LoopBack 3.x here: loopback's test/helpers/expect.js

@@ -155,12 +155,12 @@ Connector.getNestedPropertyDefinition = function(definition, propPath) {
const isArray = !isPropUndefined && Array.isArray(prop.type);
const isFunction = !isPropUndefined && !isArray && typeof prop.type === 'function';

if (propPath.length === 1) return prop;

if (isPropUndefined || (propPath.length > 1 && (isArray && prop.type.length === 0))) {
throw new Error(g.f('Invalid property path'));
}
Copy link
Contributor

Choose a reason for hiding this comment

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

This method is to retrieve metadata. IMO, we should return undefined if no property definition exists for such path and have caller of this function to throw if necessary.

@pierissimo
Copy link
Contributor

I think we should release it as a semver major since it has the breaking changes (I feel we can improve on the error message depending on the condition, but that can be done in a follow-up PR). If we decide to go ahead with this patch, then we can release a new version which is a semver minor and still backward-compatible. @strongloop/ch-lb-db-connectors @strongloop/sq-lb-apex Thoughts?

@b-admike In that case, we could just add a return statement here (line 161), instead of throwing the error:
https://github.com/strongloop/loopback-connector/pull/138/files#diff-d756fa1c3234311d61ddb0dc4e6630b0R161

@b-admike b-admike force-pushed the fix/reserve-bc-propdef branch from b3eb823 to a9d4a3f Compare February 20, 2019 17:09
@b-admike
Copy link
Contributor Author

Okay, I think we are all good with landing this patch and releasing a minor/patch to npm. Thank you all for the feedback/reviews.

Return early for non-nested properties as before,
and undefined instead of erroring out when the
property type is undefined.
@b-admike b-admike force-pushed the fix/reserve-bc-propdef branch from 340f397 to 95b907b Compare February 23, 2019 03:46
@b-admike b-admike deleted the fix/reserve-bc-propdef branch February 25, 2019 19:57
@b-admike b-admike merged commit 386e041 into master Feb 25, 2019
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.

5 participants