json-schema-to-typescript icon indicating copy to clipboard operation
json-schema-to-typescript copied to clipboard

$merge in schema produces empty typescript interface

Open mukul1203 opened this issue 5 years ago • 4 comments

I have a json schema using keyword $merge (to merge two subschemas, where allof fails due to the 'additionalProperties=false' issue). Passing it through the json-schema-to-typescript gives an empty interface.

mukul1203 avatar Mar 16 '20 10:03 mukul1203

Is $merge part of the JSON-Schema spec, or is it a custom extension you're using?

bcherny avatar May 02 '20 20:05 bcherny

I just came across this issue looking to see if json-schema-to-typescript supports $merge and $patch. I'm only learning this stuff as I go, but maybe if I add in my senario it'll help with context.

The base JSON-Schema spec doesn't have strong support for extending/inheriting schemas. This can make it difficult to write complex schemas without a lot of copy-paste or undesirable behaviours^. Instead there are are two additional specifications to help express schemas in a DRY way $extend and $merge.

As they are seperate specifications some tools choose to support them but via an optional add-on. ajv for example has ajv-merge-patch.

RFC: JavaScript Object Notation (JSON) Patch RFC: JSON Merge Patch

So I believe the answer is yes it's a "custom extension", but it is a somewhat standard extension.

As I said I'm learning this stuff now so I could be wrong, but I hope that helps. I was investigating using $extend in my work to improve the generated types/schemas, but I'm not sure if it's ideal.

^I'm seeing undesirable behaviour when I use allOf and $ref syntax, the tools are working as intended but I'm not getting the ideal end result, which lead me to $extend.

jahredhope avatar Jun 10 '20 01:06 jahredhope

Shoot, just bumped into the same issue, using $merge for my schemas and trying to generate TS from it.

I'd settle for a way to generate a 'normal' schema from one that has '$merge' in it, I'm also learning, did you find a way in the mean time @jaredhope?

raarts avatar Jul 03 '20 21:07 raarts

If someone wants to take this on, a good start would be speccing out how exactly $merge differs from allOf. If the two are identical, this could simply be implemented as a normalizer rules that transforms $merge to allOf.

bcherny avatar Nov 29 '20 22:11 bcherny