Search code examples
.net-core.net-8.0nswag

Newtonsoft.Json.JsonSerializationException: Error setting value to 'SelectedSwaggerGeneratorRaw' on 'NSwag.Commands.NSwagDocument'


I am upgrading the .NET 6.0 project to .NET 8.0.

After upgrading the

  1. .NET version to 8.0,
  2. NSwag.MSBuild package version: <PackageReference Include="NSwag.MSBuild" Version="14.0.4">

While generating Client using NSwag,

I am getting the below error:

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

Here is the NSwag.json file:

{
  "runtime": "Net80",
  "defaultVariables": "Configuration=Release,OutDir=bin/Release/net8.0/,HomeDir=%USERPROFILE%",
  "documentGenerator": {
    "webApiToOpenApi": {
      "controllerNames": [],
      "isAspNetCore": true,
      "resolveJsonOptions": false,
      "defaultUrlTemplate": "api/{controller}/{id?}",
      "addMissingPathParameters": false,
      "includedVersions": null,
      "defaultPropertyNameHandling": "CamelCase",
      "defaultReferenceTypeNullHandling": "Null",
      "defaultDictionaryValueReferenceTypeNullHandling": "NotNull",
      "defaultResponseReferenceTypeNullHandling": "Null",
      "defaultEnumHandling": "Integer",
      "flattenInheritanceHierarchy": false,
      "generateKnownTypes": true,
      "generateEnumMappingDescription": false,
      "generateXmlObjects": false,
      "generateAbstractProperties": false,
      "generateAbstractSchemas": true,
      "ignoreObsoleteProperties": false,
      "allowReferencesWithProperties": false,
      "excludedTypeNames": [],
      "serviceHost": null,
      "serviceBasePath": null,
      "serviceSchemes": [],
      "infoTitle": "User Authentication Content Services",
      "infoDescription": null,
      "infoVersion": "1.0.0",
      "documentTemplate": null,
      "documentProcessorTypes": [],
      "operationProcessorTypes": [],
      "typeNameGeneratorType": null,
      "schemaNameGeneratorType": null,
      "contractResolverType": null,
      "serializerSettingsType": null,
      "useDocumentProvider": true,
      "documentName": "v1",
      "aspNetCoreEnvironment": null,
      "createWebHostBuilderMethod": null,
      "startupType": null,
      "allowNullableBodyParameters": true,
      "output": null,
      "outputType": "Swagger2",
      "newLineBehavior": "Auto",
      "assemblyPaths": [
        "$(OutDir)/AControllers.dll",
        "$(OutDir)/AppServices.dll"
      ],
      "assemblyConfig": null,
      "referencePaths": [
        "$(HomeDir)/.nuget/packages"
      ],
      "useNuGetCache": false
    }
  }

If I use "noBuild": false, the error is not occurring. But the Client code not generating.

I was following the bug:(https://github.com/RicoSuter/NSwag/issues/4659) But no luck yet.


Solution

  • According to the post: https://github.com/RicoSuter/NSwag/issues/4524

    In NSwag.MSBUILd v14.7, nswag.json now only supports .csproj based specification generation (reflection/assembly based removed, only aspnetcore2openapi) Remove WebApiToOpenApiCommand: Use the WebApiOpenApiDocumentGenerator to build your own CLI referencing controllers project to generate OpenAPI without starting the app. Here is the change in the NSwag.json file:

        "defaultVariables": "Configuration=Release,OutDir=bin/Release/net8.0/,HomeDir=%USERPROFILE%",
    "documentGenerator": {
        "aspNetCoreToOpenApi": {
            "project": "MyAppServices.csproj",
            "assemblyPaths": [],