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

feat: append sourceFileName on BaseType, used by TopRefNodeParser and ExposeNodeParser #1019

Closed
wants to merge 1 commit into from

Conversation

charlzyx
Copy link

for this idea #1018

And here is my custome example

utils/reformatRefDefName.ts

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

export const reformatDefName = (type: BaseType) => {
  let name = type.getName();
  /** myCustomeName Rule */
  if (!(type instanceof DefinitionType) || /def-\[REQ\]/.test(name)) {
    return name;
  }
  if (/__file:\/\/\//.test(name)) {
    return name;
  }
  const source = (type.sourceFileName || '').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;
  }
};

custome DefinitionTypeFormatter

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()),
    ]);
  }
}

custom ReferenceTypeFormatter

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()),
    );
  }
}

Comment on lines +3 to +5
* sourcecode fileName for DefinitionType. used at TopRefNodeParser
* and ExposeNodeParser. so that, you can override DefinitionFormatter
* to custome your self DefinitionType name by fileName.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use proper capitalization.

* and ExposeNodeParser. so that, you can override DefinitionFormatter
* to custome your self DefinitionType name by fileName.
*/
sourceFileName?: string;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this just be in the DefinitionType instead of the base type?


if (baseType === undefined) {
return undefined;
}

if (this.topRef && !(baseType instanceof DefinitionType)) {
return new DefinitionType(this.fullName, baseType);
const defBaseType = new DefinitionType(this.fullName, baseType);
defBaseType.sourceFileName = sourceFileName;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't feel clean. Can we make sourceFileName an argument to the DefinitionType constructor?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your correction. I think it is a very good suggestion. I will try to submit a version again tomorrow to correct these problems.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your pull request.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any updates @charlzyx?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry i'm too late, too busy to submit; And sorry again, i don't know how to submit this or, so i have reopen another one #1056

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you push to the branch this pull request was made from?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry about #1056 this is my first time to use github pr,I'm not familiar with it.
just want submit a cleaning commit,i will try fix next free time.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay. Please notify me when you want a review then.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'm sorry but i have to say a bad info: this PR's fork in my personal repo is deleted, and not local repo. so link [charlzyx:next] override by #1056 's fork. and this PR cannot edit.

image

image

If possible, I suggest closing the current PR and reopening #1056

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, please make sure my comment here are addressed. Ideally, copy them over.

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

Successfully merging this pull request may close these issues.

None yet

2 participants