quicktype icon indicating copy to clipboard operation
quicktype copied to clipboard

Bug: allow muliple json schema files as input

Open bohndthi opened this issue 3 years ago • 2 comments

Hi, the following input creates unexpected output: quicktype --src-lang schema /schemas/ -o Models.ts

Example: suppose the file /schemas/alert_schema.json:

{
    "type": "object",
    "required": [
        "issue_id",
        "alert_type_id",
        "options"
    ],
    "properties": {
        "issue_id": {
            "type": "string",
            "format": "uuid"
        },
        "alert_type_id": {
            "type": "string",
            "format": "uuid"
        },
        "options": {
            "type": "object"
        }
    },
    "additionalProperites": false
}

actual content of Models.ts:

export interface AlertSchema {
    type:                 Type;
    required:             string[];
    properties:           AlertSchemaProperties;
    additionalProperites: boolean;
}

export interface AlertSchemaProperties {
    issue_id:      AlertTypeID;
    alert_type_id: AlertTypeID;
    options:       Options;
}

export interface AlertTypeID {
    type:   Type;
    format: Format;
}

export enum Format {
    DateTime = "date-time",
    UUID = "uuid",
}

export enum Type {
    Boolean = "boolean",
    Integer = "integer",
    Number = "number",
    Object = "object",
    String = "string",
}

export interface Options {
    type: Type;
}

expected content of Models.ts:

export interface AlertSchema {
    alert_type_id: string;
    issue_id:      string;
    options:       { [key: string]: any };
}

bohndthi avatar Mar 16 '22 11:03 bohndthi

I noticed the same problem. When specifying a directory instead of a single file, the --src-lang schema flag does not seem to work properly. The files are parsed as if they were json files (i.e. examples from which quicktype should derive the schema).

ndepal avatar Mar 16 '22 14:03 ndepal

I found a workaround for this problem: when I rename the file ending of the input file to '.schema' (e.g. /schemas/alert_schema.schema:) quicktype uses the proper 'schema' source type and everything works as expected. However, it would be nice if quicktype would take the --src-lang into account, even for directories

bohndthi avatar Mar 17 '22 09:03 bohndthi

+1 on this, just ran into it.

Another workaround is to specify each file you want as your source json schema independently. Sample command would be something like this:

quicktype -s schema --src schema1.json --src schema2.json -o interfaces.ts

This successfully generated interfaces from both files for me but makes sure to only use common types once - both base schemas reference the same object definition and the generated interfaces only define them once. This might be a slightly cleaner way of doing this that doesn't include changing the schemas to suit quicktype.

Definitely needs to be fixed though.

darcyowens avatar Apr 11 '23 04:04 darcyowens

I threw together a node script to apply @darcyowens workaround (there's a lot of schema files in the folder I need to generate types from and I can't rename than as they linked in an opensource standards project: https://fdc3.finos.org/):

const path = require('path');
const fs = require('fs');
const exec = require('child_process').exec;

const args = process.argv;
const inputFolder = args[2]
const outputFile = args[3];

console.log("Input folder argument: " + inputFolder);
console.log("Output file argument: " + outputFile);

let srcs = "";
fs.readdirSync(inputFolder).forEach(file => {
    srcs += `--src ${path.join(inputFolder, file)} `;
});

// Normalise path to local quicktype executable.
const quicktypeExec = ['.', 'node_modules', '.bin', 'quicktype'].join(path.sep);

const command = `${quicktypeExec} -s schema -o ${outputFile} ${srcs}`;
console.log("command to run: " + command);

exec(command, function(error, stdout, stderr) {
	if (stdout) {
		console.log(stdout);
	}
	if (stderr) {
		console.log(stderr);
	}
});

To run it, perhaps via a npm script, do: node quicktypeUtil.js <path to input folder> <path to output file> (output type is inferred from output file extension)

I agree the bug definitely needs fixing!

kriswest avatar Apr 14 '23 16:04 kriswest