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

Non-deterministic results using `getSchemaForSymbol`

Open jsakas opened this issue 3 years ago • 1 comments

I am experiencing some behavior that I am unsure if it is a feature or a bug. If I run multiple calls to getSchemaForSymbol I am not guaranteed to get the same results for each schema. Sometimes they are missing the properties key and come with a $ref instead. This s making it challenging for using this tool in some data processing pipelines.

If I run a single call, I will get something that looks like this:

{
  "type": "object",
  "properties": {},
  "required": [],
  "definitions": {},
  "$schema": "http://json-schema.org/draft-07/schema#"
}

If I run multiple calls to getSchemaForSymbol, the later results often come looking like below. Notice that there is no type or properties:

{
  "$ref": "#/definitions/MyDefinition",
  "required": [],
  "definitions": {},
  "$schema": "http://json-schema.org/draft-07/schema#"
}

If I run the same object again on its own, it comes through correctly.

jsakas avatar Jan 27 '22 19:01 jsakas

@jsakas Internally TJS is using the Type's name as the index key on an internal storage map (FQN might be better). I fixed it by forcing our internal types to take precedence when building the generator.

buildGenerator function takes a third argument to choose when multiple types have the same name.

const settings = { required: true };
const generator = TJS.buildGenerator(
  program, 
  settings, 
  { indexOf: (file) => file.indexOf('/packages/types/src/') } // or an array of all the source files to prioritize
);

puppybits avatar Feb 10 '22 19:02 puppybits