Skip to content

Missing type attribute from array property with enum string values restriction serialises as array of int. #5167

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
andrensairr opened this issue May 13, 2025 · 0 comments

Comments

@andrensairr
Copy link

Describe the bug

When generating a client from a third-party OpenAPI YAML specification which describes a model property as an array of enum strings, but is missing the type attribute from the items schema, the C# DTO property is generated with a Newtonsoft.Json.JsonPropertyAttribute without a ItemConverterType property being set. Other example with arrays of enum options are generated as expected when the type attribute is set to string. This leads to object instances being serialised to JSON with numeric values, rather than string values from the specified enum.

Version of NSwag toolchain, computer and .NET runtime used

14.4.0.0, .NET Framework 4.7.1.

To Reproduce

Given the following YAML schema:

exampleModel:
  type: object
  required:
  - exampleProperty
  properties:
    exampleProperty:
      type: array
      minItems: 1
      items:
        enum:
        - VALUE_ONE
        - VALUE_TWO
        - VALUE_THREE
      description: "A description of the property"

The generated DTO object property's serialisation attribute hint is generated with a missing ItemConverterType:

/// <summary>
/// A description of the property
/// </summary>
[Newtonsoft.Json.JsonProperty("exampleProperty", Required = Newtonsoft.Json.Required.Always)]
[System.ComponentModel.DataAnnotations.Required]
public System.Collections.Generic.ICollection<ExampleProperty> ExampleProperty { get; set; } = new System.Collections.ObjectModel.Collection<ExampleProperty>();

Calling Newtonsoft.Json.JsonConvert.SerializeObject on such an object results in JSON similar to the following:

{"exampleProperty":[0,2]}

Expected behavior

I expected the generator would infer the array items schema type from the enum and generate a c# property like the following.

/// <summary>
/// A description of the property
/// </summary>
[Newtonsoft.Json.JsonProperty("exampleProperty", Required = Newtonsoft.Json.Required.Always, ItemConverterType = typeof(Newtonsoft.Json.Converters.StringEnumConverter))]
        [System.ComponentModel.DataAnnotations.Required]
public System.Collections.Generic.ICollection<ExampleProperty> ExampleProperty { get; set; } = new System.Collections.ObjectModel.Collection<ExampleProperty>();

Which would in turn serialise like the following:

{"exampleProperty":["VALUE_ONE","VALUE_THREE"]}

Additional context

I realise the specification probably should include a type attribute, however as best as I can determine from the JSON Schema spec, it was never intended that this would be required - in fact, it is already implied by the valid set of strings in the enum. See this discussion. The YAML is unambiguous about which values are valid.

Apologies if this should have been posted to the NJsonSchema repo, but I'm not familiar enough yet with how these libraries work to determine where the logic is located which produces the output I'm seeing. I can't tell if the problem is due to the logic in the code generation model to determine which liquid template to use, or whether it's how the code generation model is built from the YAML spec.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant