RicoSuter / NJsonSchema

JSON Schema reader, generator and validator for .NET
http://NJsonSchema.org
MIT License
1.39k stars 534 forks source link

Property type "null | string" does not add StringEnumConverter attribute. #1161

Closed ikutsin closed 4 years ago

ikutsin commented 4 years ago

NJsonSchema.CodeGeneration.CSharp (10.1.12)

When there is a

    "myProperty": {
      "type": [
        "string",
        "null"
      ],
      "enum": [
        "AS_PER_GTC",
        "AS_PER_TTB",
        "AS_PER_CP",
        "NONE",
        null
      ]    },

The StringEnumConverter attribute is not being added.

[Newtonsoft.Json.JsonProperty("myProperty", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public MyClassMyProperty? MyProperty
{
    get;
    set;
}
[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "10.1.12.0 (Newtonsoft.Json v9.0.0.0)")]
[System.Flags]
public enum LogisticsNominationClause
{
    [System.Runtime.Serialization.EnumMember(Value = @"AS_PER_GTC")]
    AS_PER_GTC = 1,
    [System.Runtime.Serialization.EnumMember(Value = @"AS_PER_TTB")]
    AS_PER_TTB = 2,
    [System.Runtime.Serialization.EnumMember(Value = @"AS_PER_CP")]
    AS_PER_CP = 4,
    [System.Runtime.Serialization.EnumMember(Value = @"NONE")]
    NONE = 8,
}

Probably because: PropertyModel.cs

/// <summary>Gets a value indicating whether the property type is string enum.</summary>
public bool IsStringEnum
{
  get
  {
    if (this._property.ActualTypeSchema.IsEnumeration)
      return this._property.ActualTypeSchema.Type == JsonObjectType.String;
    return false;
  }
}
RicoSuter commented 4 years ago

Yes, probably it’s better to use .Type.HasFlag(string).

Can you create a PR?

ikutsin commented 4 years ago

Sure, done. When, do you think, it will be pushed to nuget feed?