{"record":{"id":"9a5524a99329e21d","repo":"XINCGer/Unity3DTraining","slug":"multiple-values-specified-for-oneof","errorCode":null,"errorMessage":"Multiple values specified for oneof ","messagePattern":"Multiple values specified for oneof ","errorType":"validation","errorClass":"InvalidProtocolBufferException","httpStatus":null,"severity":"error","filePath":"NetWorkAndResources/Socket_Protobuff/Assets/Plugins/Google.Protobuf/JsonParser.cs","lineNumber":199,"sourceCode":"                    return;\n                }\n                if (token.Type != JsonToken.TokenType.Name)\n                {\n                    throw new InvalidOperationException(\"Unexpected token type \" + token.Type);\n                }\n                string name = token.StringValue;\n                FieldDescriptor field;\n                if (jsonFieldMap.TryGetValue(name, out field))\n                {\n                    if (field.ContainingOneof != null)\n                    {\n                        if (seenOneofs == null)\n                        {\n                            seenOneofs = new HashSet<OneofDescriptor>();\n                        }\n                        if (!seenOneofs.Add(field.ContainingOneof))\n                        {\n                            throw new InvalidProtocolBufferException(\"Multiple values specified for oneof \" + field.ContainingOneof.Name);\n                        }\n                    }\n                    MergeField(message, field, tokenizer);\n                }\n                else\n                {\n                    // TODO: Is this what we want to do? If not, we'll need to skip the value,\n                    // which may be an object or array. (We might want to put code in the tokenizer\n                    // to do that.)\n                    throw new InvalidProtocolBufferException(\"Unknown field: \" + name);\n                }\n            }\n        }\n\n        private void MergeField(IMessage message, FieldDescriptor field, JsonTokenizer tokenizer)\n        {\n            var token = tokenizer.Next();\n            if (token.Type == JsonToken.TokenType.Null)","sourceCodeStart":181,"sourceCodeEnd":217,"githubUrl":"https://github.com/XINCGer/Unity3DTraining/blob/016f98412ea5e11756b286736f479b66ab6216d5/NetWorkAndResources/Socket_Protobuff/Assets/Plugins/Google.Protobuf/JsonParser.cs#L181-L217","documentation":"Proto3 oneof fields may have at most one member set. When JSON contains values for two or more members of the same oneof, JsonParser.Merge detects the second assignment via its seenOneofs set and throws InvalidProtocolBufferException naming the oneof.","triggerScenarios":"Parsing JSON like {\"fieldA\": 1, \"fieldB\": 2} where fieldA and fieldB belong to the same oneof; merging two JSON patches that each set a different member of the same oneof; schema evolution where a field moved into a oneof and older clients still send the old sibling field.","commonSituations":"Clients unaware a field was moved into a oneof after a proto refactor; JSON merge/patch operations combining documents; manually written JSON setting several variants at once.","solutions":["Send exactly one member of the oneof in the JSON; remove the conflicting key","Check the .proto definition to see which fields share the oneof and update the client","If migrating, keep old fields out of oneofs or version the message schema","Pre-validate the JSON (count oneof member keys) before calling Parse when accepting external input"],"exampleFix":"// before\n{ \"amountCents\": 100, \"amountEuros\": 90 }   // both in oneof 'amount'\n// after\n{ \"amountCents\": 100 }","handlingStrategy":"validation","validationCode":"// Reject JSON objects that set more than one member of any oneof (adjust names per schema)\nstatic bool SetsMultipleOneofMembers(System.Text.Json.JsonElement obj, string[] oneofMembers) => oneofMembers.Count(m => obj.TryGetProperty(m, out _)) > 1;","typeGuard":null,"tryCatchPattern":"try { return parser.Parse<T>(json); } catch (InvalidProtocolBufferException ex) when (ex.Message.StartsWith(\"Multiple values specified for oneof\")) { log.Error(\"JSON sets multiple members of a oneof\"); throw new BadRequestException(ex.Message, ex); }","preventionTips":["Document oneof semantics for API clients; send exactly one member","When moving fields into a oneof, version the API and inform senders","Reject conflicting keys at your API gateway before parsing"],"tags":["protobuf","json-parsing","oneof","conflicting-fields","schema-validation"],"backgroundTag":"schema-validation-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"}