appvision-gmbh / json2typescript

Map JSON to a TypeScript class with secure type checking!
https://www.npmjs.com/package/json2typescript
MIT License
278 stars 55 forks source link

Behavior with IgnorePrimitiveChecks = true #175

Open andreas-aeschlimann opened 2 years ago

andreas-aeschlimann commented 2 years ago

I noticed that if we set ignorePrimitiveChecks = true, then all undefined properties are ignored as well.

Example:

JSON:

{ "testt": "Hello World" }

TYPESCRIPT:

class Test {
  @JsonProperty("test", "String") test: string = ""; 
}

Deserializing the JSON above usually results in an error (property test not found in the JSON). However, when ignorePrimitiveChecks = true, then the error is ignored.

The reason is the following: json2typescript considers a missing property undefined. undefined is a primitive property and we ignore the type. Instead, json2typescript maps the undefined value to the test property in the class.

Is this behavior to be expected? Or should we treat a missing property different?

Note again, that in case ignorePrimitiveChecks = false, which is the default, there is an exception because undefined cannot be mapped as a String.

CC: @tobiasschweizer @antal-horvath