ts-json-schema-generator icon indicating copy to clipboard operation
ts-json-schema-generator copied to clipboard

Custom ReferenceTypeFormatter and DefinitionTypeFormatter name by source fileName

Open charlzyx opened this issue 3 years ago • 2 comments

Hey, guys, i have an idea; by defaults, export alias and top ref node in defs name looks like this, not friendly to read. i want to rename it by alias Identity and folder name, or it means named by fileName, so i suggest add source fileName info on the BaseType , at least, DefinitionType, the patch for 0.97.0 looks like this

diff --git a/node_modules/ts-json-schema-generator/dist/src/ExposeNodeParser.js b/node_modules/ts-json-schema-generator/dist/src/ExposeNodeParser.js
index 49cc4c6..d679512 100644
--- a/node_modules/ts-json-schema-generator/dist/src/ExposeNodeParser.js
+++ b/node_modules/ts-json-schema-generator/dist/src/ExposeNodeParser.js
@@ -24,9 +24,12 @@ class ExposeNodeParser {
             return undefined;
         }
         if (!this.isExportNode(node)) {
+            baseType.source = node.getSourceFile().fileName;
             return baseType;
         }
-        return new DefinitionType_1.DefinitionType(this.getDefinitionName(node, context), baseType);
+        const type = new DefinitionType_1.DefinitionType(this.getDefinitionName(node, context), baseType);
+        type.source = node.getSourceFile().fileName;
+        return type;
     }
     isExportNode(node) {
         if (this.expose === "all") {
diff --git a/node_modules/ts-json-schema-generator/dist/src/TopRefNodeParser.js b/node_modules/ts-json-schema-generator/dist/src/TopRefNodeParser.js
index 6bc8406..c4a0df0 100644
--- a/node_modules/ts-json-schema-generator/dist/src/TopRefNodeParser.js
+++ b/node_modules/ts-json-schema-generator/dist/src/TopRefNodeParser.js
@@ -2,6 +2,7 @@
 Object.defineProperty(exports, "__esModule", { value: true });
 exports.TopRefNodeParser = void 0;
 const DefinitionType_1 = require("./Type/DefinitionType");
+
 class TopRefNodeParser {
     constructor(childNodeParser, fullName, topRef) {
         this.childNodeParser = childNodeParser;
@@ -14,12 +15,17 @@ class TopRefNodeParser {
             return undefined;
         }
         if (this.topRef && !(baseType instanceof DefinitionType_1.DefinitionType)) {
-            return new DefinitionType_1.DefinitionType(this.fullName, baseType);
+            const type = new DefinitionType_1.DefinitionType(this.fullName, baseType);
+            type.source = node.getSourceFile().fileName;
+            return type;
         }
         else if (!this.topRef && baseType instanceof DefinitionType_1.DefinitionType) {
-            return baseType.getType();
+            const type = baseType.getType();
+            type.source = node.getSourceFile().fileName;
+            return type;
         }
         else {
+            baseType.source = node.getSourceFile().fileName;
             return baseType;
         }
     }

so that, i can custom DefinitionTypeFormatter and ReferenceTypeFormatter like this

reformatRefDefName.ts

import { DefinitionType, ReferenceType } from 'ts-json-schema-generator';

export const reformatDefName = (type: any) => {
  let name = type.getName();
  if (!(type instanceof DefinitionType) || /def-\[REQ\]/.test(name)) {
    return name;
  }
  if (/__file:\/\/\//.test(name)) {
    return name;
  }
  const source = ((type as any).source || '').replace(process.cwd(), '');
  const ref = name + (source ? `__file:///${source}` : '');
  (type as any).name = ref;
  return ref;
};

export const reformatRefName = (type: any) => {
  let name = type.getName();
  if (!(type instanceof ReferenceType)) {
    return name;
  }
  const isDef = (type as any).type instanceof DefinitionType;
  if (!isDef) return name;
  const defName = (type as any)?.type?.name;
  const isDiff = defName !== name;
  if (isDiff) {
    (type as any).name = defName;
    return defName;
  } else {
    return name;
  }
};

DefinitionTypeFormatter.ts

import {
  BaseType,
  Definition,
  DefinitionType,
  SubTypeFormatter,
  TypeFormatter,
  uniqueArray,
} from 'ts-json-schema-generator';
import { reformatDefName } from '../utils/reformatRefDefName';

export class DefinitionTypeFormatter implements SubTypeFormatter {
  public constructor(
    private childTypeFormatter: TypeFormatter,
    private encodeRefs: boolean,
  ) {}

  public supportsType(type: DefinitionType): boolean {
    return type instanceof DefinitionType;
  }
  public getDefinition(type: DefinitionType): Definition {
    const ref = reformatDefName(type);
    return {
      $ref: `#/definitions/${this.encodeRefs ? encodeURIComponent(ref) : ref}`,
    };
  }
  public getChildren(type: DefinitionType): BaseType[] {
    return uniqueArray([
      type,
      ...this.childTypeFormatter.getChildren(type.getType()),
    ]);
  }
}

ReferenceTypeFormatter.ts

import {
  BaseType,
  Definition,
  DefinitionType,
  ReferenceType,
  SubTypeFormatter,
  TypeFormatter,
} from 'ts-json-schema-generator';
import { reformatRefName } from '../utils/reformatRefDefName';

export class ReferenceTypeFormatter implements SubTypeFormatter {
  public constructor(
    private childTypeFormatter: TypeFormatter,
    private encodeRefs: boolean,
  ) {}

  public supportsType(type: ReferenceType): boolean {
    return type instanceof ReferenceType;
  }
  public getDefinition(type: ReferenceType): Definition {
    const ref = reformatRefName(type);
    return {
      $ref: `#/definitions/${this.encodeRefs ? encodeURIComponent(ref) : ref}`,
    };
  }
  public getChildren(type: ReferenceType): BaseType[] {
    if (type.getType() instanceof DefinitionType) {
      return [];
    }

    // this means that the referred interface is private
    // so we have to expose it in the schema definitions
    return this.childTypeFormatter.getChildren(
      new DefinitionType(reformatRefName(type), type.getType()),
    );
  }
}

charlzyx avatar Nov 17 '21 02:11 charlzyx

Do you want to send a pull request?

domoritz avatar Nov 17 '21 14:11 domoritz

Do you want to send a pull request?

it's my pleasure ! #1019

charlzyx avatar Nov 17 '21 15:11 charlzyx