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

description is purged if class attribute has a default value #1531

Open
Zamiell opened this issue Jan 12, 2023 · 7 comments
Open

description is purged if class attribute has a default value #1531

Zamiell opened this issue Jan 12, 2023 · 7 comments
Labels

Comments

@Zamiell
Copy link

Zamiell commented Jan 12, 2023

ts-json-schema-generator appears to be bugged when generating schemas for classes.

For example:

export class Foo {
  /** Whether or not foo has a bar. */
  someAttribute: boolean;
}

This will correctly work to generate:

        "someAttribute": {
          "description": "Whether or not foo has a bar.",
          "type": "boolean"
        },

However, if we change it to have a default property like this:

export class Foo {
  /** Whether or not foo has a bar. */
  someAttribute = true;
}

The description value will be purged:

        "someAttribute": {
          "type": "boolean"
        },
@domoritz
Copy link
Member

There was a recent pull request that added support for default values. Looks like it has a bug. Can you take a look?

@Zamiell
Copy link
Author

Zamiell commented Jan 13, 2023

I believe that was PR #1407.

I don't have the time to do a PR.

@swnf Can you take a look?

@swnf
Copy link
Contributor

swnf commented Jan 13, 2023

For a property with a type annotation, this project uses .parent to get the original PropertyDeclaration (and the corresponding JSDoc);

} else if (node.parent.kind === ts.SyntaxKind.PropertyDeclaration) {
return node.parent;

I think getTypeAtLocation in my code is the problem:

if (memberType === undefined && member.initializer !== undefined) {
const type = this.typeChecker.getTypeAtLocation(member);
memberType = this.typeChecker.typeToTypeNode(type, node, ts.NodeBuilderFlags.NoTruncation);
}

It seems like the result of getTypeAtLocation does not have a reference to the PropertyDeclaration. Therefore, .parent on the resulting type is undefined.

I don't understand this project well enough to fix this properly. I'm not sure why AnnotatedNodeParser is invoked on the type node and not the PropertyDeclaration node (where the JSDoc is still available). It might also be possible to just assign .parent after typeToTypeNode. But I don't think the typescript API is supposed to be used like this. Unfortunately, I don't have more time to analyse this issue.

@Zamiell
Copy link
Author

Zamiell commented Feb 2, 2023

@domoritz Can you please take a look? This issue is blocking use of the schema generator with classes.

@domoritz
Copy link
Member

domoritz commented Feb 2, 2023

I don't have cycles but I could be happy to merge a pull request with a fix or a revert.

@JoshuaCarter
Copy link

For those facing this issue, using the form id: string = "default"; works correctly. You may need to disable @typescript-eslint/no-inferrable-types.

@lordrip
Copy link

lordrip commented Jan 31, 2024

For those facing this issue, using the form id: string = "default"; works correctly. You may need to disable @typescript-eslint/no-inferrable-types.

In my case, using v1.5.0, I couldn't manage to make it work, so I used this instead

  /**
   * Tax percentage
   * @default 0
   */
  tax: number;

This generated:

                "tax": {
                    "type": "number",
                    "description": "Tax percentage",
                    "default": 0
                },

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

No branches or pull requests

6 participants