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

Using -i with wildcard overwrites source JSON file #365

Open
bingtimren opened this issue Feb 10, 2021 · 6 comments
Open

Using -i with wildcard overwrites source JSON file #365

bingtimren opened this issue Feb 10, 2021 · 6 comments

Comments

@bingtimren
Copy link

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.

@ohueter
Copy link

ohueter commented Mar 26, 2021

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

@bcherny
Copy link
Owner

bcherny commented Mar 28, 2021

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

@bingtimren
Copy link
Author

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.

@bcherny
Copy link
Owner

bcherny commented Mar 28, 2021

@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.

@bingtimren
Copy link
Author

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;
}

@bcherny
Copy link
Owner

bcherny commented Mar 29, 2021

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

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

No branches or pull requests

3 participants