RicoSuter / NJsonSchema

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

C# client generator does not generate enum converter attribute for System.Text.Json serializer #1474

Open Dietatko opened 2 years ago

Dietatko commented 2 years ago

One of data schemas in OpenApi spec has property containing array of DayOfWeek enumerations:

    ShiftDto:
      type: object
      properties:
        daysOfWeekActive:
          type: array
          items:
            $ref: '#/components/schemas/DayOfWeek'
    DayOfWeek:
      enum:
        - Sunday
        - Monday
        - Tuesday
        - Wednesday
        - Thursday
        - Friday
        - Saturday
      type: string

I want to use the System.DayOfWeek enum in the generated C# client so I tweaked the generator options:

{
  ...,
  "codeGenerators": {
    "openApiToCSharpClient": {
      "additionalNamespaceUsages": [
        "DayOfWeek = System.DayOfWeek"
      ],
      "excludedTypeNames": [
        "DayOfWeek"
      ],
      ...
    }
  }
}

Actual behaviour:

The generated client code is:

    public partial class ShiftDto
    {
        [System.Text.Json.Serialization.JsonPropertyName("daysOfWeekActive")]
        // TODO(system.text.json): Add string enum item converter
        public System.Collections.Generic.ICollection<DayOfWeek> DaysOfWeekActive { get; set; }
    }

and the deserialisation fails unless the JsonStringEnumConverter is explicitly added to serialiser options.

Expected behaviour:

The object can be deserialised without configuring the serialiser.

zsyphon commented 2 years ago

@Dietatko, Did you find a solution to your problem? We have a similar issue. THnak you for your help!!