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

Using JsonCustomConverter causes Expected type to be undefined #75

Open
fider opened this issue Sep 11, 2018 · 4 comments
Open

Using JsonCustomConverter causes Expected type to be undefined #75

fider opened this issue Sep 11, 2018 · 4 comments
Assignees
Labels
enhancement New feature or request invalid This doesn't seem right

Comments

@fider
Copy link

fider commented Sep 11, 2018

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

@andreas-aeschlimann
Copy link
Member

andreas-aeschlimann commented Sep 11, 2018

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 andreas-aeschlimann self-assigned this Sep 11, 2018
@andreas-aeschlimann andreas-aeschlimann added the enhancement New feature or request label Sep 11, 2018
@fider
Copy link
Author

fider commented Sep 12, 2018

@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
Copy link
Author

fider commented Sep 19, 2018

@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*/        }

@andreas-aeschlimann
Copy link
Member

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 andreas-aeschlimann added the invalid This doesn't seem right label Feb 27, 2019
@andreas-aeschlimann andreas-aeschlimann added this to the version 2.x milestone Feb 27, 2019
@andreas-aeschlimann andreas-aeschlimann removed this from the version 2.x milestone May 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request invalid This doesn't seem right
Projects
None yet
Development

No branches or pull requests

2 participants