From 3811784a98e3d9a33d99c6db4e923fefb532d671 Mon Sep 17 00:00:00 2001 From: Thomas Van Lenten Date: Fri, 16 Apr 2021 18:12:47 -0400 Subject: [PATCH] Only trigger conflicting oneof if a value is decoded. This lets things like JSON that can have `null` as the value not count as setting the field and thus not causing the conflict per the new conformance test. Fixes #1133 --- Sources/protoc-gen-swift/OneofGenerator.swift | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/Sources/protoc-gen-swift/OneofGenerator.swift b/Sources/protoc-gen-swift/OneofGenerator.swift index 677450248..9e217f2e7 100644 --- a/Sources/protoc-gen-swift/OneofGenerator.swift +++ b/Sources/protoc-gen-swift/OneofGenerator.swift @@ -366,27 +366,35 @@ class OneofGenerator { p.print("case \(field.number): try {\n") p.indent() + let hadValueTest: String if field.isGroupOrMessage { // Messages need to fetch the current value so new fields are merged into the existing // value p.print( "var v: \(field.swiftType)?\n", + "var hadOneofValue = false\n", "if let current = \(storedProperty) {\n") p.indent() p.print( - "try decoder.handleConflictingOneOf()\n", + "hadOneofValue = true\n", "if case \(field.dottedSwiftName)(let m) = current {v = m}\n") p.outdent() p.print("}\n") + hadValueTest = "hadOneofValue" } else { - p.print( - "if \(storedProperty) != nil {try decoder.handleConflictingOneOf()}\n", - "var v: \(field.swiftType)?\n") + p.print("var v: \(field.swiftType)?\n") + hadValueTest = "\(storedProperty) != nil" } p.print( "try decoder.decodeSingular\(field.protoGenericType)Field(value: &v)\n", - "if let v = v {\(storedProperty) = \(field.dottedSwiftName)(v)}\n") + "if let v = v {\n") + p.indent() + p.print( + "if \(hadValueTest) {try decoder.handleConflictingOneOf()}\n", + "\(storedProperty) = \(field.dottedSwiftName)(v)\n") + p.outdent() + p.print("}\n") p.outdent() p.print("}()\n") }