{"record":{"id":"aead0264bd0616ad","repo":"microsoft/aspire","slug":"expected-int32orstringv1-but-got-value-gettype","errorCode":null,"errorMessage":"Expected Int32OrStringV1 but got {value?.GetType()}","messagePattern":"Expected Int32OrStringV1 but got (.+?)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting.Kubernetes/Yaml/IntOrStringConverter.cs","lineNumber":60,"sourceCode":"        var value = scalar.Value;\n        parser.MoveNext();\n\n        return string.IsNullOrEmpty(value) ? null : new Int32OrStringV1(value);\n    }\n\n    /// <summary>\n    /// Writes the given object to the provided YAML emitter using the appropriate format.\n    /// </summary>\n    /// <param name=\"emitter\">The emitter used to write the YAML output.</param>\n    /// <param name=\"value\">The object to be serialized. Expected to be of type <see cref=\"Int32OrStringV1\"/>.</param>\n    /// <param name=\"type\">The type of the object being serialized.</param>\n    /// <param name=\"serializer\">The serializer to be used for complex object serialization.</param>\n    /// <exception cref=\"InvalidOperationException\">Thrown when the provided value is not of type <see cref=\"Int32OrStringV1\"/>.</exception>\n    public void WriteYaml(IEmitter emitter, object? value, Type type, ObjectSerializer serializer)\n    {\n        if (value is not Int32OrStringV1 obj)\n        {\n            throw new InvalidOperationException($\"Expected {nameof(Int32OrStringV1)} but got {value?.GetType()}\");\n        }\n\n        if (obj.Number != null)\n        {\n            serializer(obj.Number);\n        }\n        else\n        {\n            var val = obj.Value ?? string.Empty;\n            serializer(val);\n        }\n    }\n}\n","sourceCodeStart":42,"sourceCodeEnd":74,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.Kubernetes/Yaml/IntOrStringConverter.cs#L42-L74","documentation":"IntOrStringConverter.WriteYaml serializes Kubernetes Int32OrString values (quantities like ports or replicas that may be a number or a string). It throws InvalidOperationException when the value handed to the YAML serializer is not an Int32OrStringV1 instance. This is an internal invariant: the converter is registered for a specific type, so receiving anything else means the object graph or converter registration is wrong.","triggerScenarios":"Calling WriteYaml via YamlDotNet with the IntOrStringConverter registered for a type, but passing an object that is not Int32OrStringV1 (e.g. a raw int, string, or null) at src/Aspire.Hosting.Kubernetes/Yaml/IntOrStringConverter.cs:60.","commonSituations":"Custom code serializing a Kubernetes manifest graph where a property was assigned a plain int or string instead of Int32OrStringV1; manually invoking the converter against the wrong type; a custom resource type mimicking Int32OrStringV1 but using a different CLR type.","solutions":["Ensure every value assigned to a member serialized with IntOrStringConverter is an Int32OrStringV1 instance (wrap ints/strings with the Int32OrStringV1 implicit conversions or constructor).","Check that the converter is registered (IEventEmitters/IBetterYamlDotNet type resolution) only for typeof(Int32OrStringV1) and not applied to other types.","If the value may legitimately be absent, ensure it is null-handled upstream rather than passed as a foreign type; a null here yields value?.GetType() == null in the message, pointing to an untyped null path."],"exampleFix":"// before\nmanifest.Spec.Ports[0].TargetPort = 8080; // plain int in a member typed object\n// after\nmanifest.Spec.Ports[0].TargetPort = new Int32OrStringV1(8080);","handlingStrategy":"type-guard","validationCode":"if (value is not Int32OrStringV1) throw new ArgumentException($\"Expected Int32OrStringV1, got {value?.GetType().Name}\");","typeGuard":"static bool IsValidIntOrString(object? v) => v is Int32OrStringV1;","tryCatchPattern":"try { serializer.Serialize(writer, value); } catch (InvalidOperationException ex) when (ex.Message.Contains(\"Int32OrStringV1\")) { /* fix object graph type */ }","preventionTips":["Always assign Int32OrStringV1 (or use its implicit conversions) to fields serialized by this converter","Keep converter registrations scoped to typeof(Int32OrStringV1)","Add unit tests that serialize manifests end-to-end to catch type drift"],"tags":["yaml","serialization","kubernetes","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"}