kiota: C# Model - oneOf of list and object is not deserialized correctly

I want to consume an API that has the following construct for properties (OneOrManyItems in this example) at quite a lot of places:

{
  "openapi": "3.0.3",
  "info": {
    "title": "Test",
    "version": ""
  },
  "paths": {
    "/test": {
      "get": {
        "responses": {
          "200": {
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Result"
                }
              }
            }
          },
          "401": {
            "description": "Unauthorized Request"
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "Result": {
        "properties": {
          "OneOrManyItems": {
            "oneOf": [
              {
                "type": "array",
                "items": {
                  "$ref": "#/components/schemas/Item"
                }
              },
              {
                "$ref": "#/components/schemas/Item"
              }
            ]
          }
        }
      },
      "Item": {
        "properties": {
          "Id": {
            "type": "string"
          }
        }
      }
    }
  }
}

Its basically a property (OneOrManyItems) that can be a list of objects or just one object (both with the same type Item).

When I now generate a client using this command: kiota generate -d desciption.json -l CSharp. I get a model for the result that does not parse the json response correctly. It only parses it correctly for the case that it is a list of items not a single item.

The Result_OneOrManyItems class inside the Result looks something like this:

public class Result_OneOrManyItems : IComposedTypeWrapper, IParsable {
	public List<ApiSdk.Models.Item>? Item { get; set; }
	public ApiSdk.Models.Item? ResultOneOrManyItemsItem { get; set; }

	public static Result_OneOrManyItems CreateFromDiscriminatorValue(IParseNode parseNode) {
		_ = parseNode ?? throw new ArgumentNullException(nameof(parseNode));
		var mappingValue = parseNode.GetChildNode("")?.GetStringValue();
		var result = new Result_OneOrManyItems();
		if("Item".Equals(mappingValue, StringComparison.OrdinalIgnoreCase)) {
			result.ResultOneOrManyItemsItem = new ApiSdk.Models.Item();
		}
		else if(parseNode.GetCollectionOfObjectValues<ApiSdk.Models.Item>(ApiSdk.Models.Item.CreateFromDiscriminatorValue)?.ToList() is List<ApiSdk.Models.Item> itemValue) {
			result.Item = itemValue;
		}
		return result;
	}
	
	public virtual IDictionary<string, Action<IParseNode>> GetFieldDeserializers() {
		if(ResultOneOrManyItemsItem != null) {
			return ResultOneOrManyItemsItem.GetFieldDeserializers();
		}
		return new Dictionary<string, Action<IParseNode>>();
	}
	
	public virtual void Serialize(ISerializationWriter writer) {
		_ = writer ?? throw new ArgumentNullException(nameof(writer));
		if(ResultOneOrManyItemsItem != null) {
			writer.WriteObjectValue<ApiSdk.Models.Item>(null, ResultOneOrManyItemsItem);
		}
		else if(Item != null) {
			writer.WriteCollectionOfObjectValues<ApiSdk.Models.Item>(null, Item);
		}
	}
}

When deserialized the ResultOneOrManyItemsItem property is always null and the Item is always non null but only contains something, if the parsed json is an array. The problem seems to me that the CreateFromDiscriminatorValue function only looks at the discriminator value even if there is none and not at the type of the json that is being parsed.

The solution I found for my usecase looks like this:

public static Result_OneOrManyItems CreateFromDiscriminatorValue(IParseNode parseNode) {
	_ = parseNode ?? throw new ArgumentNullException(nameof(parseNode));
	var result = new Result_OneOrManyItems();
	var list = parseNode.GetCollectionOfObjectValues<ApiSdk.Models.Item>(ApiSdk.Models.Item.CreateFromDiscriminatorValue)?.ToList() as List<ApiSdk.Models.Item>;
	if((list.Count ?? 0) == 0) {
		result.ResultOneOrManyItemsItem = new ApiSdk.Models.Item();
	}
	else {
		result.Item = list;
	}
	return result;
}

This is far from ideal because it does not allow empty lists and does not use the real underling type. I also have to touch a lot of generated code for this solution which is not convenient and error prone.

This Issue is probably similar to #2338 but it wasn’t 100% my use case, hence I created a new issue. I also saw a similar API description in this issue #3963 but the problem was a different one.

If this is not fixable in the short term is there a better workaround than adjusting all implementations of CreateFromDiscriminatorValue by hand?

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Comments: 18 (11 by maintainers)

Most upvoted comments

The related issue I encounter is that type value for deserialization is expected from the type name, not the given type enum values. Given the following schema where ParentObject and two derived objects can be returned in the array of ResultObject Discriminator with enum is defined.

components:
    schemas:
      ParentObject:
        required:
        - type
        type: object
        properties:
          type:
            type: string
            enum:
            - derivedA
            - derivedB
        discriminator:
          propertyName: type
          mapping:
            derivedA: '#/components/schemas/DerivedAObject'
            derivedB: '#/components/schemas/DerivedBObject'
      DerivedAObject:
        required:
        - type
        type: object
        allOf:
        - $ref: '#/components/schemas/ParentObject'
      DerivedBObject:
        required:
        - type
        type: object
        allOf:
        - $ref: '#/components/schemas/ParentObject'
      ResultObject:
        required:
        - objects_array
        type: object
        properties:
          objects_array:
            uniqueItems: true
            type: array
            items:
              oneOf:
              - $ref: '#/components/schemas/DerivedAObject'
              - $ref: '#/components/schemas/DerivedBObject'

When parsing ResultObject object_array:

public static global::Client.Models.ResultObject_object_array CreateFromDiscriminatorValue(IParseNode parseNode)
{
    _ = parseNode ?? throw new ArgumentNullException(nameof(parseNode));
    var mappingValue = parseNode.GetChildNode("type")?.GetStringValue();
    var result = new global::Client.Models.ResultObject.ResultObject_object_array();
    //Expecting if("derivedA".Equals... 
    if("DerivedAObject".Equals(mappingValue, StringComparison.OrdinalIgnoreCase)) 
    {
        result.DerivedAObject= new global::Client.Models.DerivedAObject();
    }
    //Expecting if("derivedB".Equals... 
    else if("DerivedBObject".Equals(mappingValue, StringComparison.OrdinalIgnoreCase))
    {
        result.DerivedBObject= new global::Client.Models.DerivedBObject();
    }
    return result;
}

Is this an expected behavior or a potential bug?

gentle reminder @andrueastman on this one. It seems we forgot about it. Sorry about that @Irame