{"record":{"id":"acaf5245ea3acfb7","repo":"XINCGer/Unity3DTraining","slug":"repeated-field-elements-cannot-be-null","errorCode":null,"errorMessage":"Repeated field elements cannot be null","messagePattern":"Repeated field elements cannot be null","errorType":"validation","errorClass":"InvalidProtocolBufferException","httpStatus":null,"severity":"error","filePath":"NetWorkAndResources/Socket_Protobuff/Assets/Plugins/Google.Protobuf/JsonParser.cs","lineNumber":265,"sourceCode":"        {\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        }\n\n        private void MergeMapField(IMessage message, FieldDescriptor field, JsonTokenizer tokenizer)\n        {\n            // Map fields are always objects, even if the values are well-known types: ParseSingleValue handles those.\n            var token = tokenizer.Next();\n            if (token.Type != JsonToken.TokenType.StartObject)\n            {\n                throw new InvalidProtocolBufferException(\"Expected an object to populate a map\");\n            }\n\n            var type = field.MessageType;\n            var keyField = type.FindFieldByNumber(1);\n            var valueField = type.FindFieldByNumber(2);\n            if (keyField == null || valueField == null)","sourceCodeStart":247,"sourceCodeEnd":283,"githubUrl":"https://github.com/XINCGer/Unity3DTraining/blob/016f98412ea5e11756b286736f479b66ab6216d5/NetWorkAndResources/Socket_Protobuff/Assets/Plugins/Google.Protobuf/JsonParser.cs#L247-L283","documentation":"Canonical proto3 JSON forbids null inside repeated fields (null is only allowed for null-able value fields like google.protobuf.Value or wrappers at the element position of singular fields... but repeated elements must be concrete values). JsonParser stops array parsing on an EndArray token; if the next token is Null, it throws InvalidProtocolBufferException.","triggerScenarios":"Parsing JSON like {\"items\": [1, null, 3]} for a repeated int32/string/message field; clients serializing empty/absent values as null inside arrays; generic serializers mapping undefined list entries to null.","commonSituations":"JavaScript/Python code building arrays with undefined/None entries; JSON produced from sparse data structures; missing-field semantics collapsed into null by an intermediate layer.","solutions":["Remove null entries from the array before parsing — omit them or use a wrapper type (google.protobuf.Int32Value etc.) if null must be representable","Fix the producer to skip null/undefined elements when building the JSON array","Pre-filter nulls in a normalization step before calling JsonParser for untrusted input","If nullable elements are genuinely required, change the field to repeated wrapper/Value type"],"exampleFix":"// before\n{ \"ids\": [1, null, 3] }\n// after\n{ \"ids\": [1, 3] }  // or repeated google.protobuf.Int32Value to allow null","handlingStrategy":"validation","validationCode":"// Strip null entries from arrays before parsing\nstatic System.Text.Json.JsonElement DropNullArrayElements(System.Text.Json.JsonElement arr) => arr; // filter with: arr.EnumerateArray().Where(e => e.ValueKind != JsonValueKind.Null)","typeGuard":null,"tryCatchPattern":"try { return parser.Parse<T>(json); } catch (InvalidProtocolBufferException ex) when (ex.Message == \"Repeated field elements cannot be null\") { log.Error(\"null element inside a repeated field\"); throw new BadRequestException(ex.Message, ex); }","preventionTips":["Skip null/undefined elements when building arrays client-side","Use wrapper types (google.protobuf.*Value) if null elements are required","Filter nulls from untrusted JSON before JsonParser"],"tags":["protobuf","json-parsing","null-value","repeated-field","schema-validation"],"backgroundTag":"protobuf-unmarshal-failed","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"}