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

oneOf convert to unknown after update from v9 to v10

Open joshuaavalon opened this issue 4 years ago • 1 comments

If I use the following schema

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "title": "Condition",
  "properties": {
    "conditions": {
      "type": "object",
      "additionalProperties": {
        "type": "object",
        "items": {
          "oneOf": [
            {
              "$ref": "#/definitions/conditionA"
            },
            {
              "type": "string"
            }
          ]
        }
      }
    }
  },
  "definitions": {
    "conditionA": {
      "title": "ConditionA",
      "type": "object",
      "properties": {
        "type": {
          "type": "string",
          "const": "ConditionA"
        }
      },
      "additionalProperties": false,
      "required": ["type"]
    }
  },
  "additionalProperties": false
}

In 9.10,

export interface Condition {
  conditions?: {
    [k: string]: (ConditionA | string)[]
  }
}
export interface ConditionA {
  type: string
}

in 10.1.0,

export interface Condition {
  conditions?: {
    [k: string]: {
      [k: string]: unknown;
    };
  };
}

joshuaavalon avatar Mar 29 '21 06:03 joshuaavalon

@joshuaavalon Hi there, I'm new to this library and json-schema but thought I'd take a crack at looking into this issue.

I think the posted version 10 output is correct based on the provided schema because items isn't a valid key on the object type. I updated the type of the additionalProperties in the posted schema to array and that seems to produce the desired result in version 10.1.4.

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "title": "Condition",
  "properties": {
    "conditions": {
      "type": "object",
      "additionalProperties": {
        "type": "array",
        "items": {
          "oneOf": [
            {
              "$ref": "#/definitions/conditionA"
            },
            {
              "type": "string"
            }
          ]
        }
      }
    }
  },
  "definitions": {
    "conditionA": {
      "title": "ConditionA",
      "type": "object",
      "properties": {
        "type": {
          "type": "string",
          "const": "ConditionA"
        }
      },
      "additionalProperties": false,
      "required": ["type"]
    }
  },
  "additionalProperties": false
}

in 10.1.4,

export interface Condition {
  conditions?: {
    [k: string]: (ConditionA | string)[];
  };
}
export interface ConditionA {
  type: "ConditionA";
}

My guess here is that improvements to the library between v9 and v10 have resulted in a more accurate type definition.

ericArbour avatar Apr 03 '21 17:04 ericArbour