Skip to content

Commit ebb5d5f

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 ebb5d5f

4 files changed

Lines changed: 307 additions & 0 deletions

File tree

‎Cargo.lock‎

Lines changed: 18 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: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
#
27+
# Do include every crate that a published converter depends on and that can
28+
# reach `gaxi::prost` (today or by enabling `_internal-grpc-client`). The canary
29+
# only sees impls added by crates in its own dependency graph.
30+
[dev-dependencies]
31+
bytes.workspace = true
32+
gaxi = { workspace = true, features = ["_internal-grpc-client"] }
33+
google-cloud-api.workspace = true
34+
google-cloud-iam-v1 = { workspace = true, features = ["default"] }
35+
google-cloud-location = { workspace = true, features = ["default"] }
36+
google-cloud-logging-type.workspace = true
37+
google-cloud-longrunning = { workspace = true, features = ["default"] }
38+
google-cloud-lro.workspace = true
39+
google-cloud-rpc.workspace = true
40+
google-cloud-type.workspace = true
41+
google-cloud-wkt.workspace = true
42+
prost-types.workspace = true
43+
44+
[lints]
45+
workspace = true

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

Lines changed: 242 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,242 @@
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+
//! The canary only sees impls added by crates in its own dependency graph. So
34+
//! do depend on every crate that a published converter depends on and that can
35+
//! reach `gaxi::prost`, today or by enabling `_internal-grpc-client` (e.g.
36+
//! `google-cloud-lro`, `google-cloud-longrunning`, `google-cloud-iam-v1`,
37+
//! `google-cloud-location`). If such a crate defines none of the checked
38+
//! types, also load it with `extern crate` below.
39+
//!
40+
//! See <https://github.com/googleapis/google-cloud-rust/issues/7284>.
41+
42+
// rustc only loads dependencies that the source references, and only sees
43+
// impls from loaded crates. These crates define none of the checked types, so
44+
// load them explicitly.
45+
#[cfg(test)]
46+
extern crate google_cloud_location as _;
47+
#[cfg(test)]
48+
extern crate google_cloud_lro as _;
49+
50+
/// External model types that published crates convert with their own
51+
/// `ToProto` impl.
52+
///
53+
/// For example, aiplatform-v1 1.18.0 implements `ToProto` for
54+
/// `google_cloud_rpc::model::Status`, targeting its own prost type, and calls
55+
/// `.to_proto()` on it unqualified. Each type gets a local
56+
/// `ToProto<LocalProst>` impl, which stands in for the published one. If an
57+
/// upstream crate adds another impl, the unqualified call below no longer
58+
/// compiles.
59+
///
60+
/// This list is frozen. Code generated after #7374 qualifies these calls, so
61+
/// only crate versions published before that release need coverage.
62+
#[cfg(test)]
63+
mod external_model_to_proto {
64+
use gaxi::prost::{ConvertError, ToProto};
65+
66+
struct LocalProst;
67+
68+
macro_rules! assert_single_upstream_impl {
69+
($($ty:ty),* $(,)?) => {
70+
$(
71+
impl ToProto<LocalProst> for $ty {
72+
type Output = LocalProst;
73+
fn to_proto(self) -> Result<LocalProst, ConvertError> {
74+
Ok(LocalProst)
75+
}
76+
}
77+
)*
78+
79+
#[test]
80+
fn no_upstream_impl() -> Result<(), ConvertError> {
81+
$(
82+
let _: LocalProst = <$ty>::default().to_proto()?;
83+
)*
84+
Ok(())
85+
}
86+
};
87+
}
88+
89+
assert_single_upstream_impl!(
90+
// Converted in aiplatform-v1 1.18.0, apigeeconnect-v1 1.12.0,
91+
// dialogflow-cx-v3 2.11.0, dialogflow-v2 1.18.0,
92+
// google-maps-routes-v1 1.1.0, and google-maps-routing-v2 1.2.0.
93+
google_cloud_rpc::model::Status,
94+
// Converted in storage 1.21.0 and aiplatform-v1 1.18.0.
95+
google_cloud_type::model::Date,
96+
// Converted in storage 1.21.0.
97+
google_cloud_type::model::Expr,
98+
// Converted in storage 1.21.0 and datacatalog-lineage-v1 1.15.0.
99+
google_cloud_type::model::Interval,
100+
// Converted in aiplatform-v1 1.18.0, dialogflow-cx-v3 2.11.0,
101+
// dialogflow-v2 1.18.0, google-maps-routes-v1 1.1.0, and
102+
// google-maps-routing-v2 1.2.0.
103+
google_cloud_type::model::LatLng,
104+
// Converted in google-maps-routing-v2 1.2.0.
105+
google_cloud_type::model::LocalizedText,
106+
// Converted in google-maps-routes-v1 1.1.0 and
107+
// google-maps-routing-v2 1.2.0.
108+
google_cloud_type::model::Money,
109+
// Converted in aiplatform-v1 1.18.0.
110+
google_cloud_api::model::HttpBody,
111+
// Converted in logging-v2 1.15.0.
112+
google_cloud_api::model::MonitoredResource,
113+
google_cloud_logging_type::model::HttpRequest,
114+
google_cloud_logging_type::model::LogSeverity,
115+
// Converted in storage 1.21.0.
116+
google_cloud_iam_v1::model::AuditConfig,
117+
google_cloud_iam_v1::model::AuditConfigDelta,
118+
google_cloud_iam_v1::model::AuditLogConfig,
119+
google_cloud_iam_v1::model::Binding,
120+
google_cloud_iam_v1::model::BindingDelta,
121+
google_cloud_iam_v1::model::GetIamPolicyRequest,
122+
google_cloud_iam_v1::model::GetPolicyOptions,
123+
google_cloud_iam_v1::model::Policy,
124+
google_cloud_iam_v1::model::PolicyDelta,
125+
google_cloud_iam_v1::model::SetIamPolicyRequest,
126+
google_cloud_iam_v1::model::TestIamPermissionsRequest,
127+
google_cloud_iam_v1::model::TestIamPermissionsResponse,
128+
google_cloud_iam_v1::model::audit_config_delta::Action,
129+
google_cloud_iam_v1::model::audit_log_config::LogType,
130+
google_cloud_iam_v1::model::binding_delta::Action,
131+
// Converted in storage 1.21.0. The `Operation` impl is handwritten, in
132+
// `src/control/convert.rs`.
133+
google_cloud_longrunning::model::CancelOperationRequest,
134+
google_cloud_longrunning::model::DeleteOperationRequest,
135+
google_cloud_longrunning::model::GetOperationRequest,
136+
google_cloud_longrunning::model::ListOperationsRequest,
137+
google_cloud_longrunning::model::ListOperationsResponse,
138+
google_cloud_longrunning::model::Operation,
139+
google_cloud_longrunning::model::OperationInfo,
140+
google_cloud_longrunning::model::WaitOperationRequest,
141+
);
142+
}
143+
144+
/// Types that `gaxi` itself converts with `ToProto`.
145+
///
146+
/// Published code calls `.to_proto()` on these types unqualified, e.g. storage
147+
/// 1.21.0 `generated/convert/control/convert.rs:62`:
148+
/// `create_time: self.create_time.map(|v| v.to_proto())`.
149+
///
150+
/// A local impl would itself be a second impl, so these checks have none.
151+
/// Nothing in the call constrains the `ToProto<T>` parameter, so each call
152+
/// compiles only while `gaxi` provides exactly one impl.
153+
#[cfg(test)]
154+
mod gaxi_to_proto {
155+
use gaxi::prost::{ConvertError, ToProto};
156+
157+
macro_rules! assert_single_impl {
158+
($($ty:ty),* $(,)?) => {
159+
#[test]
160+
fn single_impl() -> Result<(), ConvertError> {
161+
$(
162+
let _ = <$ty>::default().to_proto()?;
163+
)*
164+
Ok(())
165+
}
166+
};
167+
}
168+
169+
assert_single_impl!(
170+
google_cloud_wkt::Duration,
171+
google_cloud_wkt::FieldMask,
172+
google_cloud_wkt::Timestamp,
173+
google_cloud_wkt::Struct,
174+
google_cloud_wkt::Value,
175+
google_cloud_wkt::ListValue,
176+
google_cloud_wkt::NullValue,
177+
google_cloud_wkt::Empty,
178+
(),
179+
bool,
180+
f32,
181+
i32,
182+
u32,
183+
f64,
184+
i64,
185+
u64,
186+
String,
187+
bytes::Bytes,
188+
);
189+
}
190+
191+
/// Prost types that `gaxi` converts with `FromProto`.
192+
///
193+
/// Published code passes the result of an unqualified `.cnv()` to a setter
194+
/// that accepts any `Into<Model>`, e.g. storage 1.21.0
195+
/// `generated/convert/control/convert.rs:75`:
196+
/// `.set_or_clear_create_time(self.create_time.map(|v| v.cnv()).transpose()?)`.
197+
///
198+
/// `set_or_clear()` has the same shape as that setter, and the calls below
199+
/// copy the `map(...).transpose()?` expression. The `Into` bound does not pin
200+
/// the `FromProto<T>` parameter, so each call compiles only while `gaxi`
201+
/// provides exactly one impl.
202+
#[cfg(test)]
203+
mod gaxi_from_proto {
204+
use gaxi::prost::{ConvertError, FromProto};
205+
206+
fn set_or_clear<M, T: Into<M>>(_: Option<T>) {}
207+
208+
macro_rules! assert_single_impl {
209+
($($prost:ty => $model:ty),* $(,)?) => {
210+
#[test]
211+
fn single_impl() -> Result<(), ConvertError> {
212+
$(
213+
set_or_clear::<$model, _>(
214+
Some(<$prost>::default()).map(|v| v.cnv()).transpose()?,
215+
);
216+
)*
217+
Ok(())
218+
}
219+
};
220+
}
221+
222+
assert_single_impl!(
223+
prost_types::Duration => google_cloud_wkt::Duration,
224+
prost_types::FieldMask => google_cloud_wkt::FieldMask,
225+
prost_types::Timestamp => google_cloud_wkt::Timestamp,
226+
prost_types::Struct => google_cloud_wkt::Struct,
227+
prost_types::Value => google_cloud_wkt::Value,
228+
prost_types::ListValue => google_cloud_wkt::ListValue,
229+
prost_types::NullValue => google_cloud_wkt::NullValue,
230+
gaxi::prost::Empty => google_cloud_wkt::Empty,
231+
() => (),
232+
bool => bool,
233+
f32 => f32,
234+
i32 => i32,
235+
u32 => u32,
236+
f64 => f64,
237+
i64 => i64,
238+
u64 => u64,
239+
String => String,
240+
bytes::Bytes => bytes::Bytes,
241+
);
242+
}

0 commit comments

Comments
 (0)