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

Fix generated code of the json provider with PreferDictionaries when values are arrays #1476

Merged
merged 2 commits into from
Mar 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* Move common runtime utilities out of `FSharp.Data.Http` and into a new `FSharp.Data.Runtime.Utilities` assembly.
* Add `aria-label` to the list of html attributes used to infer names of types provided by the HtmlProvider.
* Enable TLS 1.2 when requesting http(s) samples from the type providers.
* Fix generated code of the json provider with `PreferDictionaries` when values are arrays.

### 6.0.1-beta001 - Aug 18 2022

Expand Down
41 changes: 35 additions & 6 deletions src/FSharp.Data.DesignTime/Json/JsonGenerator.fs
Original file line number Diff line number Diff line change
Expand Up @@ -399,15 +399,42 @@ module JsonTypeBuilder =
let inferedKeyValueType =
let aggr = List.fold (StructuralInference.subtypeInfered false) InferedType.Top

let dropRecordName infType =
let dropRecordsNames infType =

let dropRecordName infType =
match infType with
| InferedType.Record (_, fields, opt) -> InferedType.Record(None, fields, opt)
| _ -> infType

let dropTagName tag =
match tag with
| InferedTypeTag.Record (Some _) -> InferedTypeTag.Record None
| _ -> tag

let infType = dropRecordName infType

match infType with
| InferedType.Record (_, fields, opt) -> InferedType.Record(None, fields, opt)
| InferedType.Collection (order, types) ->
// Records in collections have the parent property as name.
// We drop it too so they can be merged into a unified type.
let order = order |> List.map dropTagName

let types =
types
|> Map.toSeq
|> Seq.map (fun (tag, (multiplicity, typ)) ->
let tag = dropTagName tag
let typ = dropRecordName typ
tag, (multiplicity, typ))
|> Map.ofSeq

InferedType.Collection(order, types)
| _ -> infType

if not ctx.PreferDictionaries then
None
else
let infType =
let infKeyType =
[ for prop in props ->
StructuralInference.getInferedTypeFromString
ctx.UnitsOfMeasureProvider
Expand All @@ -417,14 +444,16 @@ module JsonTypeBuilder =
None ]
|> aggr

match infType with
match infKeyType with
| InferedType.Primitive (typ = typ) when typ <> typeof<string> ->
let inferValueType =
([ for prop in props -> prop.Type |> dropRecordName ]
([ for prop in props -> prop.Type |> dropRecordsNames ]
|> aggr)
// Optional properties in the initial record should translate
// to simply missing values in the dictionary, not an optional type.
.DropOptionality()

(infType, inferValueType) |> Some
Some(infKeyType, inferValueType)
| _ -> None

match inferedKeyValueType with
Expand Down
10 changes: 9 additions & 1 deletion tests/FSharp.Data.DesignTime.Tests/SignatureTestCases.config
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,14 @@ Json,DictionaryInference.json,false,,,true,true,BackwardCompatible
Json,DictionaryInference.json,false,,,true,true,ValuesOnly
Json,DictionaryInference.json,false,,,true,true,ValuesAndInlineSchemasHints
Json,DictionaryInference.json,false,,,true,true,ValuesAndInlineSchemasOverrides
Json,DictionaryInference-arrays.json,false,,,true,false,BackwardCompatible
Json,DictionaryInference-arrays.json,false,,,true,false,ValuesOnly
Json,DictionaryInference-arrays.json,false,,,true,false,ValuesAndInlineSchemasHints
Json,DictionaryInference-arrays.json,false,,,true,false,ValuesAndInlineSchemasOverrides
Json,DictionaryInference-arrays.json,false,,,true,true,BackwardCompatible
Json,DictionaryInference-arrays.json,false,,,true,true,ValuesOnly
Json,DictionaryInference-arrays.json,false,,,true,true,ValuesAndInlineSchemasHints
Json,DictionaryInference-arrays.json,false,,,true,true,ValuesAndInlineSchemasOverrides
Html,MarketDepth.htm,false,false,
Html,MarketDepth.htm,true,false,
Html,SimpleHtmlTablesWithTr.html,false,false,
Expand All @@ -207,4 +215,4 @@ Html,doctor_who3.html,false,false,
Html,SimpleHtmlLists.html,false,false,
Html,EmptyDefinitionLists.html,false,false,
Html,zoopla.html,false,false,
Html,zoopla2.html,false,false,
Html,zoopla2.html,false,false,
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
class JsonProvider : obj
static member AsyncGetSample: () -> JsonProvider+Root async
let f = new Func<_,_>(fun (t:TextReader) -> JsonDocument.Create(t))
TextRuntime.AsyncMap((IO.asyncReadTextAtRuntimeWithDesignTimeRules "<RESOLUTION_FOLDER>" "" "JSON" "" "DictionaryInference-arrays.json"), f)

static member AsyncLoad: uri:string -> JsonProvider+Root async
let f = new Func<_,_>(fun (t:TextReader) -> JsonDocument.Create(t))
TextRuntime.AsyncMap((IO.asyncReadTextAtRuntime false "<RESOLUTION_FOLDER>" "" "JSON" "" uri), f)

static member GetSample: () -> JsonProvider+Root
JsonDocument.Create(FSharpAsync.RunSynchronously((IO.asyncReadTextAtRuntimeWithDesignTimeRules "<RESOLUTION_FOLDER>" "" "JSON" "" "DictionaryInference-arrays.json")))

static member Load: stream:System.IO.Stream -> JsonProvider+Root
JsonDocument.Create(((new StreamReader(stream)) :> TextReader))

static member Load: reader:System.IO.TextReader -> JsonProvider+Root
JsonDocument.Create(reader)

static member Load: uri:string -> JsonProvider+Root
JsonDocument.Create(FSharpAsync.RunSynchronously((IO.asyncReadTextAtRuntime false "<RESOLUTION_FOLDER>" "" "JSON" "" uri)))

static member Load: value:JsonValue -> JsonProvider+Root
JsonDocument.Create(value, "")

static member Parse: text:string -> JsonProvider+Root
JsonDocument.Create(((new StringReader(text)) :> TextReader))

static member ParseList: text:string -> JsonProvider+JsonProvider+Root[]
JsonDocument.CreateList(((new StringReader(text)) :> TextReader))


class JsonProvider+Root : FDR.BaseTypes.IJsonDocument
new : mappings:JsonProvider+Mappings -> JsonProvider+Root
JsonRuntime.CreateRecord([| ("Mappings",
(mappings :> obj)) |], "")

new : jsonValue:JsonValue -> JsonProvider+Root
JsonDocument.Create(jsonValue, "")

member Mappings: JsonProvider+Mappings with get
JsonRuntime.GetPropertyPacked(this, "Mappings")


class JsonProvider+Mappings : FDR.BaseTypes.IJsonDocument
new : 123:JsonProvider+JsonProvider+123[] -> 456:JsonProvider+JsonProvider+123[] -> 789:JsonProvider+JsonProvider+789[] -> JsonProvider+Mappings
JsonRuntime.CreateRecord([| ("123",
(123 :> obj))
("456",
(456 :> obj))
("789",
(789 :> obj)) |], "")

new : jsonValue:JsonValue -> JsonProvider+Mappings
JsonDocument.Create(jsonValue, "")

member 123: JsonProvider+JsonProvider+123[] with get
JsonRuntime.ConvertArray(JsonRuntime.GetPropertyPackedOrNull(this, "123"), new Func<_,_>(id)))

member 456: JsonProvider+JsonProvider+123[] with get
JsonRuntime.ConvertArray(JsonRuntime.GetPropertyPackedOrNull(this, "456"), new Func<_,_>(id)))

member 789: JsonProvider+JsonProvider+789[] with get
JsonRuntime.ConvertArray(JsonRuntime.GetPropertyPackedOrNull(this, "789"), new Func<_,_>(id)))


class JsonProvider+123 : FDR.BaseTypes.IJsonDocument
new : groupId:int -> canDelete:bool -> JsonProvider+123
JsonRuntime.CreateRecord([| ("GroupId",
(groupId :> obj))
("CanDelete",
(canDelete :> obj)) |], "")

new : jsonValue:JsonValue -> JsonProvider+123
JsonDocument.Create(jsonValue, "")

member CanDelete: bool with get
let value = JsonRuntime.TryGetPropertyUnpackedWithPath(this, "CanDelete")
JsonRuntime.GetNonOptionalValue(value.Path, JsonRuntime.ConvertBoolean(value.JsonOpt), value.JsonOpt)

member GroupId: int with get
let value = JsonRuntime.TryGetPropertyUnpackedWithPath(this, "GroupId")
JsonRuntime.GetNonOptionalValue(value.Path, JsonRuntime.ConvertInteger("", value.JsonOpt), value.JsonOpt)


class JsonProvider+789 : FDR.BaseTypes.IJsonDocument
new : groupId:int -> canDelete:bool -> errorMessage:string option -> JsonProvider+789
JsonRuntime.CreateRecord([| ("GroupId",
(groupId :> obj))
("CanDelete",
(canDelete :> obj))
("ErrorMessage",
(errorMessage :> obj)) |], "")

new : jsonValue:JsonValue -> JsonProvider+789
JsonDocument.Create(jsonValue, "")

member CanDelete: bool with get
let value = JsonRuntime.TryGetPropertyUnpackedWithPath(this, "CanDelete")
JsonRuntime.GetNonOptionalValue(value.Path, JsonRuntime.ConvertBoolean(value.JsonOpt), value.JsonOpt)

member ErrorMessage: string option with get
JsonRuntime.ConvertString("", JsonRuntime.TryGetPropertyUnpacked(this, "ErrorMessage"))

member GroupId: int with get
let value = JsonRuntime.TryGetPropertyUnpackedWithPath(this, "GroupId")
JsonRuntime.GetNonOptionalValue(value.Path, JsonRuntime.ConvertInteger("", value.JsonOpt), value.JsonOpt)


Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
class JsonProvider : obj
static member AsyncGetSample: () -> JsonProvider+Root async
let f = new Func<_,_>(fun (t:TextReader) -> JsonDocument.Create(t))
TextRuntime.AsyncMap((IO.asyncReadTextAtRuntimeWithDesignTimeRules "<RESOLUTION_FOLDER>" "" "JSON" "" "DictionaryInference-arrays.json"), f)

static member AsyncLoad: uri:string -> JsonProvider+Root async
let f = new Func<_,_>(fun (t:TextReader) -> JsonDocument.Create(t))
TextRuntime.AsyncMap((IO.asyncReadTextAtRuntime false "<RESOLUTION_FOLDER>" "" "JSON" "" uri), f)

static member GetSample: () -> JsonProvider+Root
JsonDocument.Create(FSharpAsync.RunSynchronously((IO.asyncReadTextAtRuntimeWithDesignTimeRules "<RESOLUTION_FOLDER>" "" "JSON" "" "DictionaryInference-arrays.json")))

static member Load: stream:System.IO.Stream -> JsonProvider+Root
JsonDocument.Create(((new StreamReader(stream)) :> TextReader))

static member Load: reader:System.IO.TextReader -> JsonProvider+Root
JsonDocument.Create(reader)

static member Load: uri:string -> JsonProvider+Root
JsonDocument.Create(FSharpAsync.RunSynchronously((IO.asyncReadTextAtRuntime false "<RESOLUTION_FOLDER>" "" "JSON" "" uri)))

static member Load: value:JsonValue -> JsonProvider+Root
JsonDocument.Create(value, "")

static member Parse: text:string -> JsonProvider+Root
JsonDocument.Create(((new StringReader(text)) :> TextReader))

static member ParseList: text:string -> JsonProvider+JsonProvider+Root[]
JsonDocument.CreateList(((new StringReader(text)) :> TextReader))


class JsonProvider+Root : FDR.BaseTypes.IJsonDocument
new : mappings:JsonProvider+Mappings -> JsonProvider+Root
JsonRuntime.CreateRecord([| ("Mappings",
(mappings :> obj)) |], "")

new : jsonValue:JsonValue -> JsonProvider+Root
JsonDocument.Create(jsonValue, "")

member Mappings: JsonProvider+Mappings with get
JsonRuntime.GetPropertyPacked(this, "Mappings")


class JsonProvider+Mappings : FDR.BaseTypes.IJsonDocument
new : 123:JsonProvider+JsonProvider+123[] -> 456:JsonProvider+JsonProvider+123[] -> 789:JsonProvider+JsonProvider+789[] -> JsonProvider+Mappings
JsonRuntime.CreateRecord([| ("123",
(123 :> obj))
("456",
(456 :> obj))
("789",
(789 :> obj)) |], "")

new : jsonValue:JsonValue -> JsonProvider+Mappings
JsonDocument.Create(jsonValue, "")

member 123: JsonProvider+JsonProvider+123[] with get
JsonRuntime.ConvertArray(JsonRuntime.GetPropertyPackedOrNull(this, "123"), new Func<_,_>(id)))

member 456: JsonProvider+JsonProvider+123[] with get
JsonRuntime.ConvertArray(JsonRuntime.GetPropertyPackedOrNull(this, "456"), new Func<_,_>(id)))

member 789: JsonProvider+JsonProvider+789[] with get
JsonRuntime.ConvertArray(JsonRuntime.GetPropertyPackedOrNull(this, "789"), new Func<_,_>(id)))


class JsonProvider+123 : FDR.BaseTypes.IJsonDocument
new : groupId:int -> canDelete:bool -> JsonProvider+123
JsonRuntime.CreateRecord([| ("GroupId",
(groupId :> obj))
("CanDelete",
(canDelete :> obj)) |], "")

new : jsonValue:JsonValue -> JsonProvider+123
JsonDocument.Create(jsonValue, "")

member CanDelete: bool with get
let value = JsonRuntime.TryGetPropertyUnpackedWithPath(this, "CanDelete")
JsonRuntime.GetNonOptionalValue(value.Path, JsonRuntime.ConvertBoolean(value.JsonOpt), value.JsonOpt)

member GroupId: int with get
let value = JsonRuntime.TryGetPropertyUnpackedWithPath(this, "GroupId")
JsonRuntime.GetNonOptionalValue(value.Path, JsonRuntime.ConvertInteger("", value.JsonOpt), value.JsonOpt)


class JsonProvider+789 : FDR.BaseTypes.IJsonDocument
new : groupId:int -> canDelete:bool -> errorMessage:string option -> JsonProvider+789
JsonRuntime.CreateRecord([| ("GroupId",
(groupId :> obj))
("CanDelete",
(canDelete :> obj))
("ErrorMessage",
(errorMessage :> obj)) |], "")

new : jsonValue:JsonValue -> JsonProvider+789
JsonDocument.Create(jsonValue, "")

member CanDelete: bool with get
let value = JsonRuntime.TryGetPropertyUnpackedWithPath(this, "CanDelete")
JsonRuntime.GetNonOptionalValue(value.Path, JsonRuntime.ConvertBoolean(value.JsonOpt), value.JsonOpt)

member ErrorMessage: string option with get
JsonRuntime.ConvertString("", JsonRuntime.TryGetPropertyUnpacked(this, "ErrorMessage"))

member GroupId: int with get
let value = JsonRuntime.TryGetPropertyUnpackedWithPath(this, "GroupId")
JsonRuntime.GetNonOptionalValue(value.Path, JsonRuntime.ConvertInteger("", value.JsonOpt), value.JsonOpt)


Loading