If I send a JSON body with a wrong type (e.g. integer where a string is expected in a model by Asp.Net Core), I get a following message:
"$.name": [ "The JSON value could not be converted to System.String. Path: $.name | LineNumber: 1 | BytePositionInLine: 11." ]
This information is not very clear for a random user of a Web Api. Additionally if the same error happens with enum, it will also leak information about fully qualified name of the enum (including namespace) and is even more meaningless for a user than System.String
.
Is there any better way to handle this in WebApi? E.g. to change all messages to a generic Value was not of expected type
or something like that?
I know of two possible solutions, but both are rather cumbersome:
- All models should accept string (though we'll still get the one with string, but it's at least more or less understandable) or object (if it's possible). Then all the validation and mapping should be done manually
- Wringing a Converter for each type that is used in model and supplying it to WebApi to use
I really hope there is a better way that allows us to e.g "catch" the exception from parser or inherit from a default converter, etc.