glideapps / quicktype

Generate types and converters from JSON, Schema, and GraphQL
https://app.quicktype.io
Apache License 2.0
12.31k stars 1.07k forks source link

Typescript doesn't generate correct types when root element is an array of objects #1309

Open MaikuMori opened 5 years ago

MaikuMori commented 5 years ago

When using this JSON Schema as a source:

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "$id": "pr.json",
    "$ref": "#/definitions/GetResp",
    "definitions": {
        "GetResp": {
            "title": "GetResp",
            "type": "array",
            "items": {
                "$ref": "#/definitions/SomeObject"
            }
        },
        "SomeObject": {
            "title": "SomeObject",
            "type": "object",
            "additionalProperties": false,
            "properties": {
                "name": {
                    "type": "string"
                }
            },
            "required": [
                "name"
            ]
        }
    }
}

When I generate Golang types, I get expected result:

type GetResp []SomeObject

type SomeObject struct {
    Name string `json:"name"`
}

While TypeScript output is:

export interface GetResp {
    name: string;
}

I expect it to generate:

export interface SomeObject {
    name: string;
}

export type GetResp = SomeObject[]
Acrontum-Carmichael commented 4 years ago

We're experiencing the same issue.

This JSON Schema:

{
  "id": "http://json-schema.org/geo",
  "$schema": "http://json-schema.org/draft-06/schema#",
  "description": "A geographical coordinate",
  "type": "object",
  "properties": {
     "type": "array",
      "items": {
          "type": "string"
      }
  }
}

When dropped into the online example (https://app.quicktype.io/) returns nothing.

It would be expected to see something like:

interface Name {
    [index: number]: string;
}