Skip to content

Nullable Enums Create Empty Class #5324

@MuhKuh7

Description

@MuhKuh7

Describe the bug

A nullable enum property creates a C# class with a property that has an empty class as its type.

Version of NSwag toolchain, computer and .NET runtime used

NSwagStudio 14.6.3.0 with .NET 10

To Reproduce

I have a simple object with a nullable enum property.

{
  "openapi": "3.1.1",
  "info": {
    "title": "TestApi | v1",
    "version": "1.0.0"
  },
  "components": {
    "schemas": {
      "ResponseObject": {
        "type": "object",
        "properties": {
          "testProperty": {
            "oneOf": [
              {
                "type": "null"
              },
              {
                "$ref": "#/components/schemas/MyEnum"
              }
            ]
          }
        }
      },
      "MyEnum": {
        "enum": [
          "Entry1",
          "Entry2",
          null
        ]
      }
    }
  }
}

When creating a C# client out of this, the resulting property is:

public TestProperty? TestProperty { get; set; } = default!;

with TestProperty being an empty class (besided the AdditionalProperties)

public partial class TestProperty
{

	private System.Collections.Generic.IDictionary<string, object>? _additionalProperties;

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

}

The enum itself is correctly created, but unsused.

public enum MyEnum
{

	[System.Runtime.Serialization.EnumMember(Value = @"Entry1")]
	Entry1 = 0,

	[System.Runtime.Serialization.EnumMember(Value = @"Entry2")]
	Entry2 = 1,

}

Expected behavior
A property that uses the correct nullable enum.

public MyEnum? TestProperty { get; set; }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions