Skip to content

Commit 1843c35

Browse files
committed
test: add ToProto/FromProto canary
Adds a compile-time canary crate, tests/convert-canary, for crate versions already published on crates.io. Their generated code calls gaxi::prost::ToProto::to_proto and FromProto::cnv without naming the target type, so it only compiles while each source type has exactly one impl. A new gax-internal 0.7.x release that adds a second impl would break them for users. The canary checks three cases: - External model types that published crates convert themselves (e.g. google_cloud_type::model::Date in storage 1.21.0). - Types gax-internal converts with ToProto (well-known types and primitives). - Prost types gax-internal converts with FromProto, using the same setter shape as the published code. The crate keeps minimal dependencies on purpose. Depending on a crate that legitimately converts these types would make it fail. For googleapis#7284
1 parent 5544fcd commit 1843c35

4 files changed

Lines changed: 284 additions & 0 deletions

File tree

‎Cargo.lock‎

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Cargo.toml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ default-members = [
4848
"tests/auto-populated",
4949
"tests/bigquery",
5050
"tests/bigtable",
51+
"tests/convert-canary",
5152
"tests/default-idempotency",
5253
"tests/discovery",
5354
"tests/dns",
@@ -371,6 +372,7 @@ members = [
371372
"tests/auto-populated",
372373
"tests/bigquery",
373374
"tests/bigtable",
375+
"tests/convert-canary",
374376
"tests/crypto-providers/auth-with-default",
375377
"tests/crypto-providers/auth-without-default",
376378
"tests/crypto-providers/gaxi-with-aws-lc-rs",

‎tests/convert-canary/Cargo.toml‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Copyright 2026 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
[package]
16+
description = "Detects ToProto/FromProto changes that would break published crates."
17+
name = "integration-tests-convert-canary"
18+
publish = false
19+
version = "0.0.0"
20+
edition.workspace = true
21+
license.workspace = true
22+
23+
# Keep these dependencies minimal. Do not add any crate that implements
24+
# `ToProto` or `FromProto` for the types checked here (e.g. storage, logging-v2,
25+
# aiplatform-v1). Their impls are legitimate and would make the canary fail.
26+
[dev-dependencies]
27+
bytes.workspace = true
28+
gaxi = { workspace = true, features = ["_internal-grpc-client"] }
29+
google-cloud-api.workspace = true
30+
google-cloud-iam-v1 = { workspace = true, features = ["default"] }
31+
google-cloud-logging-type.workspace = true
32+
google-cloud-longrunning = { workspace = true, features = ["default"] }
33+
google-cloud-rpc.workspace = true
34+
google-cloud-type.workspace = true
35+
google-cloud-wkt.workspace = true
36+
prost-types.workspace = true
37+
38+
[lints]
39+
workspace = true

‎tests/convert-canary/src/lib.rs‎

Lines changed: 227 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,227 @@
1+
// Copyright 2026 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
//! Compile-time canary for `gaxi::prost::ToProto` and `FromProto`.
16+
//!
17+
//! Crate versions already published on crates.io call `.to_proto()` and
18+
//! `.cnv()` without naming the target type. They compile only while each
19+
//! source type has exactly one matching impl. Applications pick up new
20+
//! `google-cloud-gax-internal` 0.7.x releases automatically, so adding a second
21+
//! impl upstream (in `gaxi`, or in a crate that defines a shared model type)
22+
//! would break those published versions.
23+
//!
24+
//! The tests in this crate mirror the published call sites. They fail to
25+
//! compile (`E0283` or `E0284`, "type annotations needed") if such an impl is
26+
//! added.
27+
//!
28+
//! Keep the dependencies minimal. Do not depend on any crate that implements
29+
//! `ToProto` or `FromProto` for the types checked here (e.g. storage,
30+
//! logging-v2, aiplatform-v1). Those impls are legitimate, and depending on
31+
//! them would make this canary fail.
32+
//!
33+
//! See <https://github.com/googleapis/google-cloud-rust/issues/7284>.
34+
35+
/// External model types that published crates convert with their own
36+
/// `ToProto` impl.
37+
///
38+
/// For example, aiplatform-v1 1.18.0 implements `ToProto` for
39+
/// `google_cloud_rpc::model::Status`, targeting its own prost type, and calls
40+
/// `.to_proto()` on it unqualified. Each type gets a local
41+
/// `ToProto<LocalProst>` impl, which stands in for the published one. If an
42+
/// upstream crate adds another impl, the unqualified call below no longer
43+
/// compiles.
44+
///
45+
/// This list is frozen. Code generated after #7374 qualifies these calls, so
46+
/// only crate versions published before that release need coverage.
47+
#[cfg(test)]
48+
mod external_model_to_proto {
49+
use gaxi::prost::{ConvertError, ToProto};
50+
51+
struct LocalProst;
52+
53+
macro_rules! assert_single_upstream_impl {
54+
($($ty:ty),* $(,)?) => {
55+
$(
56+
impl ToProto<LocalProst> for $ty {
57+
type Output = LocalProst;
58+
fn to_proto(self) -> Result<LocalProst, ConvertError> {
59+
Ok(LocalProst)
60+
}
61+
}
62+
)*
63+
64+
#[test]
65+
fn no_upstream_impl() -> Result<(), ConvertError> {
66+
$(
67+
let _: LocalProst = <$ty>::default().to_proto()?;
68+
)*
69+
Ok(())
70+
}
71+
};
72+
}
73+
74+
assert_single_upstream_impl!(
75+
// Converted in aiplatform-v1 1.18.0, apigeeconnect-v1 1.12.0,
76+
// dialogflow-cx-v3 2.11.0, dialogflow-v2 1.18.0,
77+
// google-maps-routes-v1 1.1.0, and google-maps-routing-v2 1.2.0.
78+
google_cloud_rpc::model::Status,
79+
// Converted in storage 1.21.0 and aiplatform-v1 1.18.0.
80+
google_cloud_type::model::Date,
81+
// Converted in storage 1.21.0.
82+
google_cloud_type::model::Expr,
83+
// Converted in storage 1.21.0 and datacatalog-lineage-v1 1.15.0.
84+
google_cloud_type::model::Interval,
85+
// Converted in aiplatform-v1 1.18.0, dialogflow-cx-v3 2.11.0,
86+
// dialogflow-v2 1.18.0, google-maps-routes-v1 1.1.0, and
87+
// google-maps-routing-v2 1.2.0.
88+
google_cloud_type::model::LatLng,
89+
// Converted in google-maps-routing-v2 1.2.0.
90+
google_cloud_type::model::LocalizedText,
91+
// Converted in google-maps-routes-v1 1.1.0 and
92+
// google-maps-routing-v2 1.2.0.
93+
google_cloud_type::model::Money,
94+
// Converted in aiplatform-v1 1.18.0.
95+
google_cloud_api::model::HttpBody,
96+
// Converted in logging-v2 1.15.0.
97+
google_cloud_api::model::MonitoredResource,
98+
google_cloud_logging_type::model::HttpRequest,
99+
google_cloud_logging_type::model::LogSeverity,
100+
// Converted in storage 1.21.0.
101+
google_cloud_iam_v1::model::AuditConfig,
102+
google_cloud_iam_v1::model::AuditConfigDelta,
103+
google_cloud_iam_v1::model::AuditLogConfig,
104+
google_cloud_iam_v1::model::Binding,
105+
google_cloud_iam_v1::model::BindingDelta,
106+
google_cloud_iam_v1::model::GetIamPolicyRequest,
107+
google_cloud_iam_v1::model::GetPolicyOptions,
108+
google_cloud_iam_v1::model::Policy,
109+
google_cloud_iam_v1::model::PolicyDelta,
110+
google_cloud_iam_v1::model::SetIamPolicyRequest,
111+
google_cloud_iam_v1::model::TestIamPermissionsRequest,
112+
google_cloud_iam_v1::model::TestIamPermissionsResponse,
113+
google_cloud_iam_v1::model::audit_config_delta::Action,
114+
google_cloud_iam_v1::model::audit_log_config::LogType,
115+
google_cloud_iam_v1::model::binding_delta::Action,
116+
// Converted in storage 1.21.0. The `Operation` impl is handwritten, in
117+
// `src/control/convert.rs`.
118+
google_cloud_longrunning::model::CancelOperationRequest,
119+
google_cloud_longrunning::model::DeleteOperationRequest,
120+
google_cloud_longrunning::model::GetOperationRequest,
121+
google_cloud_longrunning::model::ListOperationsRequest,
122+
google_cloud_longrunning::model::ListOperationsResponse,
123+
google_cloud_longrunning::model::Operation,
124+
google_cloud_longrunning::model::OperationInfo,
125+
google_cloud_longrunning::model::WaitOperationRequest,
126+
);
127+
}
128+
129+
/// Types that `gaxi` itself converts with `ToProto`.
130+
///
131+
/// Published code calls `.to_proto()` on these types unqualified, e.g. storage
132+
/// 1.21.0 `generated/convert/control/convert.rs:62`:
133+
/// `create_time: self.create_time.map(|v| v.to_proto())`.
134+
///
135+
/// A local impl would itself be a second impl, so these checks have none.
136+
/// Nothing in the call constrains the `ToProto<T>` parameter, so each call
137+
/// compiles only while `gaxi` provides exactly one impl.
138+
#[cfg(test)]
139+
mod gaxi_to_proto {
140+
use gaxi::prost::{ConvertError, ToProto};
141+
142+
macro_rules! assert_single_impl {
143+
($($ty:ty),* $(,)?) => {
144+
#[test]
145+
fn single_impl() -> Result<(), ConvertError> {
146+
$(
147+
let _ = <$ty>::default().to_proto()?;
148+
)*
149+
Ok(())
150+
}
151+
};
152+
}
153+
154+
assert_single_impl!(
155+
google_cloud_wkt::Duration,
156+
google_cloud_wkt::FieldMask,
157+
google_cloud_wkt::Timestamp,
158+
google_cloud_wkt::Struct,
159+
google_cloud_wkt::Value,
160+
google_cloud_wkt::ListValue,
161+
google_cloud_wkt::NullValue,
162+
google_cloud_wkt::Empty,
163+
(),
164+
bool,
165+
f32,
166+
i32,
167+
u32,
168+
f64,
169+
i64,
170+
u64,
171+
String,
172+
bytes::Bytes,
173+
);
174+
}
175+
176+
/// Prost types that `gaxi` converts with `FromProto`.
177+
///
178+
/// Published code passes the result of an unqualified `.cnv()` to a setter
179+
/// that accepts any `Into<Model>`, e.g. storage 1.21.0
180+
/// `generated/convert/control/convert.rs:75`:
181+
/// `.set_or_clear_create_time(self.create_time.map(|v| v.cnv()).transpose()?)`.
182+
///
183+
/// `set_or_clear()` has the same shape as that setter, and the calls below
184+
/// copy the `map(...).transpose()?` expression. The `Into` bound does not pin
185+
/// the `FromProto<T>` parameter, so each call compiles only while `gaxi`
186+
/// provides exactly one impl.
187+
#[cfg(test)]
188+
mod gaxi_from_proto {
189+
use gaxi::prost::{ConvertError, FromProto};
190+
191+
fn set_or_clear<M, T: Into<M>>(_: Option<T>) {}
192+
193+
macro_rules! assert_single_impl {
194+
($($prost:ty => $model:ty),* $(,)?) => {
195+
#[test]
196+
fn single_impl() -> Result<(), ConvertError> {
197+
$(
198+
set_or_clear::<$model, _>(
199+
Some(<$prost>::default()).map(|v| v.cnv()).transpose()?,
200+
);
201+
)*
202+
Ok(())
203+
}
204+
};
205+
}
206+
207+
assert_single_impl!(
208+
prost_types::Duration => google_cloud_wkt::Duration,
209+
prost_types::FieldMask => google_cloud_wkt::FieldMask,
210+
prost_types::Timestamp => google_cloud_wkt::Timestamp,
211+
prost_types::Struct => google_cloud_wkt::Struct,
212+
prost_types::Value => google_cloud_wkt::Value,
213+
prost_types::ListValue => google_cloud_wkt::ListValue,
214+
prost_types::NullValue => google_cloud_wkt::NullValue,
215+
gaxi::prost::Empty => google_cloud_wkt::Empty,
216+
() => (),
217+
bool => bool,
218+
f32 => f32,
219+
i32 => i32,
220+
u32 => u32,
221+
f64 => f64,
222+
i64 => i64,
223+
u64 => u64,
224+
String => String,
225+
bytes::Bytes => bytes::Bytes,
226+
);
227+
}

0 commit comments

Comments
 (0)