{"record":{"id":"ecd6718f06257359","repo":"microsoft/aspire","slug":"expected-a-typeof-t-name-literal-or-nameof-bicepvalueproxy","errorCode":null,"errorMessage":"Expected a {typeof(T).Name} literal or {nameof(BicepValueProxy)}.","messagePattern":"Expected a (.+?) literal or (.+?)\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting.Azure.Provisioning/BicepValueProxy.cs","lineNumber":143,"sourceCode":"    public static BicepValue<T> Convert<T>(object value)\n    {\n        ArgumentNullException.ThrowIfNull(value);\n\n        if (value is BicepValueProxy proxy)\n        {\n            proxy.EnsureLiteralType<T>();\n\n            var converted = new BicepValue<T>((T)default!);\n            proxy.AssignTo(converted);\n            return converted;\n        }\n\n        if (value is T literal)\n        {\n            return literal;\n        }\n\n        throw new ArgumentException($\"Expected a {typeof(T).Name} literal or {nameof(BicepValueProxy)}.\", nameof(value));\n    }\n\n    internal static BicepValueProxy CreateExpression(BicepExpression expression, bool isSecure)\n    {\n        ArgumentNullException.ThrowIfNull(expression);\n\n        return new(new BicepValue<object>(expression), typeof(object), isSecure);\n    }\n\n    internal BicepExpression ToBicepExpression()\n    {\n        return _value.ToBicepExpression();\n    }\n\n    internal BicepValue<object> ToObjectBicepValue()\n    {\n        return new(ToBicepExpression());\n    }","sourceCodeStart":125,"sourceCodeEnd":161,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.Azure.Provisioning/BicepValueProxy.cs#L125-L161","documentation":"BicepValueProxy.Convert<T> unwraps a stored value to a concrete literal of type T. If the underlying value is not an instance of T (nor convertible via the checked path), the method throws ArgumentException stating it expected a T literal or a BicepValueProxy. This guards callers against silently mistyping Bicep values.","triggerScenarios":"Calling Convert<T> (e.g. Convert<int>, Convert<string>) on a BicepValueProxy whose stored value was created from a different literal type — such as converting a string '8080' proxy to int, or an int proxy to string.","commonSituations":"Reading back a Bicep value with a mismatched generic argument; refactoring a value from string to int (or vice versa) in one place but not the read site; passing values parsed from config as strings into Convert<int>.","solutions":["Call Convert with the generic type matching the literal originally stored (check the producing code).","Parse/convert explicitly first: int.TryParse on strings before Convert<int>, or ToString() before Convert<string>.","Use nullable-aware checks on the proxy's value type before converting.","Centralize the expected type in one place so producer and consumer agree."],"exampleFix":"// before\nvar port = proxy.Convert<int>(); // stored as string \"8080\"\n\n// after\nvar port = int.Parse(proxy.Convert<string>());","handlingStrategy":"type-guard","validationCode":"if (proxy.ValueType == typeof(int)) { /* safe: proxy.Convert<int>() */ }","typeGuard":"bool CanConvert<T>(BicepValueProxy p) where T : notnull => p.ValueType == typeof(T) || Nullable.GetUnderlyingType(p.ValueType) == typeof(T);","tryCatchPattern":"try { var v = proxy.Convert<int>(); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"literal or BicepValueProxy\")) { /* read with the stored type and convert manually */ }","preventionTips":["Use the same generic type at the write site and the Convert read site.","Convert explicitly (int.Parse/ToString) instead of relying on Convert<T> to coerce.","Track Bicep value types in one definition to avoid write/read drift after refactors."],"tags":["azure","bicep","provisioning","type-mismatch"],"backgroundTag":"type-mismatch","analyzedSha":"25830f84bd145686607ad00c057b3f84e2e51d43","analyzedAt":"2026-09-16T11:10:06.193Z","contentChangedAt":"2026-09-16T11:10:06.193Z","schemaVersion":2},"datasetVersion":"2026-09-21T04:17:39.646Z"}