Skip to content

Commit e3af602

Browse files
authored
fix: fix option serialization (#27)
1 parent 3161543 commit e3af602

20 files changed

Lines changed: 252 additions & 139 deletions

.github/workflows/on-push-branch.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717
- name: Setup .NET Core
1818
uses: actions/setup-dotnet@v4.2.0
1919
with:
20-
dotnet-version: 9.0.100
20+
dotnet-version: 9.0.203
2121

2222
- name: Build & Test
2323
run: make test config=Release

.github/workflows/on-push-tag.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424
- name: Setup .NET Core
2525
uses: actions/setup-dotnet@v4.2.0
2626
with:
27-
dotnet-version: 9.0.100
27+
dotnet-version: 9.0.203
2828

2929
- name: Extract Version Suffix
3030
run: |

.github/workflows/on-release-published.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
- name: Setup .NET Core
1414
uses: actions/setup-dotnet@v4.2.0
1515
with:
16-
dotnet-version: 9.0.100
16+
dotnet-version: 9.0.203
1717

1818
- name: Download Release artifacts
1919
uses: robinraju/release-downloader@v1.11

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,6 @@ test:
1010

1111
nuget:
1212
dotnet pack -c $(config) -p:Version=$(version) -o .out
13+
14+
upgrade:
15+
dotnet restore --force-evaluate

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ Repository origins are:
2121

2222
## Supported platforms
2323

24-
This project targets `netstandard2.1` ([compatible runtimes](https://learn.microsoft.com/en-us/dotnet/standard/net-standard?tabs=net-standard-2-1#select-net-standard-version)).
24+
This project targets `.net 8`, `.net 9` and `netstandard2.1` ([compatible runtimes](https://learn.microsoft.com/en-us/dotnet/standard/net-standard?tabs=net-standard-2-1#select-net-standard-version))
25+
26+
:warning: NRT support starts with `.net sdk 9.0.200`. F# compiler in .net sdk 9.0.10x does not set correctly nullable attributes on F# types. NRT are not supported on `netstandard2.1`.
2527

2628
## Contributing
2729
* If you have a question about the library, then create an [issue][issues] with the `question` label.
@@ -89,7 +91,9 @@ NRT are serialized as:
8991
* `null` if `Null`
9092
* `object` if `NonNull` object
9193

92-
:warning: As of now, NRT can't be considered `null` when deserializing if value is missing.
94+
On deserialization, missing value is mapped to `null`.
95+
96+
:warning: NRT support starts with .net sdk 9.0.200. F# compiler in .net sdk 9.0.10x does not set correctly nullable attributes on F# types.
9397

9498
# License
9599
The contents of this library are made available under the [Apache License, Version 2.0][license].

global.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"sdk": {
3+
"version": "9.0.203"
4+
}
5+
}

src/FSharp.MongoDB.Bson/FSharp.MongoDB.Bson.fsproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>net9.0;netstandard2.1</TargetFrameworks>
4+
<TargetFrameworks>net9.0;net8.0;netstandard2.1</TargetFrameworks>
55
<GenerateDocumentationFile>true</GenerateDocumentationFile>
66
<Nullable>enable</Nullable>
77
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
@@ -26,7 +26,7 @@
2626
</ItemGroup>
2727

2828
<ItemGroup>
29-
<PackageReference Include="MongoDB.Bson" Version="3.1.0" />
29+
<PackageReference Include="MongoDB.Bson" Version="3.4.0" />
3030
</ItemGroup>
3131

3232
<PropertyGroup>

src/FSharp.MongoDB.Bson/Serialization/Conventions/FSharpRecordConvention.fs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ type FSharpRecordConvention() =
3131
match classMap.ClassType with
3232
| IsRecord typ ->
3333
let fields = FSharpType.GetRecordFields(typ, bindingFlags)
34-
let names = fields |> Array.map (fun x -> x.Name)
34+
let names = fields |> Array.map _.Name
3535

3636
// Map the constructor of the record type.
3737
let ctor = FSharpValue.PreComputeRecordConstructorInfo(typ, bindingFlags)
3838
classMap.MapConstructor(ctor, names) |> ignore
3939

4040
// Map each field of the record type.
41-
fields |> Array.iter (classMap.MapMember >> ignore)
41+
fields |> Array.iter (mapMemberNullable classMap)
4242
| _ -> ()

src/FSharp.MongoDB.Bson/Serialization/Conventions/UnionCaseConvention.fs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ type UnionCaseConvention() =
6565

6666
let mapUnionCase (classMap:BsonClassMap) (unionCase:UnionCaseInfo) =
6767
let fields = unionCase.GetFields()
68-
let names = fields |> Array.map (fun x -> x.Name)
68+
let names = fields |> Array.map _.Name
6969

7070
classMap.SetDiscriminator unionCase.Name
7171
classMap.SetDiscriminatorIsRequired true
@@ -76,7 +76,7 @@ type UnionCaseConvention() =
7676
classMap.MapCreator(del, names) |> ignore
7777

7878
// Map each field of the union case.
79-
fields |> Array.iter (classMap.MapMember >> ignore)
79+
fields |> Array.iter (mapMemberNullable classMap)
8080

8181
interface IClassMapConvention with
8282
member _.Apply classMap =

src/FSharp.MongoDB.Bson/Serialization/FSharpTypeHelpers.fs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ module private Helpers =
2323

2424
let bindingFlags = BindingFlags.Public ||| BindingFlags.NonPublic
2525

26+
#if !NETSTANDARD2_1
27+
let nrtContext = NullabilityInfoContext()
28+
#endif
29+
2630
/// <summary>
2731
/// Returns <c>Some typ</c> when <c>pred typ</c> returns true, and <c>None</c> when
2832
/// <c>pred typ</c> returns false.
@@ -90,3 +94,16 @@ module private Helpers =
9094
/// Creates a generic type <c>'T</c> using the generic arguments of <c>typ</c>.
9195
/// </summary>
9296
let mkGenericUsingDef<'T> (typ:System.Type) = typ.GetGenericArguments() |> mkGeneric<'T>
97+
98+
/// <summary>
99+
/// Maps a member of a <c>BsonClassMap</c> to a nullable value if possible.
100+
/// </summary>
101+
let mapMemberNullable (classMap: BsonClassMap) (propertyInfo: PropertyInfo) =
102+
let memberMap = classMap.MapMember(propertyInfo)
103+
#if !NETSTANDARD2_1
104+
let nrtInfo = nrtContext.Create(propertyInfo)
105+
if nrtInfo.ReadState = NullabilityState.Nullable then
106+
memberMap.SetDefaultValue(objnull) |> ignore
107+
#else
108+
memberMap.SetDefaultValue(objnull) |> ignore
109+
#endif

0 commit comments

Comments
 (0)