Reputation: 1455
I'm using the FSharp.Data.TypeProvider but I have an optional property that is not rendered as such.
I have this example JSON:
[
{
"error": {
"message": "Error validating access token: Session has expired on Friday, 24-Jul-20 16:00:00 PDT. The current time is Friday, 24-Jul-20 16:06:14 PDT.",
"type": "OAuthException",
"code": 190,
"error_subcode": 463,
"fbtrace_id": "A0yQG8l8ggauD5PMbYSnEyA"
}
},
{
"error": {
"message": "Error validating access token: Session has expired on Friday, 24-Jul-20 16:00:00 PDT. The current time is Friday, 24-Jul-20 16:06:14 PDT.",
"type": "OAuthException",
"code": 190,
"error_subcode": 463,
"fbtrace_id": "A0yQG8l8ggauD5PMbYSnEyA"
}
},
{
"data": [
{
"id": "17841511906082530"
}
]
}
]
The field error
is read as Optional<Error>
meanwhile the field data
is Datum []
, It should be Optional<Data>
, this error seems to have something to do with the array, if I change the value of data
to another type, like a number, it correctly infers the type.
Maybe is some cache that's wrong? How could I reset the TypeProvider cache?
OR am I doing something else wrong?
This issue is also reported in the github project: https://github.com/fsharp/FSharp.Data/issues/1322
Upvotes: 0
Views: 348
Reputation: 3784
That's expected behavior.
In your JSON string, data
is an array, an empty array indicates None
, thus the field Data
is read as array. That makes sense.
The fact that each element of that array belongs to type Datum
also makes sense because Datum
is the single form of Data
(plural form). Indeed, if you rename data
to dogs
you will see it is read as Dog []
.
Update: I don’t think we can tell the type provider to infer the field data
as Option<Datum[]>
. To check whether it is actually an empty array or not existed, we need to examine the JsonValue
field:
match node.JsonValue with
| JsonValue.Record [|("data", _)|] ->
// yep, we have field data here
| _ ->
// nope, field data is missed
Upvotes: 1