svenheden / csharp-models-to-typescript

C# models to TypeScript
88 stars 58 forks source link

Error "The output from `csharp-models-to-json` contains invalid JSON." on nuget warnings in projects #61

Closed r-pankevicius closed 3 months ago

r-pankevicius commented 2 years ago

Version: "csharp-models-to-typescript": "^0.21.1"

After recent Visual Studio 2022 update I started to get NU1803 warnings compiling projects (actually not compiling, but restoring). And npm run generate-types stopped working.

generate-types defined in package.json scripts:

"generate-types": "csharp-models-to-typescript --config=cs-to-ts-config.json"

Output from npm run generate-types:

> csharp-models-to-typescript --config=cs-to-ts-config.json

The output from `csharp-models-to-json` contains invalid JSON.

Unexpected token C in JSON at position 0

C:\sources\ReactApp\ClientApp\node_modules\csharp-models-to-typescript\lib\csharp-models-to-json\csharp-models-to-json.csproj : warning NU1803: You are running the 'restore' operation with an 'HTTP' source, 'http://nuget.mycompany.com/v3/index.json'. Non-HTTPS access will be removed in a future version. Consider migrating to an 'HTTPS' source.
C:\sources\ReactApp\ClientApp\node_modules\csharp-models-to-typescript\lib\csharp-models-to-json\csharp-models-to-json.csproj : warning NU1803: You are running the 'restore' operation with an 'HTTP' source, 'http://nuget.mycompany.com/v3/index.json'. Non-HTTPS access will be removed in a future version. Consider migrating to an 'HTTPS' source.
[{"FileName":"C:\\sources\\ReactApp\Backend\\Models\\Customer.cs","Models":[{"ModelName":
...

Proper JSON goes after warnings.

The quick fix is to change line in index.js to remove warning lines:

json = JSON.parse(stdout);

to this:

const removeWarningLines = (str) =>
    str.includes("csharp-models-to-json.csproj : warning") ? "" : str;

stdout = stdout.split("\n").map(removeWarningLines).join("\n");
stdout = stdout.split("\r").map(removeWarningLines).join("\r");
json = JSON.parse(stdout);
cederron commented 1 year ago

I solved this installing .net core 3.1 runtime.

calebeno commented 9 months ago

Update to this issue. We're also starting to see a similar thing but with a different set of warnings:

The output from 'csharp-models-to-json' contains invalid JSON.

warning NETSDK1138: The target framework 'netcoreapp3.1' is out of support and will not receive security updates in the future. Please refer to https://aka.ms/dotnet-core-support for more information about the support policy.

warning NU1903: Package 'Newtonsoft.Json' 12.0.3 has a known high severity vulnerability, https://github.com/advisories/GHSA-5crp-9r3c-p9vr

For new team members, installing .net core 3.1 runtime does not fix this issue. I had to recompile the csharp-models-to-json project using .net 7 after updating the target platform and updating the nuget packages. Then I zipped that up to distribute to the team and am having team members replace the csharp-models-to-json folder in their node modules with the new one.

I can confirm this does fix it.

Obviously, this is less than ideal as any reinstall of the node modules will blow away this change. Thankfully, this conversion process for us is not a part of ci, otherwise we'd probably have to look into forking the library.

I think it would be valuable to have this library updated to .net 7 at least. Alternatively, you may be able to compile multiple versions (though this will admittedly increase the package size).

Just some thoughts for improvement as well as a sure-fire way for anyone else with this problem to solve it. You could easily change the .net version to support whatever environment you are working with.

calebeno commented 9 months ago

Update to the above. We've gone ahead and moved the library out of node modules and into our main codebase. This effectively creates a local fork with the update to .net 7.

r-pankevicius commented 9 months ago

That's different issue. Can be fixed by changing csharp-models-to-json.csproj:

<TargetFramework>net8.0</TargetFramework>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
calebeno commented 9 months ago

@r-pankevicius yes, the underlying issue is different but it produces a similar effect on the json output. I found this thread in my own search for a solution so I figured it was a reasonable place to give my own update. Did moving to 8.0 resolve your original issue as well?

r-pankevicius commented 9 months ago

@calebeno It doesn't matter .net 8.0 or 7.0. Should work for both, Newtonsoft.Json must be updated.

calebeno commented 9 months ago

That was my solution as well. We moved the package out of node modules and into a lib folder within our project. Unfortunate but seems to be what has to be done.

r-pankevicius commented 8 months ago

Worked well, @calebeno :

npm uninstall csharp-models-to-typescript

Put csharp-models-to-typescript sources under folder lib/csharp-models-to-typescript

Made 2 tweaks mentioned in this thread

In package.json changed from "generate-types": "csharp-models-to-typescript --config=cs-to-ts-config.json" to "generate-types": "node ./lib/csharp-models-to-typescript/index.js --config=cs-to-ts-config.json"

Last thing was to change container csproj of .net web app to exclude csharp-models-to-typescript inner csproj-s:

  <ItemGroup>
    <Compile Remove="ClientApp\lib\**" />
    <Content Remove="ClientApp\lib\**" />
    <EmbeddedResource Remove="ClientApp\lib\**" />
  </ItemGroup>