ts-json-schema-generator icon indicating copy to clipboard operation
ts-json-schema-generator copied to clipboard

feat: support for sourceless nodes.

Open arthurfiorette opened this issue 3 years ago • 13 comments

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 -1 locations.
  • 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.

arthurfiorette avatar Aug 28 '22 17:08 arthurfiorette

Thanks for the pull request. Please add tests and also fix the todo.

domoritz avatar Aug 28 '22 18:08 domoritz

@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?

arthurfiorette avatar Aug 28 '22 21:08 arthurfiorette

I'm starting to teach on Tuesday so I unfortunately don't have cycles to write tests for you.

domoritz avatar Aug 28 '22 21:08 domoritz

Thanks for adding the tests. Ping me when they are passing and I will review the pull request.

domoritz avatar Aug 29 '22 17:08 domoritz

@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.

arthurfiorette avatar Aug 30 '22 12:08 arthurfiorette

@domoritz, Can this be released soon?

arthurfiorette avatar Sep 01 '22 12:09 arthurfiorette

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.

arthurfiorette avatar Sep 05 '22 11:09 arthurfiorette

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.

arthurfiorette avatar Sep 05 '22 12:09 arthurfiorette

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...

arthurfiorette avatar Sep 05 '22 12:09 arthurfiorette

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.

domoritz avatar Sep 06 '22 14:09 domoritz

This pull request introduces 1 alert when merging 30108341b900a1a3e30bde8b9180ad65aa4d397b into f7e9cb58a12a9b153e82f7e47dd1c5061dc3aa95 - view on LGTM.com

new alerts:

  • 1 for Implicit operand conversion

lgtm-com[bot] avatar Sep 09 '22 10:09 lgtm-com[bot]

@domoritz, sorry for pinging, but this PR would help me so much :)

arthurfiorette avatar Sep 29 '22 23:09 arthurfiorette

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.

domoritz avatar Sep 30 '22 12:09 domoritz

:rocket: PR was released in v1.2.0-next.1 :rocket:

github-actions[bot] avatar Oct 17 '22 13:10 github-actions[bot]