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

Classes with default values don't work #1390

Closed
Zamiell opened this issue Sep 3, 2022 · 4 comments
Closed

Classes with default values don't work #1390

Zamiell opened this issue Sep 3, 2022 · 4 comments

Comments

@Zamiell
Copy link

Zamiell commented Sep 3, 2022

Hello, and thanks for the tool.

Unfortunately, classes seem to be broken. For example:

foo.ts

export class Foo {
  field1 = "someValue";
  field2?: string;
}

Feeding this file to ts-json-schema-generator will result in a schema that looks like this:

foo-schema.json

{
  "$ref": "#/definitions/Foo",
  "$schema": "http://json-schema.org/draft-07/schema#",
  "definitions": {
    "Foo": {
      "additionalProperties": false,
      "properties": {
        "field2": {
          "type": "string"
        }
      },
      "type": "object"
    }
  }
}

As you can see, field1 is completely missing and only field2 is listed. It seems like the tool skips over any fields that are defined with a default value, which makes it mostly useless for people who want to generate a schema from a class. :(

@domoritz
Copy link
Member

domoritz commented Sep 4, 2022

Good catch. Can you send a fix?

@Zamiell
Copy link
Author

Zamiell commented Sep 22, 2022

Sorry, I don't have the time. If you can fix it, I'd be greatly appreciative. Otherwise, I guess I will try to find a separate tool that works with classes. :(

@ThatOneAwkwardGuy
Copy link
Contributor

ThatOneAwkwardGuy commented Sep 25, 2022

Had a look, the type appears to get lost in the InterfaceAndClassNodeParser file, due to the

member.type

check, inside of

 .filter((member) => isPublic(member) && !isStatic(member) && member.type && !isNodeHidden(member))

doing this is an intermediary fix:

export class Foo {
  field1:string = "someValue";
  field2?: string;
}

Is this overly verbose? yup. But don't have enough experience with the codebase to solve this one.

update: #1407 should fix this

@domoritz
Copy link
Member

domoritz commented Dec 3, 2022

Done with #1407

@domoritz domoritz closed this as completed Dec 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants