Skip to content

Commit

Permalink
Merge pull request #3151 from square/cycle_bug
Browse files Browse the repository at this point in the history
[swift] Fix not marking oneof field as `@Indirect` when detecting a cycle
  • Loading branch information
dnkoutso authored Oct 17, 2024
2 parents b726bcc + 976e1de commit 633cf04
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/
@propertyWrapper
public enum Indirect<Value : ProtoCodable> {
public enum Indirect<Value> {
// 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
Expand Down
11 changes: 11 additions & 0 deletions wire-runtime-swift/src/test/proto/cycles.proto
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
34 changes: 34 additions & 0 deletions wire-runtime-swift/src/test/swift/CyclesTests.swift
Original file line number Diff line number Diff line change
@@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
)
Expand Down

0 comments on commit 633cf04

Please sign in to comment.