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

question: How to serialize a class instance with default values automatically removed? #1505

Closed
ahochsteger opened this issue Apr 17, 2023 · 4 comments
Labels
type: question Questions about the usage of the library.

Comments

@ahochsteger
Copy link

I was trying to...
I have a large config class in TypeScript with a lot of default values defined in the class (e.g. empty strings, some boolean values, empty arrays) that I want to serialize back to a minimal JSON representation that just contain the essential part of the config with all properties removed that have values identical to their defined default values.

The problem:
How can I do that in a generic way by just using the information that is available from the class definition without having add extra annotation to these properties?

@ahochsteger ahochsteger added the type: question Questions about the usage of the library. label Apr 17, 2023
@diffy0712
Copy link

Hello @ahochsteger,

could you please provide an example for your question? If I understand correctly, there is no option for what you want, but I cannot say it for sure without an example.

Thanks

@ahochsteger
Copy link
Author

ahochsteger commented May 17, 2024

@diffy0712 have a look at this commented-out test case that is currently failing:
https://github.com/ahochsteger/gmail-processor/blob/main/src/test/environment/class-transformer.spec.ts#L182-L192

// See the source for the class definitions
it("should serialize to JSON config without default values", () => {
  const obj = getNewRootObj(jsonData)
  const actual: any = instanceToPlain(obj, {
    exposeDefaultValues: false,
    exposeUnsetFields: false,
  })
  expect(actual).toMatchObject(jsonData) // <- this is OK
  expect(actual.str).toBeUndefined() // <- this is failing
  expect(actual.nested?.num).toBeUndefined() // <- this is failing
  expect(actual.nested?.bool).toBeUndefined() // <- this is failing
  expect(actual.nestedArray[0]?.bool).toBeUndefined() // <- this is failing
  expect(actual.nestedArray[0]?.arr).toBeUndefined() // <- this is failing
})

It looks like it's the same as this issue: #1609

@diffy0712
Copy link

hello @ahochsteger,
Yes it seems that it is the same problem as #1609.
if you use exposeUnsetFields:false and exposeDefaultValues:false together it does not work as expected.

Closing this issue in favor of #1609, please follow the bugfix there.

@diffy0712 diffy0712 closed this as not planned Won't fix, can't repro, duplicate, stale May 17, 2024
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 17, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
type: question Questions about the usage of the library.
Development

No branches or pull requests

2 participants