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

.NET 8 - Newtonsoft.Json.JsonSerializationException: Error setting value to 'SelectedSwaggerGeneratorRaw' on 'NSwag.Commands.NSwagDocument'. #4659

Open
jepperaskdk opened this issue Dec 26, 2023 · 4 comments

Comments

@jepperaskdk
Copy link

This bug seem similar to this, however I'm seeing this after upgrading from .NET 6 to .NET 8: #1576

Reproduce on a fresh API with no changes except nswag.json and adding nswag packages of 14.0.0-preview012. See this repository for a reproduce: https://github.com/jepperaskdk/nswagdotnet8

> dotnet "C:\Users\USER\.nuget\packages\nswag.msbuild\14.0.0-preview012\buildTransitive\../tools/Net80/dotnet-nswag.dll" run nswag.json
NSwag command line tool for .NET Core Net80, toolchain v14.0.0.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))
Visit http://NSwag.org for more information.
NSwag bin directory: C:\Users\Jeppe\.nuget\packages\nswag.msbuild\14.0.0-preview012\tools\Net80

Executing file 'nswag.json' with variables ''...
Newtonsoft.Json.JsonSerializationException: Error setting value to 'SelectedSwaggerGeneratorRaw' on 'NSwag.Commands.NSwagDocument'.
 ---> System.NullReferenceException: Object reference not set to an instance of an object.
   at NSwag.Commands.NSwagDocumentBase.set_SelectedSwaggerGeneratorRaw(JObject value) in /_/src/NSwag.Commands/NSwagDocumentBase.cs:line 88
   at Newtonsoft.Json.Serialization.ExpressionValueProvider.SetValue(Object target, Object value)
   --- End of inner exception stack trace ---
@jepperaskdk
Copy link
Author

jepperaskdk commented Dec 26, 2023

After cloning/running nswag locally, it fails on the same line:

var collectionProperty = SwaggerGenerators.GetType().GetRuntimeProperty(key[0].ToString().ToUpperInvariant() + key.Substring(1));

Where the key is "WebApiToOpenApiCommand"

SwaggerGenerators.Items has the following items:

{NSwag.Commands.IOutputCommand[3]}
    [0]: {NSwag.Commands.Generation.FromDocumentCommand}
    [1]: {NSwag.Commands.CodeGeneration.JsonSchemaToOpenApiCommand}
    [2]: {NSwag.Commands.Generation.AspNetCore.AspNetCoreToOpenApiCommand}

Any idea why it is missing WebApiToOpenApiCommand?
EDIT: I see now it is announced that WebApiToOpenApiCommand is deprecated and removed: #4524

@Ben555555
Copy link

Ben555555 commented Jan 8, 2024

@jepperaskdk I have the same issue. Do you know what the alternative is for "webApiToOpenApi" in nswag.json?

It says: "Removed WebApiToOpenApiCommand: Use the WebApiOpenApiDocumentGenerator to build your own CLI referencing controllers project to generate OpenAPI without starting the app" which links to https://github.com/RicoSuter/NSwag/wiki/WebApiOpenApiDocumentGenerator and there it says it's deprecated and links to https://github.com/RicoSuter/NSwag/wiki/AspNetCoreOpenApiDocumentGenerator where I cannot find any information how to change it. If I remove the documentGenerator from the nswag.json, it will build, but it will generate some random services with the code generator "openApiToTypeScriptClient".

When I use "aspNetCoreToOpenApi" for the document configuration it will start so many .NET processes that I will have to restart my computer.

I'm confused.

Edit: For now it seems to work for me like this, by setting noBuild = true:

{
  "runtime": "Net80",
  "defaultVariables": "Configuration=Debug",
  "documentGenerator": {
    "aspNetCoreToOpenApi": {
      "project": "MySolution.Api.csproj",
      "documentName": "v1",
      "msBuildProjectExtensionsPath": null,
      "configuration": null,
      "runtime": null,
      "targetFramework": null,
      "noBuild": true,
      "msBuildOutputPath": null,
      "verbose": true,
      "workingDirectory": null,
      "aspNetCoreEnvironment": null,
      "output": null,
      "newLineBehavior": "Auto"
    }
  },
  "codeGenerators": {
    "openApiToTypeScriptClient": {

But in my case I actually wouldn't need to run Program.cs which it seems to do. I only want to generate the client scripts...

@jepperaskdk
Copy link
Author

jepperaskdk commented Jan 8, 2024

@Ben555555 I have done the same, and I also experience the same issue with .NET processes being spawned, and as such I have had to disable generating the API clients on build (in csproj). EDIT: Setting "noBuild": true fixes it, as it still generates clients.

It might be a different issue however. For this issue, I suggest to throw a helpful error that "WebApiToOpenApiCommand" is not supported, rather than an obscure uncaught exception.

@MehdiElMellali
Copy link

I'm encountering a similar issue when migrating from .NET 6 to .NET 8 with NSwag. The error regarding 'WebApiToOpenApiCommand' removal and its alternatives resonates with what I'm experiencing. Have you found any further insights or workarounds since your last comment? I've tried adjusting configurations as suggested but still facing challenges. Any advice or updates on this matter would be greatly appreciated! Thanks in advance."

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants