RicoSuter / NSwag

The Swagger/OpenAPI toolchain for .NET, ASP.NET Core and TypeScript.
http://NSwag.org
MIT License
6.81k stars 1.3k forks source link

Null Reference exception using DateTime parameter in Swagger 2 using Moment JS #4016

Open dub-survivor opened 2 years ago

dub-survivor commented 2 years ago

This is re-creatable by creating a new asp.net Core Web Api using Visual Studio 2022 (not sure if this matters) and .net 6.0. This will generate the WeatherForecast API. Add NSwag.AspNetCore nuget package. Add a DateTime parameter to the Get method.

[HttpGet(Name = "GetWeatherForecast")]
public IEnumerable<WeatherForecast> Get(DateTime date)
{
    return Enumerable.Range(1, 5).Select(index => new WeatherForecast
    {
        Date = DateTime.Now.AddDays(index),
        TemperatureC = Random.Shared.Next(-20, 55),
        Summary = Summaries[Random.Shared.Next(Summaries.Length)]
    })
    .ToArray();
}

The Program.cs file is altered to generate Swagger v2 output

builder.Services.AddSwaggerGen();

builder.Services.AddSwaggerDocument();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseOpenApi();
    app.UseSwaggerUi3();
}

The NSwag code generator is set to output a Typescript client, using Angular Template (RxJs version 6, TypeScript version 2.7) and Moment JS Date Time type. The exception is occurring in the TypeScriptParameterModel , GetDateTimeToString method. The _schema member is null. This error possibly occurs in any version of NSwagStudio greater than v13.15.9.0. It definitely occurs in v13.15.10.0 and v13.16.0.0

Regards

BG-Software-BG commented 2 years ago

I have a similar problem after updating NSwag from v13.13.2.0 to v13.15.10.0 (and later to v13.16.0.0). I am getting an error while generating TypeScript files. Everything worked fine in 13.13.2.0 version.

I found out that changing "Date Time Type" from "MomentJS" to "Date", "String" or "Luxon" in settings solves the issue but I want to keep MomentJS.

image

My nswag.json:

{
  "runtime": "Net50",
  "defaultVariables": null,
  "documentGenerator": {
    "fromDocument": {
      "json": "",
      "url": "http://localhost:4816/swagger/v1/swagger.json",
      "output": null
    }
  },
  "codeGenerators": {
    "openApiToTypeScriptClient": {
      "className": "{controller}Service",
      "moduleName": "",
      "namespace": "",
      "typeScriptVersion": 2.7,
      "template": "Angular",
      "promiseType": "Promise",
      "httpClass": "HttpClient",
      "useSingletonProvider": false,
      "injectionTokenType": "InjectionToken",
      "rxJsVersion": 6.0,
      "dateTimeType": "MomentJS",
      "nullValue": "Undefined",
      "generateClientClasses": true,
      "generateClientInterfaces": false,
      "generateOptionalParameters": false,
      "exportTypes": true,
      "wrapDtoExceptions": false,
      "exceptionClass": "SwaggerException",
      "clientBaseClass": null,
      "wrapResponses": false,
      "wrapResponseMethods": [],
      "generateResponseClasses": true,
      "responseClass": "SwaggerResponse",
      "protectedMethods": [],
      "configurationClass": null,
      "useTransformOptionsMethod": false,
      "useTransformResultMethod": false,
      "generateDtoTypes": true,
      "operationGenerationMode": "MultipleClientsFromOperationId",
      "markOptionalProperties": true,
      "generateCloneMethod": false,
      "typeStyle": "Class",
      "classTypes": [],
      "extendedClasses": [],
      "extensionCode": null,
      "generateDefaultValues": true,
      "excludedTypeNames": [],
      "excludedParameterNames": [],
      "handleReferences": false,
      "generateConstructorInterface": true,
      "convertConstructorInterfaceData": false,
      "importRequiredTypes": true,
      "useGetBaseUrlMethod": false,
      "baseUrlTokenName": "API_BASE_URL",
      "queryNullValue": "",
      "inlineNamedDictionaries": false,
      "inlineNamedAny": false,
      "templateDirectory": null,
      "typeNameGeneratorType": null,
      "propertyNameGeneratorType": null,
      "enumNameGeneratorType": null,
      "serviceHost": null,
      "serviceSchemes": null,
      "output": "../../Web/src/app/core/services/service-proxies.ts"
    }
  }
}