bcherny/json-schema-to-typescript

2 bugs in conversion of openapi json schema.

Open

#315 opened on Jun 26, 2020

View on GitHub
 (4 comments) (0 reactions) (0 assignees)TypeScript (449 forks)github user discovery
bughelp wanted

Repository metrics

Stars
 (3,302 stars)
PR merge metrics
 (No merged PRs in 30d)

Description

I tried to convert the json schema defintion for openapi to a schema.t.ts.

At first it looked succesful, but there are several details which are omitted from the final schema. Here are some examples:

The resulting main node is represented by the generated HttpsSpecOpenapisOrgOas30Schema20190402 interface. This interface describes paths, which is correct.

In reality in a json data file, the paths element could look like:

paths:  {
    "/some/random/text": {...}
    "/another/random/text": {...}
 }

By contrast, the generated interface for this paths field is just an empty type.

export interface Paths {}

It should have been more like:

export interface Paths {
  [path: string]: ...
}

The openapi json schema defines it using a reference:

"paths": {
  "$ref": "#/definitions/Paths"
},

This reference to #/definitions/Paths actually points to:

"Paths": {
  "type": "object",
  "patternProperties": {
    "^\\/": {
      "$ref": "#/definitions/PathItem"
    },
    "^x-": {
    }
  },
  "additionalProperties": false
},

I have to admit, there is something special about this case, because it uses a regex for its property names. The regex is used to define that its properties (e.g. /some/random/text) should start with a slash.

Well, right now, it just ommits those properties in the generated interface. But it would make more sense to just allow a string, right ?


Another issue is how the Parameter's are converted. The json schema doc defines a required name field, some other properties and finally an allOf of 3 other types.

"Parameter": {
  "type": "object",
  "properties": {
    "name": {
      "type": "string"
    },
    ...
  }
  ...
  "patternProperties": {
    "^x-": {
    }
  },
  "additionalProperties": false,
  "required": [
    "name",
    "in"
  ],
  "allOf": [
    {
      "$ref": "#/definitions/ExampleXORExamples"
    },
    {
      "$ref": "#/definitions/SchemaXORContent"
    },
    {
      "$ref": "#/definitions/ParameterLocation"
    }
  ]
},

Unfortunately, it just generates the following union type:

export type Parameter = ExampleXORExamples & SchemaXORContent & ParameterLocation;

It only used the allOf section, but just forgot all about the properties which were defined on Parameter itself. As a result crucial properties like name are just missing.


I hope this is of use for somebody. 👍 Keep up the good work.

Contributor guide