Some fixes to nullability (#365)

This commit is contained in:
Patrick Stevens
2025-04-20 17:26:45 +01:00
committed by GitHub
parent 682b12fdb2
commit 8d275f0047
14 changed files with 1697 additions and 65 deletions

View File

@@ -11,6 +11,10 @@
<ItemGroup>
<None Include="myriad.toml"/>
<Compile Include="JsonParseNullness.fs" />
<Compile Include="GeneratedJsonParseNullness.fs">
<MyriadFile>JsonParseNullness.fs</MyriadFile>
</Compile>
<Compile Include="AssemblyInfo.fs" />
<Compile Include="RecordFile.fs"/>
<Compile Include="GeneratedRecord.fs">

File diff suppressed because it is too large Load Diff

View File

@@ -71,6 +71,7 @@ module JsonRecordType =
)
| v -> v)
.AsArray ()
|> Seq.cast<System.Text.Json.Nodes.JsonNode>
|> Seq.map (fun elt -> elt.AsValue().GetValue<System.Int32> ())
|> Array.ofSeq
@@ -84,6 +85,7 @@ module JsonRecordType =
)
| v -> v)
.AsArray ()
|> Seq.cast<System.Text.Json.Nodes.JsonNode>
|> Seq.map (fun elt -> elt.AsValue().GetValue<System.String> ())
|> Array.ofSeq
@@ -109,6 +111,7 @@ module JsonRecordType =
)
| v -> v)
.AsArray ()
|> Seq.cast<System.Text.Json.Nodes.JsonNode>
|> Seq.map (fun elt -> elt.AsValue().GetValue<System.Int32> ())
|> List.ofSeq
@@ -201,7 +204,15 @@ module ToGetExtensionMethodJsonParseExtension =
/// Parse from a JSON node.
static member jsonParse (node : System.Text.Json.Nodes.JsonNode) : ToGetExtensionMethod =
let arg_20 = System.Numerics.BigInteger.Parse (node.["whiskey"].ToJsonString ())
let arg_20 =
let v = node.["whiskey"]
System.Numerics.BigInteger.Parse (
(match v with
| null -> raise (System.ArgumentNullException ())
| v -> v)
.ToJsonString ()
)
let arg_19 =
(match node.["victor"] with

View File

@@ -0,0 +1,53 @@
//------------------------------------------------------------------------------
// This code was generated by myriad.
// Changes to this file will be lost when the code is regenerated.
//------------------------------------------------------------------------------
namespace ConsumePlugin
/// Module containing JSON parsing methods for the InnerStruct type
[<RequireQualifiedAccess ; CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module InnerStruct =
/// Parse from a JSON node.
let jsonParse (node : System.Text.Json.Nodes.JsonNode) : InnerStruct =
let arg_0 =
(match node.["a"] with
| null ->
raise (
System.Collections.Generic.KeyNotFoundException (
sprintf "Required key '%s' not found on JSON object" ("a")
)
)
| v -> v)
.AsValue()
.GetValue<System.Int32> ()
{
A = arg_0
}
namespace ConsumePlugin
/// Module containing JSON parsing methods for the ArrayOfInnerStruct type
[<RequireQualifiedAccess ; CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module ArrayOfInnerStruct =
/// Parse from a JSON node.
let jsonParse (node : System.Text.Json.Nodes.JsonNode) : ArrayOfInnerStruct =
let arg_0 =
(match node.["b"] with
| null ->
raise (
System.Collections.Generic.KeyNotFoundException (
sprintf "Required key '%s' not found on JSON object" ("b")
)
)
| v -> v)
.AsArray ()
|> Seq.cast<System.Text.Json.Nodes.JsonNode>
|> Seq.map (fun elt -> InnerStruct.jsonParse elt)
|> Array.ofSeq
{
B = arg_0
}

View File

@@ -60,6 +60,7 @@ module GymOpeningHours =
)
| v -> v)
.AsArray ()
|> Seq.cast<System.Text.Json.Nodes.JsonNode>
|> Seq.map (fun elt -> elt.AsValue().GetValue<System.String> ())
|> List.ofSeq
@@ -1038,6 +1039,7 @@ module Sessions =
)
| v -> v)
.AsArray ()
|> Seq.cast<System.Text.Json.Nodes.JsonNode>
|> Seq.map (fun elt -> Visit.jsonParse elt)
|> List.ofSeq

View File

@@ -48,7 +48,16 @@ module PureGymApi =
System.Text.Json.Nodes.JsonNode.ParseAsync (responseStream, cancellationToken = ct)
|> Async.AwaitTask
return jsonNode.AsArray () |> Seq.map (fun elt -> Gym.jsonParse elt) |> List.ofSeq
let jsonNode =
match jsonNode with
| null -> raise (System.ArgumentNullException ())
| v -> v
return
jsonNode.AsArray ()
|> Seq.cast<System.Text.Json.Nodes.JsonNode>
|> Seq.map (fun elt -> Gym.jsonParse elt)
|> List.ofSeq
}
|> (fun a -> Async.StartAsTask (a, ?cancellationToken = ct))
@@ -82,6 +91,11 @@ module PureGymApi =
System.Text.Json.Nodes.JsonNode.ParseAsync (responseStream, cancellationToken = ct)
|> Async.AwaitTask
let jsonNode =
match jsonNode with
| null -> raise (System.ArgumentNullException ())
| v -> v
return GymAttendance.jsonParse jsonNode
}
|> (fun a -> Async.StartAsTask (a, ?cancellationToken = ct))
@@ -116,6 +130,11 @@ module PureGymApi =
System.Text.Json.Nodes.JsonNode.ParseAsync (responseStream, cancellationToken = ct)
|> Async.AwaitTask
let jsonNode =
match jsonNode with
| null -> raise (System.ArgumentNullException ())
| v -> v
return GymAttendance.jsonParse jsonNode
}
|> (fun a -> Async.StartAsTask (a, ?cancellationToken = ct))
@@ -146,6 +165,11 @@ module PureGymApi =
System.Text.Json.Nodes.JsonNode.ParseAsync (responseStream, cancellationToken = ct)
|> Async.AwaitTask
let jsonNode =
match jsonNode with
| null -> raise (System.ArgumentNullException ())
| v -> v
return Member.jsonParse jsonNode
}
|> (fun a -> Async.StartAsTask (a, ?cancellationToken = ct))
@@ -179,6 +203,11 @@ module PureGymApi =
System.Text.Json.Nodes.JsonNode.ParseAsync (responseStream, cancellationToken = ct)
|> Async.AwaitTask
let jsonNode =
match jsonNode with
| null -> raise (System.ArgumentNullException ())
| v -> v
return Gym.jsonParse jsonNode
}
|> (fun a -> Async.StartAsTask (a, ?cancellationToken = ct))
@@ -209,6 +238,11 @@ module PureGymApi =
System.Text.Json.Nodes.JsonNode.ParseAsync (responseStream, cancellationToken = ct)
|> Async.AwaitTask
let jsonNode =
match jsonNode with
| null -> raise (System.ArgumentNullException ())
| v -> v
return MemberActivityDto.jsonParse jsonNode
}
|> (fun a -> Async.StartAsTask (a, ?cancellationToken = ct))
@@ -239,6 +273,11 @@ module PureGymApi =
System.Text.Json.Nodes.JsonNode.ParseAsync (responseStream, cancellationToken = ct)
|> Async.AwaitTask
let jsonNode =
match jsonNode with
| null -> raise (System.ArgumentNullException ())
| v -> v
return UriThing.jsonParse jsonNode
}
|> (fun a -> Async.StartAsTask (a, ?cancellationToken = ct))
@@ -294,6 +333,11 @@ module PureGymApi =
System.Text.Json.Nodes.JsonNode.ParseAsync (responseStream, cancellationToken = ct)
|> Async.AwaitTask
let jsonNode =
match jsonNode with
| null -> raise (System.ArgumentNullException ())
| v -> v
return
match jsonNode with
| null -> None
@@ -346,6 +390,11 @@ module PureGymApi =
System.Text.Json.Nodes.JsonNode.ParseAsync (responseStream, cancellationToken = ct)
|> Async.AwaitTask
let jsonNode =
match jsonNode with
| null -> raise (System.ArgumentNullException ())
| v -> v
return Sessions.jsonParse jsonNode
}
|> (fun a -> Async.StartAsTask (a, ?cancellationToken = ct))
@@ -387,6 +436,11 @@ module PureGymApi =
System.Text.Json.Nodes.JsonNode.ParseAsync (responseStream, cancellationToken = ct)
|> Async.AwaitTask
let jsonNode =
match jsonNode with
| null -> raise (System.ArgumentNullException ())
| v -> v
return Sessions.jsonParse jsonNode
}
|> (fun a -> Async.StartAsTask (a, ?cancellationToken = ct))
@@ -878,6 +932,11 @@ module PureGymApi =
System.Text.Json.Nodes.JsonNode.ParseAsync (responseStream, cancellationToken = ct)
|> Async.AwaitTask
let jsonNode =
match jsonNode with
| null -> raise (System.ArgumentNullException ())
| v -> v
return
new RestEase.Response<_> (
responseString,
@@ -914,6 +973,11 @@ module PureGymApi =
System.Text.Json.Nodes.JsonNode.ParseAsync (responseStream, cancellationToken = ct)
|> Async.AwaitTask
let jsonNode =
match jsonNode with
| null -> raise (System.ArgumentNullException ())
| v -> v
return
new RestEase.Response<_> (
responseString,
@@ -950,6 +1014,11 @@ module PureGymApi =
System.Text.Json.Nodes.JsonNode.ParseAsync (responseStream, cancellationToken = ct)
|> Async.AwaitTask
let jsonNode =
match jsonNode with
| null -> raise (System.ArgumentNullException ())
| v -> v
return
new RestEase.Response<_> (
responseString,
@@ -986,6 +1055,11 @@ module PureGymApi =
System.Text.Json.Nodes.JsonNode.ParseAsync (responseStream, cancellationToken = ct)
|> Async.AwaitTask
let jsonNode =
match jsonNode with
| null -> raise (System.ArgumentNullException ())
| v -> v
return
new RestEase.Response<_> (
responseString,

View File

@@ -408,6 +408,7 @@ module InnerTypeWithBothJsonParseExtension =
let value =
(kvp.Value).AsArray ()
|> Seq.cast<System.Text.Json.Nodes.JsonNode>
|> Seq.map (fun elt -> elt.AsValue().GetValue<System.Char> ())
|> List.ofSeq
@@ -676,6 +677,7 @@ module JsonRecordTypeWithBothJsonParseExtension =
)
| v -> v)
.AsArray ()
|> Seq.cast<System.Text.Json.Nodes.JsonNode>
|> Seq.map (fun elt -> elt.AsValue().GetValue<System.Int32> ())
|> Array.ofSeq
@@ -689,6 +691,7 @@ module JsonRecordTypeWithBothJsonParseExtension =
)
| v -> v)
.AsArray ()
|> Seq.cast<System.Text.Json.Nodes.JsonNode>
|> Seq.map (fun elt -> elt.AsValue().GetValue<System.String> ())
|> Array.ofSeq
@@ -714,6 +717,7 @@ module JsonRecordTypeWithBothJsonParseExtension =
)
| v -> v)
.AsArray ()
|> Seq.cast<System.Text.Json.Nodes.JsonNode>
|> Seq.map (fun elt -> elt.AsValue().GetValue<System.Int32> ())
|> List.ofSeq

View File

@@ -94,6 +94,7 @@ module JwtVaultAuthResponse =
)
| v -> v)
.AsArray ()
|> Seq.cast<System.Text.Json.Nodes.JsonNode>
|> Seq.map (fun elt -> elt.AsValue().GetValue<System.String> ())
|> List.ofSeq
@@ -107,6 +108,7 @@ module JwtVaultAuthResponse =
)
| v -> v)
.AsArray ()
|> Seq.cast<System.Text.Json.Nodes.JsonNode>
|> Seq.map (fun elt -> elt.AsValue().GetValue<System.String> ())
|> List.ofSeq
@@ -120,6 +122,7 @@ module JwtVaultAuthResponse =
)
| v -> v)
.AsArray ()
|> Seq.cast<System.Text.Json.Nodes.JsonNode>
|> Seq.map (fun elt -> elt.AsValue().GetValue<System.String> ())
|> List.ofSeq
@@ -496,6 +499,11 @@ module VaultClient =
System.Text.Json.Nodes.JsonNode.ParseAsync (responseStream, cancellationToken = ct)
|> Async.AwaitTask
let jsonNode =
match jsonNode with
| null -> raise (System.ArgumentNullException ())
| v -> v
return JwtSecretResponse.jsonParse jsonNode
}
|> (fun a -> Async.StartAsTask (a, ?cancellationToken = ct))
@@ -532,6 +540,11 @@ module VaultClient =
System.Text.Json.Nodes.JsonNode.ParseAsync (responseStream, cancellationToken = ct)
|> Async.AwaitTask
let jsonNode =
match jsonNode with
| null -> raise (System.ArgumentNullException ())
| v -> v
return JwtVaultResponse.jsonParse jsonNode
}
|> (fun a -> Async.StartAsTask (a, ?cancellationToken = ct))
@@ -590,6 +603,11 @@ module VaultClientNonExtensionMethod =
System.Text.Json.Nodes.JsonNode.ParseAsync (responseStream, cancellationToken = ct)
|> Async.AwaitTask
let jsonNode =
match jsonNode with
| null -> raise (System.ArgumentNullException ())
| v -> v
return JwtSecretResponse.jsonParse jsonNode
}
|> (fun a -> Async.StartAsTask (a, ?cancellationToken = ct))
@@ -626,6 +644,11 @@ module VaultClientNonExtensionMethod =
System.Text.Json.Nodes.JsonNode.ParseAsync (responseStream, cancellationToken = ct)
|> Async.AwaitTask
let jsonNode =
match jsonNode with
| null -> raise (System.ArgumentNullException ())
| v -> v
return JwtVaultResponse.jsonParse jsonNode
}
|> (fun a -> Async.StartAsTask (a, ?cancellationToken = ct))
@@ -687,6 +710,11 @@ module VaultClientExtensionMethodHttpClientExtension =
System.Text.Json.Nodes.JsonNode.ParseAsync (responseStream, cancellationToken = ct)
|> Async.AwaitTask
let jsonNode =
match jsonNode with
| null -> raise (System.ArgumentNullException ())
| v -> v
return JwtSecretResponse.jsonParse jsonNode
}
|> (fun a -> Async.StartAsTask (a, ?cancellationToken = ct))
@@ -723,6 +751,11 @@ module VaultClientExtensionMethodHttpClientExtension =
System.Text.Json.Nodes.JsonNode.ParseAsync (responseStream, cancellationToken = ct)
|> Async.AwaitTask
let jsonNode =
match jsonNode with
| null -> raise (System.ArgumentNullException ())
| v -> v
return JwtVaultResponse.jsonParse jsonNode
}
|> (fun a -> Async.StartAsTask (a, ?cancellationToken = ct))

View File

@@ -0,0 +1,13 @@
namespace ConsumePlugin
[<WoofWare.Myriad.Plugins.JsonParse>]
type InnerStruct =
{
A : int
}
[<WoofWare.Myriad.Plugins.JsonParse>]
type ArrayOfInnerStruct =
{
B : InnerStruct array
}

View File

@@ -0,0 +1,23 @@
namespace WoofWare.Myriad.Plugins.Test
open System.Text.Json.Nodes
open FsUnitTyped
open NUnit.Framework
open ConsumePlugin
[<TestFixture>]
module TestJsonNullability =
[<Test>]
let ``Can consume JsonParseNullness`` () =
let options = JsonNodeOptions (PropertyNameCaseInsensitive = true)
"""{
"b": null
}"""
|> fun s -> JsonNode.Parse (s, options)
|> ArrayOfInnerStruct.jsonParse
|> shouldEqual
{
B = null
}

View File

@@ -18,6 +18,7 @@
<Compile Include="TestJsonParse\TestJsonParse.fs" />
<Compile Include="TestJsonParse\TestPureGymJson.fs" />
<Compile Include="TestJsonParse\TestExtensionMethod.fs" />
<Compile Include="TestJsonParse\TestJsonNullability.fs" />
<Compile Include="TestHttpClient\TestPureGymRestApi.fs" />
<Compile Include="TestHttpClient\TestPathParam.fs" />
<Compile Include="TestHttpClient\TestReturnTypes.fs" />

View File

@@ -356,3 +356,19 @@ module internal AstHelper =
}
)
| _ -> failwithf "Failed to get record elements for type that was: %+A" repr
let raiseIfNull (variable : Ident) : SynExpr =
SynExpr.createMatch
(SynExpr.createIdent' variable)
[
SynMatchClause.create
SynPat.createNull
(SynExpr.applyFunction
(SynExpr.createIdent "raise")
(SynExpr.paren (
SynExpr.applyFunction
(SynExpr.createLongIdent [ "System" ; "ArgumentNullException" ])
(SynExpr.CreateConst ())
)))
SynMatchClause.create (SynPat.named "v") (SynExpr.createIdent "v")
]

View File

@@ -1,6 +1,5 @@
namespace WoofWare.Myriad.Plugins
open System.IO
open System.Net.Http
open Fantomas.FCS.Syntax
open WoofWare.Whippet.Fantomas
@@ -14,17 +13,6 @@ type internal HttpClientGeneratorOutputSpec =
module internal HttpClientGenerator =
open Fantomas.FCS.Text.Range
let outputFile = FileInfo "/tmp/output.txt"
// do
// use _ = File.Create outputFile.FullName
// ()
let log (line : string) =
// use w = outputFile.AppendText ()
// w.WriteLine line
()
[<RequireQualifiedAccess>]
type PathSpec =
| Verbatim of string
@@ -568,6 +556,9 @@ module internal HttpClientGenerator =
)
)
let jsonNodeNotNull =
Let ("jsonNode", AstHelper.raiseIfNull (Ident.create "jsonNode"))
let setVariableHeaders =
variableHeaders
|> List.map (fun (headerName, callToGetValue) ->
@@ -642,6 +633,7 @@ module internal HttpClientGenerator =
yield responseString
yield responseStream
yield jsonNode
yield jsonNodeNotNull
| String -> yield responseString
| Stream -> yield responseStream
| UnitType ->
@@ -650,6 +642,7 @@ module internal HttpClientGenerator =
| _ ->
yield responseStream
yield jsonNode
yield jsonNodeNotNull
]
|> SynExpr.createCompExpr "async" returnExpr
|> SynExpr.startAsTask cancellationTokenArg
@@ -914,10 +907,6 @@ module internal HttpClientGenerator =
"Create a REST client. The input functions will be re-evaluated on every HTTP request to obtain the required values for the corresponding header properties."
|> PreXmlDoc.create
let functionName = Ident.create "client"
let pattern = SynLongIdent.createS "make"
let returnInfo = SynType.createLongIdent interfaceType.Name
let nameWithoutLeadingI =

View File

@@ -78,6 +78,7 @@ module internal JsonParseGenerator =
/// collectionType is e.g. "List"; we'll be calling `ofSeq` on it.
/// body is the body of a lambda which takes a parameter `elt`.
/// {assertNotNull node}.AsArray()
/// |> Seq.cast<JsonNode>
/// |> Seq.map (fun elt -> {body})
/// |> {collectionType}.ofSeq
let asArrayMapped
@@ -91,6 +92,13 @@ module internal JsonParseGenerator =
| None -> node
| Some propertyName -> assertNotNull propertyName node
|> SynExpr.callMethod "AsArray"
|> SynExpr.pipeThroughFunction (
SynExpr.createLongIdent [ "Seq" ; "cast" ]
|> SynExpr.typeApp
[
SynType.createLongIdent' [ "System" ; "Text" ; "Json" ; "Nodes" ; "JsonNode" ]
]
)
|> SynExpr.pipeThroughFunction (
SynExpr.applyFunction (SynExpr.createLongIdent [ "Seq" ; "map" ]) (SynExpr.createLambda "elt" body)
)
@@ -273,10 +281,12 @@ module internal JsonParseGenerator =
)
|> SynExpr.pipeThroughFunction (SynExpr.createLongIdent [ "Map" ; "ofSeq" ])
| BigInt ->
node
AstHelper.raiseIfNull (Ident.create "v")
|> SynExpr.paren
|> SynExpr.callMethod "ToJsonString"
|> SynExpr.paren
|> SynExpr.applyFunction (SynExpr.createLongIdent [ "System" ; "Numerics" ; "BigInteger" ; "Parse" ])
|> SynExpr.createLet [ SynBinding.basic [ Ident.create "v" ] [] node ]
| Measure (_measure, primType) ->
parseNumberType options propertyName node primType
|> SynExpr.pipeThroughFunction (Measure.getLanguagePrimitivesMeasure primType)