ts-json-schema-generator
ts-json-schema-generator copied to clipboard
feat: support for sourceless nodes.
Hey! I've been using your project to help with json schema generation for my recent kitajs project. Its being great, but ive encountered some problems with my specific scenarios.
This PR is what I've been fixing along the time. A shortly brief:
- It currently does not have any specific unit tests, but its tested alongside with kitajs.
- This "sourceless" requirement is because sometimes I have to create type nodes with
typeChecker.typeToTypeNode, which creates nodes without a valid source code and-1locations. - Also, I needed to generate references without the
#/reference/prefix, so I added another option too. - Promise types were being interpreted as a plain object without any fields, now it resolves to its generic parameter.
Thanks for the pull request. Please add tests and also fix the todo.
@domoritz, for now, I don't have any spare time to to these tests, are you up to do it? They seem pretty small tests.
Furthermore, the TODO is more of a question than a task. I couldn't find any place where using the string "unresolved" could cause problems. And as you are more familiar with this lib, could this cause an issue?
I'm starting to teach on Tuesday so I unfortunately don't have cycles to write tests for you.
Thanks for adding the tests. Ping me when they are passing and I will review the pull request.
@domoritz, I think that was it! Got my new notebook yesterday and now i can code while I'm at philosophy classes 😁.
The only thing remaining is this TODO. It is more a kind of a note (as I said before), and as all tests passed, seems just fine. But who knows all future cases.
Furthermore, the TODO is more of a question than a task. I couldn't find any place where using the string "unresolved" could cause problems. And as you are more familiar with this lib, could this cause an issue?
https://github.com/vega/ts-json-schema-generator/blob/9e022faa61104731d24e0397a6159a73d49eedc1/src/Utils/nodeKey.ts#L38-L40
I'd would let it here just to our future self keep an eye on it.
@domoritz, Can this be released soon?
What are sourceless types? If this is something common, I'd be happy to include the change as a separate pull request.
I'm calling sourceless nodes any ts.Node object that is created programatically. Like when using ts.typeToTypeNode or manually invoking ts.factory methods. For now, ts-json-schema-generator only works with ts.Nodes that are read from a real file in the machine, as it relies on node.getSourceFile() calls. The sourceless changes are to support any type of ts.Node, independently of where it was created.
About the #/reference/ opt-out option, involves the "contradictory" usage of it. You can read more about it at https://github.com/json-schema-org/json-schema-spec/issues/279#issuecomment-288558322.
And because of the above thread (any many others sub discussions spread out there), a lot of json schema related libraries are have different behaviours for #/reference/s, this includes fastify, avj and many others...
This option is just another related feature that is disabled by default, and it helps in these cases without having to maintain a fork of this project with their required changes, as it currently this project does not allow all nescessary customizations to make it work.
I agree with you that this PR has two unrelated to each other changes. I could split them into two small PRs, but that would lead to me just copying code over branches...
I'm not convinced that I want to support optional references at this point. Can you remove it from this pull request? The sourceless node support makes sense to me, though.
This pull request introduces 1 alert when merging 30108341b900a1a3e30bde8b9180ad65aa4d397b into f7e9cb58a12a9b153e82f7e47dd1c5061dc3aa95 - view on LGTM.com
new alerts:
- 1 for Implicit operand conversion
@domoritz, sorry for pinging, but this PR would help me so much :)
Thanks for the ping. It's in my queue to review but I have a lot on my plate right now. I'd recommend publishing your fork in your personal namespace to get unblocked.
:rocket: PR was released in v1.2.0-next.1 :rocket: