From 976e1de8132700b50136a042e94828a7f59dd2bf Mon Sep 17 00:00:00 2001 From: Dimitris Koutsogiorgas Date: Thu, 17 Oct 2024 18:31:55 +0300 Subject: [PATCH] [swift] Fix not marking one of field as Indirect when detecting a cycle --- .../swift/propertyWrappers/Indirect.swift | 2 +- .../src/test/proto/cycles.proto | 11 ++++++ .../src/test/swift/CyclesTests.swift | 34 +++++++++++++++++++ .../com/squareup/wire/swift/SwiftGenerator.kt | 3 ++ 4 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 wire-runtime-swift/src/test/swift/CyclesTests.swift diff --git a/wire-runtime-swift/src/main/swift/propertyWrappers/Indirect.swift b/wire-runtime-swift/src/main/swift/propertyWrappers/Indirect.swift index 12e6412a2b..486200b857 100644 --- a/wire-runtime-swift/src/main/swift/propertyWrappers/Indirect.swift +++ b/wire-runtime-swift/src/main/swift/propertyWrappers/Indirect.swift @@ -14,7 +14,7 @@ * limitations under the License. */ @propertyWrapper -public enum Indirect { +public enum Indirect { // Dedicated .none case for nil means the runtime size of this case is equal to a single // pointer rather than the two for the .some case. case none diff --git a/wire-runtime-swift/src/test/proto/cycles.proto b/wire-runtime-swift/src/test/proto/cycles.proto index 9fe21d621f..e23762bd1b 100644 --- a/wire-runtime-swift/src/test/proto/cycles.proto +++ b/wire-runtime-swift/src/test/proto/cycles.proto @@ -37,3 +37,14 @@ message C { message D { optional A a = 1; } + +message F { + oneof action { + G g = 1; + string s = 2; + } +} + +message G { + optional F f = 1; +} diff --git a/wire-runtime-swift/src/test/swift/CyclesTests.swift b/wire-runtime-swift/src/test/swift/CyclesTests.swift new file mode 100644 index 0000000000..74915ee3bb --- /dev/null +++ b/wire-runtime-swift/src/test/swift/CyclesTests.swift @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2024 Square, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import Foundation +import Wire +import XCTest + +final class CyclesTests: XCTestCase { + func testOneOfFieldCycle() throws { + let f = F { + $0.action = .g(G()) + } + + let encoder = ProtoEncoder() + let data = try encoder.encode(f) + + let decoder = ProtoDecoder() + let decodedMessage = try decoder.decode(F.self, from: data) + + XCTAssertEqual(decodedMessage, f) + } +} diff --git a/wire-swift-generator/src/main/java/com/squareup/wire/swift/SwiftGenerator.kt b/wire-swift-generator/src/main/java/com/squareup/wire/swift/SwiftGenerator.kt index 665a68d88e..b6b1ca564f 100644 --- a/wire-swift-generator/src/main/java/com/squareup/wire/swift/SwiftGenerator.kt +++ b/wire-swift-generator/src/main/java/com/squareup/wire/swift/SwiftGenerator.kt @@ -1363,6 +1363,9 @@ class SwiftGenerator private constructor( if (oneOf.documentation.isNotBlank()) { addDoc("%N\n", oneOf.documentation.sanitizeDoc()) } + if (oneOf.fields.any { oneOfField -> isIndirect(type, oneOfField) }) { + addAttribute(AttributeSpec.builder(indirect).build()) + } } .build(), )