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

declareExternallyReferenced should be respected for non-interface types, too #525

Open
ericmorand opened this issue May 15, 2023 · 1 comment

Comments

@ericmorand
Copy link

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;
@bcherny bcherny changed the title compileFromFile is not respecting declareExternallyReferenced option declareExternallyReferenced should be respected for non-interface types, too Jun 10, 2023
@bcherny
Copy link
Owner

bcherny commented Jun 10, 2023

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

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

Successfully merging a pull request may close this issue.

2 participants