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

Bug when using package in NuxtJS #120

Closed jonthemiller closed 4 years ago

jonthemiller commented 4 years ago

I'm having an issue when specifying the second parameter for the @JsonProprty decorator, and only within the context of a NuxtJS application. I filed this bug to the nuxt/typescript project and was wondering if you had any further insights as to what might be occurring: https://github.com/nuxt/typescript/issues/274

Specifically, the conversion fails when trying to specify an array of objects. I am able to work around the issue in my project by using a custom serializer. The linked issue has an example project that reproduces the issue. I have also created a project showing json2typescript working perfectly on its own, so my assumption has been that there is an issue with the typescript support in Nuxt.

Let me know if I can provide more information or context.

andreas-aeschlimann commented 4 years ago

Hello and thanks for posting the issue.

It is actually the first time I hear about NuxtJS, so I am afraid I will not be of any help...

Could it be connected to the way the code is compiled?

jonthemiller commented 4 years ago

I'd assume that it has to be the case as well, just not familiar enough with it to pinpoint exactly what.

Another weird issue I've come across when using the package within Nuxt. If I do a class like the following, to account for the possibility of a field not being present:

import { JsonObject, JsonProperty } from 'json2typescript';

@JsonObject('Obj')
export default class Obj {
  @JsonProperty('aString', String, true)
  public aString: string|undefined = undefined;
}

And convert an object that does have the field, I get an exception:

    const jsonObj = {
      aString: 'foobar'
    };
    // init the converter
    let jsonConvert = new JsonConvert();
    // attempt to create an Obj instance from the JS object
    // error gets thrown here
    let convertedObj = jsonConvert.deserializeObject(jsonObj, Obj);
Fatal error in JsonConvert. Failed to map the JSON object to the class "Obj" because of a type error. Class property: aString Expected type: String JSON property: aString JSON type: string JSON value: "foobar" Reason: Expected type is unknown. There might be multiple reasons for this: - You are missing the decorator @JsonObject (for object mapping) - You are missing the decorator @JsonConverter (for custom mapping) before your class definition - Your given class is undefined in the decorator because of circular dependencies

This does work, however, if the @jsonProperty second argument is Any. So it seems like the issues I'm having are specifically around the second argument for that decorator, and only when passing generic types or the special array notation. I've changed my tsconfig.json

jonthemiller commented 4 years ago

Here's the repo showing it breaking in Nuxt: https://github.com/jonthemiller/nuxt-ts-array-deserialize

And the repo working with the library on its own: https://github.com/jonthemiller/json2typescript-example

andreas-aeschlimann commented 4 years ago

I am sorry that I have not the knowledge or resources to help with that.

If your issue is still active or you have information to share, please feel free to reopen.