bcherny/json-schema-to-typescript

Incorrect output for draft6 schema with combination of oneOf + required

petrosv91 opened this issue · 0 comments

Hello everyone, i have the below schema:

{
    "$schema": "http://json-schema.org/draft-06/schema#",
    "description": "Card Data Response",
    "type": "object",
    "properties": {
        "cardDataResponse": {
            "type": "object",
            "properties": {
                "candidateList": {
                    "description": "a list of application candidates",
                    "$ref": "definitions.schema.json#/definitions/candidateList"
                },
                "emvAppData": {
                    "description": "EMV application data",
                    "$ref": "definitions.schema.json#/definitions/emvAppData"
                },
                "ctlessEmvAppData": {
                    "description": "contactless EMV application data",
                    "$ref": "definitions.schema.json#/definitions/ctlessEmvAppData"
                }
            },
            "oneOf": [
                {
                    "required": [
                        "emvAppData"
                    ]
                },
                {
                    "required": [
                        "ctlessEmvAppData"
                    ]
                }
            ]
        }
    },
    "required": [
        "cardDataResponse"
    ]
}

and i use this programmatically code to generate the types:

import { compileFromFile } from 'json-schema-to-typescript';
import { dereference } from '@apidevtools/json-schema-ref-parser';
import fs from 'fs';

// List of schema files
const filteredNames = ['cardDataResponse.schema.json'];

// Function to dereference and compile the schema
filteredNames.forEach((filePath) => {
    // Dereference the schema to resolve all $ref
    dereference(filePath)
        .then((schema) => {
            fs.writeFileSync("test.schema.json", JSON.stringify(schema, null, 2));
            // Compile the dereferenced schema into TypeScript types
            return compileFromFile("test.schema.json", {
                unreachableDefinitions: true, // Ensure unreachable definitions are included
                bannerComment: ""
            });
        })
        .then((ts) => {
            // Append the TypeScript definition to a file
            fs.appendFileSync("schema-types.ts", `${ts}\n`);
            console.log(`Generated types for: ${filePath}`);
        })
        .catch((error) => console.error(`${filePath} => \n` + error));
});

even if i dont use deference and compile directly from file or even if i use const filteredNames = ['cardDataResponse.schema.json', 'definitions.schema.json']; the output i take is [k:string]: unknown. Like the library cannot resolve the type. Every help will be more than welcome, point out or workaround.

Thanks