{"record":{"id":"1ba00349822c1dd9","repo":"XINCGer/Unity3DTraining","slug":"repeated-field-value-was-not-an-array-token-type","errorCode":null,"errorMessage":"Repeated field value was not an array. Token type: ","messagePattern":"Repeated field value was not an array\\. Token type: ","errorType":"validation","errorClass":"InvalidProtocolBufferException","httpStatus":null,"severity":"error","filePath":"NetWorkAndResources/Socket_Protobuff/Assets/Plugins/Google.Protobuf/JsonParser.cs","lineNumber":251,"sourceCode":"                MergeMapField(message, field, tokenizer);\n            }\n            else if (field.IsRepeated)\n            {\n                MergeRepeatedField(message, field, tokenizer);\n            }\n            else\n            {\n                var value = ParseSingleValue(field, tokenizer);\n                field.Accessor.SetValue(message, value);\n            }\n        }\n\n        private void MergeRepeatedField(IMessage message, FieldDescriptor field, JsonTokenizer tokenizer)\n        {\n            var token = tokenizer.Next();\n            if (token.Type != JsonToken.TokenType.StartArray)\n            {\n                throw new InvalidProtocolBufferException(\"Repeated field value was not an array. Token type: \" + token.Type);\n            }\n\n            IList list = (IList)field.Accessor.GetValue(message);\n            while (true)\n            {\n                token = tokenizer.Next();\n                if (token.Type == JsonToken.TokenType.EndArray)\n                {\n                    return;\n                }\n                tokenizer.PushBack(token);\n                if (token.Type == JsonToken.TokenType.Null)\n                {\n                    throw new InvalidProtocolBufferException(\"Repeated field elements cannot be null\");\n                }\n                list.Add(ParseSingleValue(field, tokenizer));\n            }\n        }","sourceCodeStart":233,"sourceCodeEnd":269,"githubUrl":"https://github.com/XINCGer/Unity3DTraining/blob/016f98412ea5e11756b286736f479b66ab6216d5/NetWorkAndResources/Socket_Protobuff/Assets/Plugins/Google.Protobuf/JsonParser.cs#L233-L269","documentation":"For a repeated proto field, canonical JSON requires the value to be an array. JsonParser.MergeRepeatedField reads the next token and throws InvalidProtocolBufferException if it is not a start-array token, reporting the actual token type encountered.","triggerScenarios":"Parsing JSON where a repeated field is given a single scalar instead of an array — e.g. {\"tags\": \"a\"} instead of {\"tags\": [\"a\"]}; clients built against an older non-canonical serialization; hand-written JSON omitting the brackets.","commonSituations":"Hand-crafted test fixtures; JavaScript clients assigning a single value instead of pushing to an array; JSON produced by non-canonical serializers; proto field changed from singular to repeated and old senders not updated.","solutions":["Wrap the value in a JSON array: change \"field\": value to \"field\": [value]","Update the producer so repeated fields are always serialized as arrays (canonical proto JSON)","Accept and normalize non-array values in your own pre-processing layer before calling JsonParser","If the field was recently made repeated, version the API or keep a singular field temporarily"],"exampleFix":"// before\n{ \"tags\": \"prod\" }\n// after\n{ \"tags\": [\"prod\"] }","handlingStrategy":"validation","validationCode":"// Ensure repeated fields are arrays before parsing\nstatic bool IsArrayValue(string json, string repeatedFieldName) { using var doc = System.Text.Json.JsonDocument.Parse(json); return !doc.RootElement.TryGetProperty(repeatedFieldName, out var v) || v.ValueKind == System.Text.Json.JsonValueKind.Array; }","typeGuard":null,"tryCatchPattern":"try { return parser.Parse<T>(json); } catch (InvalidProtocolBufferException ex) when (ex.Message.StartsWith(\"Repeated field value was not an array\")) { log.Error(\"Repeated field given a scalar instead of an array\"); throw new BadRequestException(ex.Message, ex); }","preventionTips":["Always wrap repeated-field values in JSON arrays","Fix producers that emit singular values for repeated fields","Normalize singular-to-array at an adapter layer for legacy clients"],"tags":["protobuf","json-parsing","repeated-field","expected-array","schema-mismatch"],"backgroundTag":"unexpected-response-shape","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"}