Skip to content
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

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

Closed
isra-fel opened this issue Jul 16, 2020 · 0 comments
Closed

Comments

@isra-fel
Copy link
Member

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;
    }
}
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