bcherny/json-schema-to-typescript

`declareExternallyReferenced` should be respected for non-interface types, too

ericmorand opened this issue · 1 comments

Consider the following JSON schema files:

foo.schema.json

{
  "title": "Foo",
  "oneOf": [
    {
      "$ref": "./bar.schema.json"
    }
  ]
}

bar.schema.json

{
  "title": "Bar",
  "type": "string"
}

Compiling foo.schema.json with the option declareExternallyReferenced set to false would lead to bar.schema.json being present in the generated code:

import {compileFromFile} from 'json-schema-to-typescript'
import {ensureDir} from 'fs-extra'

ensureDir('dist')
  .then(() => {
    const schema = 'src/foo.schema.json';

    return compileFromFile(schema, {
      declareExternallyReferenced: false
    });
  })
  .then((code) => {
    console.log(code);
  });
/* eslint-disable */
/**
 * This file was automatically generated by json-schema-to-typescript.
 * DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,
 * and run json-schema-to-typescript to regenerate this file.
 */

export type Foo = Bar;
export type Bar = string;

Except if I'm missing the purpose of declareExternallyReferenced , the generated code should be:

/* eslint-disable */
/**
 * This file was automatically generated by json-schema-to-typescript.
 * DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,
 * and run json-schema-to-typescript to regenerate this file.
 */

export type Foo = Bar;

This is a bug. declareExternallyReferenced should work for all external types, but today it only works for interface types.