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

Setting `declareExternallyReferenced` to true prevents schema from being parsed completely

Open mxro opened this issue 4 years ago • 4 comments

Thank you for this library. Very useful!

I noticed that when I set declareExternallyReferenced to true, only the root object of a schema is parsed but none of the child objects, even though there are no $ref definitions in the schema.

So with declareExternallyReferenced set to true I get:

export interface Package {
  template: Template;
  templateVersion: TemplateVersion;
  name: Name;
  configuration: Configuration;
}

And setting it to false, I get:

/**
 * Name of the template used for creating this package.
 */
export type Template = string;
/**
 * Latest template version that was applied to this package.
 */
export type TemplateVersion = string;
/**
 * Name of this package.
 */
export type Name = string;

/**
 * Definition for a Package.
 */
export interface Package {
  template: Template;
  templateVersion: TemplateVersion;
  name: Name;
  configuration: Configuration;
}
/**
 * Configuration of this package
 */
export interface Configuration {
  [k: string]: unknown;
}

Here the JSON Schema

{
  "type": "object",
  "title": "Package",
  "properties": {
    "template": {
      "type": "string",
      "title": "Template",
      "description": "Name of the template used for creating this package.",
      "pattern": "^[^\\s]*$"
    },
    "templateVersion": {
      "type": "string",
      "title": "Template Version",
      "description": "Latest template version that was applied to this package.",
      "pattern": "^(0|[1-9]\\d*)\\.(0|[1-9]\\d*)\\.(0|[1-9]\\d*)(?:-((?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\\.(?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\\+([0-9a-zA-Z-]+(?:\\.[0-9a-zA-Z-]+)*))?$"
    },
    "name": {
      "type": "string",
      "title": "Name",
      "description": "Name of this package."
    },
    "configuration": {
      "type": "object",
      "properties": {},
      "title": "Configuration",
      "description": "Configuration of this package"
    }
  },
  "description": "Definition for a Package.",
  "additionalProperties": false,
  "required": [
    "template",
    "templateVersion",
    "name",
    "configuration"
  ]
}

This is how I call the library:

const res = await compileFromFile(jsonSource, {
    style: {
      bracketSpacing: true,
      printWidth: 120,
      semi: true,
      singleQuote: true,
      tabWidth: 2,
      trailingComma: 'es5',
      useTabs: false,
    },
    declareExternallyReferenced: true,
    bannerComment,
    ...options,
  });

Is this how declareExternallyReferenced is supposed to work?

mxro avatar Jun 10 '20 22:06 mxro

Hey there, sorry for the delayed response. What is the expected behavior?

declareExternallyReferenced is an old flag in need of some love, and a good place to start would be to specify what folks want it to do.

bcherny avatar Nov 29 '20 22:11 bcherny

Thanks for your response!

I don't know if that is possible but I think one way to handle this is that all types declared in a single JSON file would be included in the generated TS and all externally referenced JSON schemas would not be generated when declareExternallyReferenced is set?

e.g. in the example above the result of generation would be the same if declareExternallyReferenced is set true or false - since there is no externally referenced JSON.

mxro avatar Nov 30 '20 20:11 mxro

Ran into the same issue as @mxro and my expectation would be the same!

I just don't want to have $refs to be resolved and included in the file and it works as long as properties don't have a title. The problem is that for all properties with a title interfaces are generated, but declareExternallyReferenced prevents those internal interfaces to be added to the file

codeliner avatar Dec 30 '21 15:12 codeliner