RicoSuter / NSwag

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

JsonInheritanceConverter NRT Warnings #3928

Open csehatt741 opened 2 years ago

csehatt741 commented 2 years ago

When nullable reference types are allowed in the project containing the generated code, several JsonInheritanceConverter related warnings are raised.

Command: openapi2csclient JsonLibrary: SystemTextJson NSwag version: 13.15.10.0

JsonInheritanceConverter.Read: hasDiscriminator ? discriminator.GetString(): Possible null reference argument for parameter 'discriminatorValue' in 'Type JsonInheritanceConverter.GetDiscriminatorType(JsonElement jObject, Type objectType, string discriminatorValue)'.

return (TBase)System.Text.Json.JsonSerializer.Deserialize(bufferWriter.ToArray(), subtype, options);: Converting null literal or possible null value to non-nullable type.

JsonInheritanceConverter.Write: GetDiscriminatorValue(value.GetType()): Dereference of a possibly null reference.

Required fixes: GetDiscriminatorType: string discriminatorValue -> string? discriminatorValue GetObjectSubtype: string discriminatorValue -> string? discriminatorValue Read: return type TBase? JsonInheritanceConverter: add where TBase : class constraint

SchuylerGoodman commented 1 year ago

This still seems to be a problem. Is there a workaround?

AnthonyLloyd commented 6 months ago

Is the best workaround to this to edit the generated code and put the methods in #nullable disable / #nullable enable?