quicktype icon indicating copy to clipboard operation
quicktype copied to clipboard

Schema are different between `--src FILE|DIRECTORY`

Open rkeely opened this issue 5 years ago • 3 comments

With

src
├── source.json
└── source2.json

I generated schemas with 2 approaches:

  • npx quicktype src -l schema -o schema-src.json
  • npx quicktype ./src/source.json ./src/source2.json -l schema -o schema-json.json

With schema-src.json I can get correct types. With schema-src.json. the type is empty

Seems when input is a DIRECTORY and it contains more than one json, the output schema is missing "$ref"

diff on schema diff -y ./schema-src.json ./schema-json.json:

{								{
    "$schema": "http://json-schema.org/draft-06/schema#",	    "$schema": "http://json-schema.org/draft-06/schema#",
							      >	    "$ref": "#/definitions/SchemaJSON",
    "definitions": {						    "definitions": {
        "Source": {					      |	        "SchemaJSON": {
            "type": "object",					            "type": "object",
            "additionalProperties": false,			            "additionalProperties": false,
            "properties": {					            "properties": {
                "name": {					                "name": {
                    "type": "string"				                    "type": "string"
                },						                },
                "id": {						                "id": {
                    "type": "integer"				                    "type": "integer"
                }					      |	                },
            },						      <
            "required": [				      <
                "id",					      <
                "name"					      <
            ],						      <
            "title": "Source"				      <
        },						      <
        "Source2": {					      <
            "type": "object",				      <
            "additionalProperties": false,		      <
            "properties": {				      <
                "gender": {					                "gender": {
                    "type": "string"				                    "type": "string"
                }						                }
            },							            },
            "required": [				      |	            "required": [],
                "gender"				      |	            "title": "SchemaJSON"
            ],						      <
            "title": "Source2"				      <
        }							        }
    }								    }
}

rkeely avatar Aug 13 '20 02:08 rkeely

I might have a similar problem.. (in C++, at least) and can't find any documentation about the CLI parameters and their use?

felix-kolbe avatar Apr 28 '23 09:04 felix-kolbe

There's a typo in the bug report I think. I think it meant to say:

With schema-json.json I can get correct types. With schema-src.json. the type is empty

I think the issue (or maybe this is a feature?) is taking an input as a directory with multiple files is treated differently than multiple individual files as input.

So input of src/ is different from src/source.json src/source2.json. The directory input is required sometimes though if we have too many files as input that we can't shell glob as src/*.

Workaround: I had to merge the many src/ files into one file and use that one combined file as input. Then I created a json schema output, and manually edited that before generating final code.

Request: can there be a flag to treat a directory input as if we listed all the files individually? Or treat the directory as multiple separate entities, one per file (as is the case today when given a directory input with multiple files)

jawspeak avatar Jul 18 '23 14:07 jawspeak

@jawspeak I can only give you these hints, maybe it helps you or someone else converting for C++:

  • Naming the schema files config.schema instead of config.json actually fixed us one or the other issue.
  • Since we got it working, we're only converting folders, i.e. passing the schema folder's path to quicktype's --src parameter. No file globbing or such.
  • Regarding the filename/path passed to --out (which will be the top generated header):
    • If quicktype is called for a single schema, the out path has to have the same name of that schema.
    • When calling quicktype for multiple schemas, it has to be a different name. That top header will include each schema's (and their subschema's) header.

felix-kolbe avatar Aug 28 '23 12:08 felix-kolbe