Skip to content

Commit 702679b

Browse files
author
Pouya Yarandi
committed
Fix issue of json path names
1 parent 35163e1 commit 702679b

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

Sources/SwiftProtobuf/NameMap.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,6 @@ public struct _NameMap: ExpressibleByDictionaryLiteral {
281281

282282
/// Returns all proto names
283283
internal var names: [Name] {
284-
protoToNumberMap.map(\.key)
284+
numberToNameMap.map(\.value.proto)
285285
}
286286
}

Sources/SwiftProtobuf/PathDecoder.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,15 @@ extension Message {
3535
guard let type = Self.self as? any _ProtoNameProviding.Type else {
3636
return nil
3737
}
38-
return Array(field.utf8).withUnsafeBytes { bytes in
38+
guard let number = Array(field.utf8).withUnsafeBytes({ bytes in
3939
type._protobuf_nameMap.number(forProtoName: bytes)
40+
}) else {
41+
return nil
42+
}
43+
if type._protobuf_nameMap.names(for: number)?.proto.description != field {
44+
return nil
4045
}
46+
return number
4147
}
4248

4349
static func name(for field: Int) -> String? {

Tests/SwiftProtobufTests/Test_FieldMask.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,15 @@ final class Test_FieldMask: XCTestCase, PBTestHelpers {
334334
XCTAssertThrowsError(try Google_Protobuf_FieldMask(fieldNumbers: [10], of: SwiftProtoTesting_TestAny.self))
335335
}
336336

337+
// Checks that json names of paths should not be contained in mask field with allFieldsOf init.
338+
func testFieldMaskAllPathsWithUniqueName() {
339+
let mask = Google_Protobuf_FieldMask(allFieldsOf: SwiftProtoTesting_Fuzz_Message.self)
340+
// proto name is included
341+
XCTAssertTrue(mask.paths.contains("SingularGroup"))
342+
// json name is not included
343+
XCTAssertFalse(mask.paths.contains("singulargroup"))
344+
}
345+
337346
// Checks `union` func of fieldMask.
338347
func testUnionFieldMasks() throws {
339348
let m1 = Google_Protobuf_FieldMask(protoPaths: ["a", "b"])
@@ -787,4 +796,21 @@ final class Test_FieldMask: XCTestCase, PBTestHelpers {
787796
try m1.merge(with: m2, fieldMask: .init(protoPaths: ["SingularGroup.group_field"]))
788797
XCTAssertEqual(m1.singularGroup.groupField, m2.singularGroup.groupField)
789798
}
799+
800+
// Checks that merging with json path should do nothing. Path should only be merged using proto names.
801+
func testMergeFieldWithJSONName() throws {
802+
var m1 = SwiftProtoTesting_Fuzz_Message()
803+
let m2 = SwiftProtoTesting_Fuzz_Message.with { m in
804+
m.singularGroup = .with { $0.groupField = 1 }
805+
}
806+
// should do nothing with json path (should not merge)
807+
try m1.merge(with: m2, fieldMask: .with({ $0.paths = ["singulargroup"] }))
808+
XCTAssertNotEqual(m1.singularGroup, m2.singularGroup)
809+
// should merge with proto path
810+
try m1.merge(with: m2, fieldMask: .with({ $0.paths = ["SingularGroup"] }))
811+
XCTAssertEqual(m1.singularGroup, m2.singularGroup)
812+
// should do nothing with json path (do not clear field)
813+
try m1.merge(with: m2, fieldMask: .with({ $0.paths = ["singulargroup"] }))
814+
XCTAssertEqual(m1.singularGroup, m2.singularGroup)
815+
}
790816
}

0 commit comments

Comments
 (0)