Policy.Autorest/custom/ParameterValuesValue.cs

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
// Code generated by Microsoft (R) AutoRest Code Generator.
// Changes may cause incorrect behavior and will be lost if the code is regenerated.
 
namespace Microsoft.Azure.PowerShell.Cmdlets.Policy.Models
{
    using Microsoft.Azure.PowerShell.Cmdlets.Policy.Runtime.PowerShell;
    public partial class ParameterValuesValue
    {
        // Temporary storage used within a single ToJson() call to save a scalar Value
        // while the generated serialization body runs (which skips null fields).
        private Any _savedValue;
 
        /// <summary>
        /// Case-insensitive deserialization from a PS hashtable/dictionary.
        /// The generated constructor gates on <c>content.Contains("Value")</c> (case-sensitive) which
        /// misses the lowercase <c>"value"</c> key produced by <c>ConvertFrom-Json -AsHashtable</c>.
        /// <c>GetValueForProperty</c> is case-insensitive, so we use it directly here.
        /// Also adds scalar fallback: <c>AnyTypeConverter.ConvertFrom</c> only handles JSON objects and
        /// PS dictionaries; plain scalars (strings, numbers, booleans) are wrapped with
        /// <see cref="Any.FromScalar"/> so they are not silently dropped.
        /// </summary>
        partial void BeforeDeserializeDictionary(global::System.Collections.IDictionary content, ref bool returnNow)
        {
            ((IParameterValuesValueInternal)this).Value =
                content.GetValueForProperty("Value", (IAny)null, v =>
                {
                    var converted = AnyTypeConverter.ConvertFrom(v);
                    return converted ?? (v != null ? Any.FromScalar(v) : null);
                });
            // Prevent the generated body from running — it uses the bare AnyTypeConverter.ConvertFrom
            // which returns null for scalar values, overwriting the correct scalar Any we just set.
            returnNow = true;
        }
 
        /// <summary>Same scalar fallback as <see cref="BeforeDeserializeDictionary"/> for PSObject input.</summary>
        partial void BeforeDeserializePSObject(global::System.Management.Automation.PSObject content, ref bool returnNow)
        {
            ((IParameterValuesValueInternal)this).Value =
                content.GetValueForProperty("Value", (IAny)null, v =>
                {
                    var converted = AnyTypeConverter.ConvertFrom(v);
                    return converted ?? (v != null ? Any.FromScalar(v) : null);
                });
            returnNow = true;
        }
 
        /// <summary>
        /// Fixes up deserialization of scalar <c>value</c> fields from JSON.
        /// The generated constructor uses <c>PropertyT&lt;JsonObject&gt;("value")</c> which silently
        /// returns null when the value is a JSON string, number, boolean, or array.
        /// </summary>
        partial void AfterFromJson(Microsoft.Azure.PowerShell.Cmdlets.Policy.Runtime.Json.JsonObject json)
        {
            if (_value == null && json != null
                && json.TryGetValue("value", out var rawNode)
                && rawNode != null && !rawNode.IsNull)
            {
                _value = Any.FromScalar(rawNode.ToValue());
            }
        }
 
        /// <summary>
        /// When <c>Value</c> is a scalar <see cref="Any"/>, the generated serialization body would call
        /// <c>_value.ToJson()</c> which always produces <c>{}</c>. This hook writes the correct scalar
        /// node directly, then temporarily nulls <c>_value</c> so the generated <c>AddIf</c> skips it.
        /// <see cref="AfterToJson"/> restores the field.
        /// </summary>
        partial void BeforeToJson(ref Microsoft.Azure.PowerShell.Cmdlets.Policy.Runtime.Json.JsonObject container, ref bool returnNow)
        {
            if (_value is Any anyVal && anyVal.IsScalar)
            {
                var scalarNode = Microsoft.Azure.PowerShell.Cmdlets.Policy.Runtime.JsonSerializable.ToJsonValue(anyVal.ScalarValue);
                if (scalarNode != null)
                {
                    container.Add("value", scalarNode);
                }
                _savedValue = anyVal;
                _value = null;
            }
        }
 
        /// <summary>Restores <c>Value</c> after the generated serialization body has run.</summary>
        partial void AfterToJson(ref Microsoft.Azure.PowerShell.Cmdlets.Policy.Runtime.Json.JsonObject container)
        {
            if (_savedValue != null)
            {
                _value = _savedValue;
                _savedValue = null;
            }
        }
    }
}