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

fix: merge compatible definitions in union types

Open dmchurch opened this issue 4 years ago • 11 comments

Most validation keywords apply to only one of the basic types, so a "string" type and an "array" type can share a definition without colliding as a ["string", "array"] type, as long as they don't have any incompatibilities with each other. Modify UnionTypeFormatter to collapse these disjoint types into a single definition, without using anyOf.

dmchurch avatar Mar 25 '21 19:03 dmchurch

I've classed this as a "fix" because it doesn't change the semantic meaning of the output, and it doesn't enable new features for schema developers. Feel free to upgrade it to a "feat", though.

dmchurch avatar Mar 25 '21 19:03 dmchurch

I think you probably just need to update the Vega-Lite test case. You can use the provided script to do it automatically.

domoritz avatar Mar 25 '21 20:03 domoritz

Right! I'll get on the tests now. Some of them weren't passing even in my original checkout, but at the very least I can make sure that anything affected by this change gets updated.

dmchurch avatar Mar 25 '21 21:03 dmchurch

Try yarn test:update

domoritz avatar Mar 25 '21 21:03 domoritz

I've actually got a couple test cases I want to add to exercise some of the weirder edge cases involving definition collapse, but I'm setting this down for the night. In any event, the existing test cases already exercise it fairly well.

dmchurch avatar Mar 26 '21 04:03 dmchurch

Whew! That got much bigger than expected. I added some schema validation tests, which uncovered some issues with the existing code, which required a more elaborate test harness, which means the test harness needs more tests... you get the idea. One of these days I'll get all those yaks shaved.

dmchurch avatar Mar 28 '21 05:03 dmchurch

...and I literally just now realized that the makeExemplar code ought to be living on-or-near the BaseType classes themselves, which would clean up the huge mess of if-else-if-else in makeExemplar.ts. Oh well, there are more revisions I need to make anyway...

Feel free to offer critique/direction if this isn't going in a useful direction, btw, I'm not averse to scrapping things and rebuilding.

dmchurch avatar Mar 28 '21 05:03 dmchurch

...and mergeDefinitions would get so very much smaller if I just called it twice, once in each direction. Yeah, don't merge this yet, I've got work to do.

dmchurch avatar Mar 28 '21 05:03 dmchurch

Thank you for working on this and making sure the feature is clean. I definitely think this is a good improvement.

domoritz avatar Mar 28 '21 15:03 domoritz

@dmchurch do you plan to wrap this up soon?

domoritz avatar May 07 '21 22:05 domoritz

@dmchurch can you please finish up this pull request?

domoritz avatar Jun 29 '21 23:06 domoritz