-
Notifications
You must be signed in to change notification settings - Fork 25
fix: correct return type of getSchemaType #85
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
base: main
Are you sure you want to change the base?
Conversation
src/utils/getSchemaType.ts
Outdated
| // nothing found yet check dynamic properties for a type | ||
| if (node.if) { | ||
| return getSchemaType(node.if, data); | ||
| return getSchemaType(node.if.getNode("#").node ?? node.if, data); |
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 is out of scope of this merge request. I am wondering, why are you adding ref-resolution here?
If we were to add this:
- getNode would need data as second argument to fully use its api
- I would prefer to use node.if.resolveRef here to be explicit
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.
the short story here is that first I realised that types of getSchemaType are off, so I fixed them and created this PR, later while working on my product I've seen that the function is also not working correctly on oneOf nodes with refs, so I thought this PR is close enough to include this as well... If you prefer I can split this into two PRs (or we can keep it as single one if that's ok)
as for node.if.resolveRef - I think it should work... I pushed the changes about that, are those ok?
and if you won't mind - what's the real benefit of using resolveRef here? I think that getNode looks to be way more generic way of "get me whatever there is underneath" right? under the hood it's also looks to handle more edge cases than resolveRef 🤔
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.
there is one more issue here:
- with
resolveRefI have errors in current tests
Exception during run: TypeError: Cannot read properties of undefined (reading 'length')
at resolveRecursiveRef (/projects/json-schema-library/src/draft2019-09/keywords/$ref.ts:108:26)
at Object.resolveRef (/projects/json-schema-library/src/draft2019-09/keywords/$ref.ts:75:26)
at getSchemaType (/projects/json-schema-library/src/utils/getSchemaType.ts:132:66)
at Object.getData (/projects/json-schema-library/src/draft2019-09/methods/getData.ts:174:31)
at Object.getData (/projects/json-schema-library/src/SchemaNode.ts:305:37)
at /projects/json-schema-library/src/draft2019-09/methods/getData.ts:200:56
at Array.forEach (<anonymous>)
at Object.object (/projects/json-schema-library/src/draft2019-09/methods/getData.ts:193:42)
at Object.getData (/projects/json-schema-library/src/draft2019-09/methods/getData.ts:175:38)
at Object.getData (/projects/json-schema-library/src/SchemaNode.ts:305:37)
at Object.getData (/projects/json-schema-library/src/draft2019-09/methods/getData.ts:161:36)
at Object.getData (/projects/json-schema-library/src/SchemaNode.ts:305:37)
at /projects/json-schema-library/src/draft2019-09/methods/getData.ts:126:39
at Array.forEach (<anonymous>)
at Object.getData (/projects/json-schema-library/src/draft2019-09/methods/getData.ts:125:27)
at Object.getData (/projects/json-schema-library/src/SchemaNode.ts:305:37)
at compileSchema (/projects/json-schema-library/src/compileSchema.ts:74:58)
at /projects/json-schema-library/src/tests/utils/runTestCases.ts:26:39
at Array.forEach (<anonymous>)
at Suite.<anonymous> (/projects/json-schema-library/src/tests/utils/runTestCases.ts:22:22)
at Object.create (/projects/json-schema-library/node_modules/mocha/lib/interfaces/common.js:148:19)
at context.describe.context.context (/projects/json-schema-library/node_modules/mocha/lib/interfaces/bdd.js:42:27)
at runTestCase (/projects/json-schema-library/src/tests/utils/runTestCases.ts:21:5)
at /projects/json-schema-library/src/tests/utils/runTestCases.ts:103:30
at Array.forEach (<anonymous>)
at Suite.<anonymous> (/projects/json-schema-library/src/tests/utils/runTestCases.ts:103:14)
at Object.create (/projects/json-schema-library/node_modules/mocha/lib/interfaces/common.js:148:19)
at context.describe.context.context (/projects/json-schema-library/node_modules/mocha/lib/interfaces/bdd.js:42:27)
at runAllTestCases (/projects/json-schema-library/src/tests/utils/runTestCases.ts:89:5)
at Object.<anonymous> (/projects/json-schema-library/src/tests/spec/draft2019-09.spec.ts:10:16)
- with
getNodeeverything works correctly
I don't have a root cause of the error yet
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 am unable to reproduce issues based your tests:
On your branch, using
const type = getSchemaType(node.allOf[i].resolveRef(), data);
if (type) {
return type;
}works just fine on my end.
getNode does a couple of things in addition to resolving refs, like
- parsing the json-pointer to find the node we are looking for and
- creating additional an extended response type for consumption.
We can save unnecessary work and simply use resolveRef. Especially since this is a core function we should be explicit on the actions we use and work on the simpler api.
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.
hmmm... maybe I'm running tests somehow wrongly... I'm just executing yarn test
but I don't know how it could work tbh...
I see quite clear bug in src/draft2019-09/keywords/$ref.ts in resolveRef function (line 72). It has optional path param, that I'm not providing to it (as it's optional and I don't need it). Then on line 75 it's calling resolveRecursiveRef with that optional path as second parameter, which is not optional in resolveRecursiveRef declaration (src/draft2019-09/keywords/$ref.ts:102-103), which obviously results in error on line 108 where it tries to do history.length with history being undefined.
I fixed that bug by providing empty path in src/draft2019-09/keywords/$ref.ts and then I used resolveRef in getSchemaType. Now it should all be good
|
Hey there, thank you for your merge request. Highly appreciated! |
|
hi @sagold - any chance to merge this? |
|
Hey mlewando. Yes, we need some tests to check if it behaves as expected and keep it working through future changes. I can help you with that next week, but maybe you can post some of your use cases? Cheers, |
|
Hey Mateusz, thank you so much for your work and your contribution to this library. I think this feature will save some headaches for other users. I suggest we merge this request! I will make some changes after merging and before publishing them
|
|
@sagold - thanks a lot :)
those I just did, so you should be free to just merge it 🤞 |
I noted that the return type of
getSchemaTypeis not correct. It looks to be derived frombut the actual declaration was
The
keyof typeof SCHEMA_TYPESis actually"length" | "push" | "pop" | "map" ...which is wrong.I made the
SCHEMA_TYPESa const array and created new simple type:to fix that issue.