dotnet / fsharp

The F# compiler, F# core library, F# language service, and F# tooling integration for Visual Studio
https://dotnet.microsoft.com/languages/fsharp
MIT License
3.87k stars 779 forks source link

JSON.Net serializer adds duplicate "field@" record fields in FSI (SDK version 6.0.300+) #13546

Closed HoraceGonzalez closed 2 years ago

HoraceGonzalez commented 2 years ago

Json.NET serialization of records adds duplicate fields with @ appended to the name in FSI (SDK versions 6.0.300 and up). For example, serializing this anonymous record:

JsonConvert.SerializeObject {| foo = "bar" |}

produces this result:

{"foo@":"bar","foo":"bar"}

Repro steps

Provide the steps required to reproduce the problem:

  1. Install SDK version 6.0.302
  2. create a new project folder, eg. mkdir temp
  3. add a new global.json` with the following content:
    {
    "sdk": {
      "version": "6.0.302"
    }
    }
  4. create a new temp/test.fsx file with the following content:

    #r "nuget: Newtonsoft.Json, 13.0.1"
    
    open System
    open Newtonsoft.Json
    
    JsonConvert.SerializeObject {| foo = "bar" |}
    |> printfn "%s"
  5. execute the script using cd temp && dotnet fsi test.fsx

Expected behavior

stdout prints:

{"foo":"bar"}

Actual behavior

stdout prints:

{"foo@":"bar","foo":"bar"}

Known workarounds

Change the sdk version to 6.0.107. I also observed the issue in 6.0.300

Related information

dotnet --info output below

.NET SDK (reflecting any global.json):
 Version:   6.0.302
 Commit:    c857713418

Runtime Environment:
 OS Name:     ubuntu
 OS Version:  22.04
 OS Platform: Linux
 RID:         ubuntu.22.04-x64
 Base Path:   /home/tracy/.dotnet/sdk/6.0.302/

global.json file:
  /home/tracy/code/fsi-test/global.json

Host:
  Version:      6.0.7
  Architecture: x64
  Commit:       0ec02c8c96

.NET SDKs installed:
  6.0.107 [/home/tracy/.dotnet/sdk]
  6.0.300 [/home/tracy/.dotnet/sdk]
  6.0.302 [/home/tracy/.dotnet/sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 6.0.5 [/home/tracy/.dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 6.0.7 [/home/tracy/.dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 6.0.5 [/home/tracy/.dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.7 [/home/tracy/.dotnet/shared/Microsoft.NETCore.App]

Download .NET:
  https://aka.ms/dotnet-download

Learn about .NET Runtimes and SDKs:
  https://aka.ms/dotnet/runtimes-sdk-info
HoraceGonzalez commented 2 years ago

Attached a sample project that reproduces the issue on my machine: issue-13546-repro.zip

Note, running the project w/t dotnet run . seems to work. The issue only seems to happen in FSI. I've tested using paket as well.

vzarytovskii commented 2 years ago

Duplicate of https://github.com/dotnet/fsharp/issues/13493