Skip to content

Fail to deserialize the model when additionalProperties is an object #625

@isra-fel

Description

@isra-fel

Current behavior

When a model is defined to have additionalProperties (aka it's a dictionary) and its value type is object, for example https://github.com/Azure/azure-rest-api-specs/blob/29446bf77d48b7128b0c6d587b78355c2b4dde73/specification/appconfiguration/resource-manager/Microsoft.AppConfiguration/preview/2019-11-01-preview/appconfiguration.json#L1303

those additional properties are lost during deserialization, resulting an empty dictionary.

Root cause

The generated code for deserialization is like:

internal ResourceIdentityUserAssignedIdentities(Microsoft.Azure.PowerShell.Cmdlets.AppConfiguration.Runtime.Json.JsonObject json, global::System.Collections.Generic.HashSet<string> exclusions = null)
{
    bool returnNow = false;
    BeforeFromJson(json, ref returnNow);
    if (returnNow)
    {
        return;
    }
    Microsoft.Azure.PowerShell.Cmdlets.AppConfiguration.Runtime.JsonSerializable.FromJson( json, ((Microsoft.Azure.PowerShell.Cmdlets.AppConfiguration.Runtime.IAssociativeArray<Microsoft.Azure.PowerShell.Cmdlets.AppConfiguration.Models.Api20191101Preview.IUserIdentity>)this).AdditionalProperties, null ,exclusions );
    AfterFromJson(json);
}

The third parameter of JsonSerializable.FromJson() should specify how the value object is deserialized, but it's null.

Work-around

Currently the work-around is to write a custom class to implement BeforeFromJson method. For example:

public partial class ResourceIdentityUserAssignedIdentities
{
    partial void BeforeFromJson(Microsoft.Azure.PowerShell.Cmdlets.AppConfiguration.Runtime.Json.JsonObject json, ref bool returnNow)
    {
        // Perform deserialization using the appropriate object factory for user identity
        Microsoft.Azure.PowerShell.Cmdlets.AppConfiguration.Runtime.JsonSerializable.FromJson(
            json,
            ((Microsoft.Azure.PowerShell.Cmdlets.AppConfiguration.Runtime.IAssociativeArray<Microsoft.Azure.PowerShell.Cmdlets.AppConfiguration.Models.Api20191101Preview.IUserIdentity>)this).AdditionalProperties,
            (identity) => UserIdentity.FromJson(identity),
            null);
        returnNow = true;
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions