Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect output for draft6 schema with combination of oneOf + required #630

Open
petrosv91 opened this issue Sep 10, 2024 · 0 comments
Open

Comments

@petrosv91
Copy link

petrosv91 commented Sep 10, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant