RicoSuter / NSwag

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

Nullable reference types are generated with default! #4000

Open Menur opened 2 years ago

Menur commented 2 years ago

Version

Expected

public MyInnerDto MyProp { get; set; } = new();

Example

paths:
components:
  schemas:
    MyDto:
      type: object
      properties:
        myProp:
          "$ref": "#/components/schemas/MyInnerDto"
    MyInnerDto:
      type: object
      properties:
        myProp:
          type: string
          format: uuid
          nullable: false
dotnet ./NSwag/Net60/dotnet-nswag.dll openapi2csclient \
    /input:/local/myDtos.yaml \
    /ContractsOutput:Dtos.Generated.cs \
    /namespace:MyNameSpace \
    /ContractsNamespace:MyNameSpace \
    /DateType:System.DateTimeOffset \
    /DateTimeType:System.DateTimeOffset \
    /GenerateClientClasses:false \
    /GenerateNullableReferenceTypes:true \
    /GenerateOptionalPropertiesAsNullable:false \
    /JsonLibrary:NewtonsoftJson \
    /GenerateContractsOutput:true
//----------------------
// <auto-generated>
//     Generated using the NSwag toolchain v13.15.10.0 (NJsonSchema v10.6.10.0 (Newtonsoft.Json v13.0.0.0)) (http://NSwag.org)
// </auto-generated>
//----------------------

#nullable enable

#pragma warning disable 108 // Disable "CS0108 '{derivedDto}.ToJson()' hides inherited member '{dtoBase}.ToJson()'. Use the new keyword if hiding was intended."
#pragma warning disable 114 // Disable "CS0114 '{derivedDto}.RaisePropertyChanged(String)' hides inherited member 'dtoBase.RaisePropertyChanged(String)'. To make the current member override that implementation, add the override keyword. Otherwise add the new keyword."
#pragma warning disable 472 // Disable "CS0472 The result of the expression is always 'false' since a value of type 'Int32' is never equal to 'null' of type 'Int32?'
#pragma warning disable 1573 // Disable "CS1573 Parameter '...' has no matching param tag in the XML comment for ...
#pragma warning disable 1591 // Disable "CS1591 Missing XML comment for publicly visible type or member ..."
#pragma warning disable 8073 // Disable "CS8073 The result of the expression is always 'false' since a value of type 'T' is never equal to 'null' of type 'T?'"
#pragma warning disable 3016 // Disable "CS3016 Arrays as attribute arguments is not CLS-compliant"
#pragma warning disable 8603 // Disable "CS8603 Possible null reference return"

namespace MyNameSpace
{
    using System = global::System;

    [System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "13.15.10.0 (NJsonSchema v10.6.10.0 (Newtonsoft.Json v13.0.0.0))")]
    public partial class MyDto
    {
        [Newtonsoft.Json.JsonProperty("myProp", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
        public MyInnerDto MyProp { get; set; } = default!;

        private System.Collections.Generic.IDictionary<string, object> _additionalProperties = new System.Collections.Generic.Dictionary<string, object>();

        [Newtonsoft.Json.JsonExtensionData]
        public System.Collections.Generic.IDictionary<string, object> AdditionalProperties
        {
            get { return _additionalProperties; }
            set { _additionalProperties = value; }
        }

    }

    [System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "13.15.10.0 (NJsonSchema v10.6.10.0 (Newtonsoft.Json v13.0.0.0))")]
    public partial class MyInnerDto
    {
        [Newtonsoft.Json.JsonProperty("myProp", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
        public System.Guid MyProp { get; set; } = default!;

        private System.Collections.Generic.IDictionary<string, object> _additionalProperties = new System.Collections.Generic.Dictionary<string, object>();

        [Newtonsoft.Json.JsonExtensionData]
        public System.Collections.Generic.IDictionary<string, object> AdditionalProperties
        {
            get { return _additionalProperties; }
            set { _additionalProperties = value; }
        }

    }

}

#pragma warning restore 1591
#pragma warning restore 1573
#pragma warning restore  472
#pragma warning restore  114
#pragma warning restore  108
#pragma warning restore 3016
#pragma warning restore 8603
ovska commented 2 years ago

You should be able to around this by creating parameterless constructors in another partial class-file which initialize the properties you need.

molinch commented 1 year ago

The proper solution would be to leverage init properties along with C#11 required attribute