APIDevTools / json-schema-ref-parser

Parse, Resolve, and Dereference JSON Schema $ref pointers in Node and browsers
https://apitools.dev/json-schema-ref-parser
MIT License
942 stars 226 forks source link

Numbers being appended to the TypeScript interface names when using the same $ref multiple times #326

Closed herrherrmann closed 5 months ago

herrherrmann commented 11 months ago

Hey there,

I’m trying to resolve some $refs in a YML model that looks like this:

type: object
required:
  - title
properties:
  identifier:
    type: string
  title:
    $ref: "TranslatableField.yml"
    example:
      de: Konzert
      en: concert
  displayName:
    $ref: "TranslatableField.yml"
  description:
    $ref: "TranslatableField.yml"
    example:
      de: Eine Beschreibung
      en: Some description

TranslatableField.yml looks like this:

type: object
additionalProperties:
  type: string
description: |-
  A string that can be translated into multiple languages, e.g. { "de": "Konzert", "en": "concert" }
example:
  de: Konzert
  en: concert

As you can see, I’m referencing TranslatableField multiple times. I would expect my TypeScript interface to look like this after calling compile():

export interface Model {
    title: TranslatableField
    displayName?: TranslatableField
    description?: TranslatableField
}

… but what I get is this:

export interface Model {
    title: TranslatableField
    displayName?: TranslatableField1
    description?: TranslatableField2
}

Note the extra numbers at the end of TranslatableField. With declareExternallyReferenced: false this breaks the generated TypeScript file, because that’s an invalid model name that cannot be found/imported from anywhere. I’d like to reuse the same name (TranslatableField without the number) for all fields.

Here’s a full example: https://codesandbox.io/p/sandbox/json-schema-to-typescript-test-8n5d9s

Is there any way to prevent these extra number suffixes, e.g. with some configuration? 🤔

I appreciate any hints or ideas!

References

jonluca commented 5 months ago

This does not seem to be an issue with json-schema-ref-parser - the json schema returned by the ref parser is valid, and does not append the names. This looks like an issue in the library you are using.