FSharp.Data icon indicating copy to clipboard operation
FSharp.Data copied to clipboard

JsonProvider accepts absurd input

Open akdor1154 opened this issue 6 years ago • 1 comments

JsonProvider doesn't seem to care if non-optional fields are completely missing, it happily infers them as an empty string.

type MyType = JsonProvider<"""{"field": "asdf"}""">

[<Fact>]
let ``json test`` () =
    let t = MyType.Parse("""{"not":"valid"}""")
    printf "field: \"%s\". type: %O" t.Field (t.Field.GetType())

Expected behaviour - should throw an exception. Actual behaviour: field: "". type: System.String

akdor1154 avatar Jan 18 '19 03:01 akdor1154

I agree this is not how I expected this case to behave either, but this is apparently by design: missing strings are transformed into "" and missing floats are transformed into nan. See this PR from 2013 #234 (and #66/#442 too)

This is implemented here https://github.com/fsprojects/FSharp.Data/blob/fd0fbccdbca7c697c8a30012754f77225b045ed1/src/Json/JsonRuntime.fs (and here for providers other than json https://github.com/fsprojects/FSharp.Data/blob/fd0fbccdbca7c697c8a30012754f77225b045ed1/src/CommonRuntime/TextRuntime.fs)
(so it's not just json. I verified it was the same with the xml provider and the csv provider too)

Other property types (e.g. int) throw on access when missing.

IMO the special casing for strings and floats makes the situation really inconsistent... (e.g. the behaviour for decimal is different)


Even without thinking about backward compat, changing this behaviour is not trivial: there are special cases made to handle optional properties that conflict with this.
(simply removing the code linked above is not enough to handle properties inferred as string but with a null or empty string value at runtime. Without also changing https://github.com/fsprojects/FSharp.Data/blob/fd0fbccdbca7c697c8a30012754f77225b045ed1/src/Json/JsonRuntime.fs such properties would mistakenly throw for missing values...)

mlaily avatar Jan 08 '23 00:01 mlaily