{"record":{"id":"3ead103a3400a880","repo":"XINCGer/Unity3DTraining","slug":"map-values-must-not-be-null","errorCode":null,"errorMessage":"Map values must not be null","messagePattern":"Map values must not be null","errorType":"validation","errorClass":"InvalidProtocolBufferException","httpStatus":null,"severity":"error","filePath":"NetWorkAndResources/Socket_Protobuff/Assets/Plugins/Google.Protobuf/JsonParser.cs","lineNumber":300,"sourceCode":"            var valueField = type.FindFieldByNumber(2);\n            if (keyField == null || valueField == null)\n            {\n                throw new InvalidProtocolBufferException(\"Invalid map field: \" + field.FullName);\n            }\n            IDictionary dictionary = (IDictionary)field.Accessor.GetValue(message);\n\n            while (true)\n            {\n                token = tokenizer.Next();\n                if (token.Type == JsonToken.TokenType.EndObject)\n                {\n                    return;\n                }\n                object key = ParseMapKey(keyField, token.StringValue);\n                object value = ParseSingleValue(valueField, tokenizer);\n                if (value == null)\n                {\n                    throw new InvalidProtocolBufferException(\"Map values must not be null\");\n                }\n                dictionary[key] = value;\n            }\n        }\n\n        private static bool IsGoogleProtobufValueField(FieldDescriptor field)\n        {\n            return field.FieldType == FieldType.Message &&\n                field.MessageType.FullName == Value.Descriptor.FullName;\n        }\n\n        private object ParseSingleValue(FieldDescriptor field, JsonTokenizer tokenizer)\n        {\n            var token = tokenizer.Next();\n            if (token.Type == JsonToken.TokenType.Null)\n            {\n                // TODO: In order to support dynamic messages, we should really build this up\n                // dynamically.","sourceCodeStart":282,"sourceCodeEnd":318,"githubUrl":"https://github.com/XINCGer/Unity3DTraining/blob/016f98412ea5e11756b286736f479b66ab6216d5/NetWorkAndResources/Socket_Protobuff/Assets/Plugins/Google.Protobuf/JsonParser.cs#L282-L318","documentation":"Protobuf map entries cannot have null values: every map entry stores a concrete key and value. When parsing a map field's JSON, if the value token is JSON null (even for message-typed values, where null could conceptually mean an empty message), MergeMapField rejects it with this InvalidProtocolBufferException.","triggerScenarios":"Calling JsonParser.Parse with JSON like {\"myMap\": {\"key1\": null}} — a null as the value of any map entry, regardless of the value type.","commonSituations":"JSON serialized from dictionaries that contained null values (common in C#/Java maps with nullable reference values); frontend code emitting null for absent values; interop with systems that allow null map values (e.g. OpenAPI-style maps) before feeding protobuf JSON parsing.","solutions":["Remove null entries from the map JSON before parsing — an entry whose value is null should either be omitted entirely or given a default/empty value.","Replace null with the default value for the value type (empty string, 0, empty message object like {}).","Sanitize dictionaries before serialization: filter out entries with null values before emitting JSON.","If nulls are meaningful, change the schema to wrap the value in a message with an optional field (e.g. google.protobuf.Value or a wrapper message)."],"exampleFix":"// before\n{\"myMap\": {\"a\": null, \"b\": 2}}\n\n// after: omit or default the null entry\n{\"myMap\": {\"b\": 2}}   // or {\"myMap\": {\"a\": \"\", \"b\": 2}}","handlingStrategy":"validation","validationCode":"// Strip null-valued map entries before parsing\nfunction cleanMap(m) {\n  return Object.fromEntries(Object.entries(m).filter(([, v]) => v !== null));\n}\nif (payload.myMap) payload.myMap = cleanMap(payload.myMap);","typeGuard":null,"tryCatchPattern":"try { var msg = JsonParser.Default.Parse<T>(json); }\ncatch (InvalidProtocolBufferException ex) when (ex.Message == \"Map values must not be null\") { /* sanitize payload and retry once */ }","preventionTips":["Filter null values out of dictionaries before serializing to protobuf JSON","Treat 'absent key' as the protobuf equivalent of null","If nulls are meaningful, use wrapper messages with optional fields in the schema"],"tags":["protobuf","json-parsing","map-field","null-value"],"backgroundTag":"null-argument","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"}