Skip to content

Generated RestClient Incorrectly Serializes Request Body #5062

Open

Description

Describe the issue or request
Utilizing Autorest v3 C# to generate REST clients based on an OpenAPI 3.0 Swagger JSON specification is resulting in certain input values for a given object to be serialized incorrectly. In the specification, the object has a 'value' property that can receive any JSON-supported data type (number, boolean, array of any data type, object, string), but when passing in array or object types as the 'value' property, the JSON writer malformats them into a collection of square and curly brackets. Stepping through the code it seems to make assumptions that the object and array types are Newtonsoft.Json.JArray and Newtonsoft.Json.JObject types, and then fails to enumerate over the array values, and then assumes the JObject type to be an array as opposed to a key value pair enumerable.

Here's what the array types look like before and after the writer attempts to serialize them into the request.
Before:

[
  "test1",
  "test2"
]

After:

[
  [],
  []
]

Here's what the object type looks like:
Before:

{
  "key1": "value1",
  "key2": "value2"
}

After:

[
  [
    []
  ],
  [
    []
  ]
]

Describe your ideas for solutions
Understanding why the switch case for Utf8JsonWriter.WriteObjectValue classifies JObject types as array types (Enumerable instead of enumerable key value pairs), and why Utf8JsonWriter.WriteObjectValue is unable to parse the JValues (items) within each JArray.

My workaround currently is just editing the generated client to serialize using a number of custom Newtonsoft JsonConverters, and I have not found a solution using the System.Text.Json or System.Text.Json.Serialization packages.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

Labels

v3Version 3 of AutoRest C# generator.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions