{"record":{"id":"d6b9cf40c30ee0ab","repo":"XINCGer/Unity3DTraining","slug":"invalid-enum-value","errorCode":null,"errorMessage":"Invalid enum value: ","messagePattern":"Invalid enum value: ","errorType":"validation","errorClass":"InvalidProtocolBufferException","httpStatus":null,"severity":"error","filePath":"NetWorkAndResources/Socket_Protobuff/Assets/Plugins/Google.Protobuf/JsonParser.cs","lineNumber":716,"sourceCode":"                case FieldType.SInt64:\n                case FieldType.SFixed64:\n                    return ParseNumericString<long>(text, long.Parse);\n                case FieldType.UInt64:\n                case FieldType.Fixed64:\n                    return ParseNumericString<ulong>(text, ulong.Parse);\n                case FieldType.Double:\n                    double d = ParseNumericString<double>(text, double.Parse);\n                    ValidateInfinityAndNan(text, double.IsPositiveInfinity(d), double.IsNegativeInfinity(d), double.IsNaN(d));\n                    return d;\n                case FieldType.Float:\n                    float f = ParseNumericString<float>(text, float.Parse);\n                    ValidateInfinityAndNan(text, float.IsPositiveInfinity(f), float.IsNegativeInfinity(f), float.IsNaN(f));\n                    return f;\n                case FieldType.Enum:\n                    var enumValue = field.EnumType.FindValueByName(text);\n                    if (enumValue == null)\n                    {\n                        throw new InvalidProtocolBufferException(\"Invalid enum value: \" + text + \" for enum type: \" + field.EnumType.FullName);\n                    }\n                    // Just return it as an int, and let the CLR convert it.\n                    return enumValue.Number;\n                default:\n                    throw new InvalidProtocolBufferException(\"Unsupported conversion from JSON string for field type \" + field.FieldType);\n            }\n        }\n\n        /// <summary>\n        /// Creates a new instance of the message type for the given field.\n        /// </summary>\n        private static IMessage NewMessageForField(FieldDescriptor field)\n        {\n            return field.MessageType.Parser.CreateTemplate();\n        }\n\n        private static T ParseNumericString<T>(string text, Func<string, NumberStyles, IFormatProvider, T> parser)\n        {","sourceCodeStart":698,"sourceCodeEnd":734,"githubUrl":"https://github.com/XINCGer/Unity3DTraining/blob/016f98412ea5e11756b286736f479b66ab6216d5/NetWorkAndResources/Socket_Protobuff/Assets/Plugins/Google.Protobuf/JsonParser.cs#L698-L734","documentation":"Google.Protobuf's JsonParser throws InvalidProtocolBufferException when converting JSON to a protobuf message. In ParseSingleStringValue, a JSON string field whose FieldType is Enum is resolved via the enum descriptor's FindValueByName; if the string is not a declared enum value name, this error is thrown. The protobuf JSON spec requires enum values in JSON to be the value name (e.g. \"VALUE_A\"), not an alias or arbitrary text.","triggerScenarios":"Calling JsonParser.Parse<T>(json) (directly or via JsonFormatter/Parse on a message containing enum fields) where a string value for an enum-typed field does not exactly match a registered enum value name, e.g. sending \"ACTIVE\" when the proto declares only STATUS_ACTIVE, or sending a numeric enum as a string like \"2\" when no alias named \"2\" exists.","commonSituations":"Older/newer .proto versions between client and server so the enum name was renamed or removed; hand-written JSON using numeric or ad-hoc strings for enums; code generators out of sync after regenerating C# stubs; typos or case mismatches (JSON enum names are case-sensitive here).","solutions":["Use the exact enum value name declared in the .proto file for the field's enum type","Check field.EnumType.FullName's declared values (e.g. dump the EnumDescriptor values) and match the JSON string to one of them","If you must send a number, use a JSON number instead of a string so the parser takes the numeric path","Regenerate the C# protobuf classes from the current .proto so descriptors contain the value names you send","Add the missing value or an allow_alias entry to the proto enum if the name is legitimately new"],"exampleFix":"// before\nvar msg = MessageParser.Parse('{\"state\": \"2\"}'); // throws: no enum value named \"2\"\n// after\nvar msg = MessageParser.Parse('{\"state\": \"STATE_INACTIVE\"}'); // matches declared proto value","handlingStrategy":"validation","validationCode":"bool IsValidEnumJson(string json, MessageDescriptor d) {\n    // For each enum field, check the string against EnumType.FindValueByName before parsing.\n    foreach (var f in d.Fields.InFieldNumberOrder())\n        if (f.FieldType == FieldType.Enum && json.Contains(\"\\\"\" + f.JsonName + \"\\\": \\\"\"))\n            if (f.EnumType.FindValueByName(ExtractEnumString(json, f.JsonName)) == null) return false;\n    return true;\n}","typeGuard":"bool IsKnownEnumValue(string text, EnumDescriptor e) => e.FindValueByName(text) != null;","tryCatchPattern":"try { return JsonParser.Default.Parse<T>(json); }\ncatch (InvalidProtocolBufferException ex) when (ex.Message.StartsWith(\"Invalid enum value\")) {\n    logger.LogWarning(ex, \"Unknown enum name in JSON\");\n    return null; // or repair/migrate the payload\n}","preventionTips":["Keep proto enums stable; never rename values without a compatibility plan","Match enum strings exactly — they are case-sensitive","Send enum values by their declared proto name, not numerics or ad-hoc strings","Regenerate C# stubs whenever the .proto changes"],"tags":["protobuf","json","enum","deserialization"],"backgroundTag":"invalid-enum-value","analyzedSha":"016f98412ea5e11756b286736f479b66ab6216d5","analyzedAt":"2026-09-12T01:08:58.711Z","contentChangedAt":"2026-09-12T01:08:58.711Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}