You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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: objectrequired:
- examplePropertyproperties:
exampleProperty:
type: arrayminItems: 1items:
enum:
- VALUE_ONE
- VALUE_TWO
- VALUE_THREEdescription: "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]publicSystem.Collections.Generic.ICollection<ExampleProperty>ExampleProperty{get;set;}=newSystem.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]publicSystem.Collections.Generic.ICollection<ExampleProperty>ExampleProperty{get;set;}=newSystem.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.
The text was updated successfully, but these errors were encountered:
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 aItemConverterType
property being set. Other example with arrays of enum options are generated as expected when thetype
attribute is set tostring
. 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:
The generated DTO object property's serialisation attribute hint is generated with a missing ItemConverterType:
Calling
Newtonsoft.Json.JsonConvert.SerializeObject
on such an object results in JSON similar to the following:Expected behavior
I expected the generator would infer the array items schema type from the enum and generate a c# property like the following.
Which would in turn serialise like the following:
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.
The text was updated successfully, but these errors were encountered: