Skip to content
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

🐛 different output in cli than programatically #565

Open
lukas-runge opened this issue Aug 21, 2023 · 1 comment
Open

🐛 different output in cli than programatically #565

lukas-runge opened this issue Aug 21, 2023 · 1 comment

Comments

@lukas-runge
Copy link

lukas-runge commented Aug 21, 2023

When trying to use typescript-json-schema programatically I stubled across some weird behaviour that did not match the cli output. I build a minimal example to reproduce this issue fast: https://github.com/lukas-runge/tjs-bug-demo. 🙌

The cli command:

typescript-json-schema --required src/udr.next.d.ts Device

The programatic implementation of the "same" thing:

import * as TJS from "typescript-json-schema";

const settings: TJS.PartialArgs = { required: true };

const program = TJS.getProgramFromFiles(["src/udr.next.d.ts"]);

const schema = TJS.buildGenerator(program, settings)?.getSchemaForSymbol("Device")

console.log(JSON.stringify(schema, null, 4));

The programatically generated schema is missing one required property (test):

$ yarn cli && yarn programatic
$ typescript-json-schema --required src/udr.next.d.ts Device
{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "properties": {
        "class": {
            "type": "string"
        },
        "friendlyName": {
            "type": "string"
        },
        "test": {
            "type": "string"
        },
        "unitNumber": {
            "type": "string"
        },
        "userIdentifier": {
            "type": "string"
        }
    },
    "required": [
        "class",
        "test",
        "userIdentifier"
    ],
    "type": "object"
}

$ ts-node main.ts
{
    "type": "object",
    "additionalProperties": {},
    "properties": {
        "class": {
            "type": "string"
        },
        "userIdentifier": {
            "type": "string"
        },
        "friendlyName": {
            "type": "string"
        },
        "unitNumber": {
            "type": "string"
        }
    },
    "required": [
        "class",
        "userIdentifier"
    ],
    "$schema": "http://json-schema.org/draft-07/schema#"
}
✨  Done in 3.15s.

I tried to fix or workaround this on my own but didn't get it resolved. I would really appreciate if someone has an idea for a quick workaround or even fix. 🙏

Looking forward to getting this resolved! 🚀

Best regards,
@lukas-runge

@lukas-runge
Copy link
Author

lukas-runge commented Aug 21, 2023

I just found a workaround when having a deeper look into the inner workings of typescript-json-schema and at least found a viable workaround for my use case.

By passing the files array into the buildGenerator as onlyIncludeFiles-parameter I can match the cli's behaviour. 🥳
I published the workaround under a new branch in my demo repo for reference: lukas-runge/tjs-bug-demo@5f3de74

import * as TJS from "typescript-json-schema";

const settings: TJS.PartialArgs = { required: true };

const files = ["src/udr.next.d.ts"];

const program = TJS.getProgramFromFiles(files);

const schema = TJS.buildGenerator(program, settings, files)?.getSchemaForSymbol("Device")

console.log(JSON.stringify(schema, null, 4));

I still find this shouldn't be the default behaviour. Maybe someone more experienced can explain what's actually going on here under the hood and if it's a bug or a feature. In any case I'd say there needs to be documentation on a programmatical implementation that matches the one of the cli. 🙌

Best regards,
@lukas-runge

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant