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

Using -i with wildcard overwrites source JSON file

Open bingtimren opened this issue 4 years ago • 7 comments

To reproduce this problem, download the attached file:

example.zip

Then execute the following commands

unzip ./example.zip 
json2ts -i test/resources/multi-example/**/*.json --cwd test/resources/multi-example/schemas/
cat test/resources/multi-example/schemas/personForm.json 

File "personForm.json" is overwritten. Source file should never be overwritten.

bingtimren avatar Feb 10 '21 11:02 bingtimren

I've encountered the same bug. It seems to occur when the input directory contains a subdirectory and --cwd is provided.

ohueter avatar Mar 26 '21 12:03 ohueter

Hey there! What's it getting overwritten with? We should only be writing .d.ts files, not .json files.

bcherny avatar Mar 28 '21 23:03 bcherny

Please follow the above steps to reproduce the problem. You should find the source test/resources/multi-example/schemas/personForm.json is overwritten after running json2ts.

bingtimren avatar Mar 28 '21 23:03 bingtimren

@bingtimren I'd appreciate it if you could take a few mins to write out what exactly is getting overwritten with what, as part of this issue, so folks don't need to download zip files and debug themselves.

bcherny avatar Mar 28 '21 23:03 bcherny

Ok, the content of the example.zip is as follows:

bing@bing-e480:~/Downloads$ tree
.
└── test
    └── resources
        └── multi-example
            └── schemas
                ├── driver.json
                ├── fleet.json
                ├── personForm.json
                ├── person.json
                └── vehicle.json

4 directories, 5 files

Contents of the files before overwritten

bing@bing-e480:~/Downloads$ for i in test/resources/multi-example/schemas/*; do echo; echo "----------------------- Content of $i ------------------------" ; cat $i;  done

----------------------- Content of test/resources/multi-example/schemas/driver.json ------------------------
{
    "$id": "driver.json",
    "$schema": "http://json-schema.org/draft-07/schema",
    "title": "Driver",
    "description": "Driver",
    "type": "object",
    "allOf": [
        {
            "$ref": "person.json"
        },
        {
            "properties": {
                "licenseNo":{
                    "type":"string",
                    "minLength":3,
                    "maxLength": 30,
                    "pattern": "^[a-zA-Z]+$"
                }
            },
            "required": ["licenseNo"]
        }
    ]
}
----------------------- Content of test/resources/multi-example/schemas/fleet.json ------------------------
{
    "$id": "fleet.json",
    "$schema": "http://json-schema.org/draft-07/schema",
    "title": "Fleet",
    "description": "Fleet",
    "type":"array",
    "items": {
        "type":"object",
        "properties": {
            "driver":{
                "$ref":"driver.json"
            },
            "vehicle":{
                "$ref": "vehicle.json"
            }
        },
        "required": ["driver","vehicle"]
    }
}
----------------------- Content of test/resources/multi-example/schemas/personForm.json ------------------------
{
    "$id": "personForm.json",
    "$schema": "http://json-schema.org/draft-07/schema",
    "title": "Person Form",
    "description": "Person Form with repeat password",
    "type": "object",
    "allOf": [
        {
            "$ref": "person.json"
        },
        {
            "properties": {
                "repeatPassword":{
                    "type":"string",
                    "const":{
                        "$data":"/password"
                    }
                }
            },
            "required":["repeatPassword"]
        }
    ]
}
----------------------- Content of test/resources/multi-example/schemas/person.json ------------------------
{
    "$id": "person.json",
    "$schema": "http://json-schema.org/draft-07/schema#",
    "title": "Person",
    "description": "A Person",
    "type": "object",
    "additionalProperties": true,
    "properties": {
        "name": {
            "type": "string",
            "minLength": 3,
            "maxLength": 20,
            "pattern": "[a-z A-Z ]+"
        },
        "dob": {
            "type": "string",
            "anyOf": [
                {"format": "date"},
                {"format": "date-time"}
            ],
            "olderThanFromNow":567648000000
        },
        "sex": {
            "type": "string",
            "enum": [
                "M",
                "F",
                "O"
            ]
        },
        "password": {
            "type": "string",
            "minLength": 5
        }
    },
    "required": [
        "name",
        "dob",
        "password"
    ]
}
----------------------- Content of test/resources/multi-example/schemas/vehicle.json ------------------------
{
    "$id": "vehicle.json",
    "$schema": "http://json-schema.org/draft-07/schema",
    "title": "Vehicle",
    "description": "Vehicle",
    "type":"object",
    "properties": {
        "type":{
            "type":"string",
            "enum": ["car","bus"]
        },
        "seats":{
            "type":"integer",
            "minimum": 1
        },
        "length":{
            "type":"number",
            "minimum": 0
        }
    },
    "required": ["type","seats","length"]

Command that caused overwritten

bing@bing-e480:~/Downloads$npx json-schema-to-typescript  -i test/resources/multi-example/**/*.json --cwd test/resources/multi-example/schemas/

After issuring the command, source "personForm.json" is overwritten

bing@bing-e480:~/Downloads$ cat test/resources/multi-example/schemas/personForm.json 
/* tslint:disable */
/**
 * This file was automatically generated by json-schema-to-typescript.
 * DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,
 * and run json-schema-to-typescript to regenerate this file.
 */

/**
 * Driver
 */
export type Driver = Person & {
  licenseNo: string;
  [k: string]: unknown;
};
/**
 * Fleet
 */
export type Fleet = {
  driver: Driver;
  vehicle: Vehicle;
  [k: string]: unknown;
}[];

/**
 * A Person
 */
export interface Person {
  name: string;
  dob: (
    | {
        [k: string]: unknown;
      }
    | {
        [k: string]: unknown;
      }
  ) &
    string;
  sex?: "M" | "F" | "O";
  password: string;
  [k: string]: unknown;
}
/**
 * Vehicle
 */
export interface Vehicle {
  type: "car" | "bus";
  seats: number;
  length: number;
  [k: string]: unknown;
}

bingtimren avatar Mar 28 '21 23:03 bingtimren

I see -- it seems like we're not setting the extension properly. Thanks for reporting!

bcherny avatar Mar 29 '21 03:03 bcherny