-
Notifications
You must be signed in to change notification settings - Fork 99
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
Conversation
There was a problem hiding this 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.
@pierissimo What is your take on this change? I'm not entirely convinced that this is the right direction. |
I'm not familiar with the postgres connector, and why is trying to access non-defined property definition. PS: Honestly I feel that throwing an exception in this case is correct, but I understand that it's not backward compatible. |
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.
Yes that would be better.
I agree with this. I have pulled the released version |
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; |
There was a problem hiding this comment.
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();
?
There was a problem hiding this comment.
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');
There was a problem hiding this comment.
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')); | |||
} |
There was a problem hiding this comment.
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.
@b-admike In that case, we could just add a return statement here (line 161), instead of throwing the error: |
b3eb823
to
a9d4a3f
Compare
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. |
a9d4a3f
to
340f397
Compare
Return early for non-nested properties as before, and undefined instead of erroring out when the property type is undefined.
340f397
to
95b907b
Compare
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
guide