json2typescript icon indicating copy to clipboard operation
json2typescript copied to clipboard

Using JsonCustomConverter causes Expected type to be undefined

Open fider opened this issue 7 years ago • 4 comments

Example:

@JsonConverter
class StringConverter implements JsonCustomConvert<string> {
    serialize(data: Data): any {return "..."}

    deserialize(str: any): Data {
        if ( ! isValid( str ) ) {
          throw new Error(`Invalid value is ${str}`);
        }
        return str;
    }
}

class Data {
    @JsonProperty("name", StringConverter )
    public name: string = undefined as any;
}

let data = { name: "invalid_name" }
jsonConvert.deserialize(data, Data);

Will thorw error message:

`
        Class property:
                name

        Expected type:
                undefined

        JSON property:
                name

        JSON type:
                string

        JSON value:
                Invalid value is invalid_name
`

I expect to be able to determine Expected type because in above case it is undefined

fider avatar Sep 11 '18 22:09 fider

The error message should be indeed improved. It cannot determine the expected type, but indeed we should fix this to show CustomConverter as expected type.

I don't understand the logic above, there are maybe some things to correct:

  • <String> should be <string> on the top
  • you should return a date object in the deserialize method, not just the string

What is isValid? Does it check whether the input string is a string that can be used as date?

andreas-aeschlimann avatar Sep 11 '18 23:09 andreas-aeschlimann

@andreas-aeschlimann should be datA not datE - just copy paste error of mine. It can be usefull if eg. you want to validate string that should not be empty. In this case you have to use custom validator but when you will provide invalid empty string value then you will receive message that "Expected type" is undefined (and IMO it should be custom validator class name eg. NonEmptyString).

If you are ok with this let me know, I can send you fix or just allow me to push something on separate branch so I can open pull request to master.

fider avatar Sep 12 '18 00:09 fider

@andreas-aeschlimann IMO below should be enoug to fix. Please verify and apply if you see no bugs in this solution:

FILE: https://github.com/dhlab-basel/json2typescript/blob/master/src/json2typescript/json-convert-decorators.ts

TODO: Do not put line 171 in else

/*170*/        } else {
/*171*/            jsonPropertyMappingOptions.expectedJsonType = conversionOption;
/*172*/        }

fider avatar Sep 19 '18 15:09 fider

Line 171 is correct actually.

If you provide a custom converter, the expected type would be the one given in the methods of the converter.

The problem is somewhere else: The error that you throw is caught by json2typescript within the class JsonConvert. We need to improve that error message depending on the caller.

However: I planned to add custom errors in the next big version to improve error handling. This might help in this case.

andreas-aeschlimann avatar Feb 13 '19 01:02 andreas-aeschlimann