@@ -13,6 +13,11 @@ pub enum ProtoStructError {
1313 UnrepresentableNumber ( serde_json:: Number ) ,
1414}
1515
16+ /// A non-finite protobuf number cannot be used as typed configuration.
17+ #[ derive( Debug , thiserror:: Error ) ]
18+ #[ error( "protobuf configuration contains a non-finite number" ) ]
19+ pub struct TypedProtoStructError ;
20+
1621/// Convert a JSON object into a protobuf Struct.
1722pub fn json_object_to_struct (
1823 object : serde_json:: Map < String , serde_json:: Value > ,
@@ -87,6 +92,77 @@ pub fn struct_to_json_value(config: &prost_types::Struct) -> serde_json::Value {
8792 serde_json:: Value :: Object ( struct_to_json_object ( config) )
8893}
8994
95+ /// Convert protobuf configuration into JSON for typed serde decoding.
96+ ///
97+ /// Finite integral doubles in the JSON integer range become integers; fractions
98+ /// and larger finite numbers remain floats. This preserves exactly represented
99+ /// large integers accepted by [`json_value_to_proto`]. A receiver cannot detect
100+ /// precision already lost before transport; the producer must enforce exactness.
101+ /// General-purpose conversions keep their existing float and null behavior.
102+ ///
103+ /// # Errors
104+ ///
105+ /// Reject non-finite numbers anywhere in the configuration instead of turning
106+ /// them into null, which could silently clear an optional constraint.
107+ pub fn struct_to_typed_json_value (
108+ config : & prost_types:: Struct ,
109+ ) -> Result < serde_json:: Value , TypedProtoStructError > {
110+ config
111+ . fields
112+ . iter ( )
113+ . map ( |( key, value) | typed_value_to_json ( value) . map ( |value| ( key. clone ( ) , value) ) )
114+ . collect :: < Result < serde_json:: Map < _ , _ > , _ > > ( )
115+ . map ( serde_json:: Value :: Object )
116+ }
117+
118+ // The casts below are preceded by finite, integral and exclusive range guards.
119+ #[ allow(
120+ clippy:: cast_possible_truncation,
121+ clippy:: cast_precision_loss,
122+ clippy:: cast_sign_loss
123+ ) ]
124+ fn typed_value_to_json (
125+ value : & prost_types:: Value ,
126+ ) -> Result < serde_json:: Value , TypedProtoStructError > {
127+ use prost_types:: value:: Kind ;
128+ use serde_json:: { Number , Value } ;
129+
130+ Ok ( match value. kind . as_ref ( ) {
131+ Some ( Kind :: NumberValue ( number) ) => {
132+ if !number. is_finite ( ) {
133+ return Err ( TypedProtoStructError ) ;
134+ }
135+ // Exclusive upper bounds are intentional: u64::MAX and i64::MAX
136+ // round UP when converted to f64. Guard before any integer cast.
137+ let integer = if number. fract ( ) == 0.0 {
138+ if * number >= 0.0 && * number < u64:: MAX as f64 {
139+ Some ( Number :: from ( * number as u64 ) )
140+ } else if * number >= i64:: MIN as f64 && * number < 0.0 {
141+ Some ( Number :: from ( * number as i64 ) )
142+ } else {
143+ None
144+ }
145+ } else {
146+ None
147+ } ;
148+ Value :: Number (
149+ integer
150+ . or_else ( || Number :: from_f64 ( * number) )
151+ . ok_or ( TypedProtoStructError ) ?,
152+ )
153+ }
154+ Some ( Kind :: StructValue ( config) ) => struct_to_typed_json_value ( config) ?,
155+ Some ( Kind :: ListValue ( list) ) => Value :: Array (
156+ list. values
157+ . iter ( )
158+ . map ( typed_value_to_json)
159+ . collect :: < Result < _ , _ > > ( ) ?,
160+ ) ,
161+ // Only numeric values differ from the compatible general conversion.
162+ _ => value_to_json ( value) ,
163+ } )
164+ }
165+
90166/// Convert a protobuf Value into a JSON value for typed serde decoding.
91167#[ must_use]
92168pub fn value_to_json ( value : & prost_types:: Value ) -> serde_json:: Value {
@@ -177,6 +253,123 @@ mod tests {
177253 ) ;
178254 }
179255
256+ #[ test]
257+ fn typed_configuration_round_trips_recursive_integer_fields ( ) {
258+ #[ derive( Debug , Deserialize , PartialEq ) ]
259+ struct Limits {
260+ max_processes : Option < u32 > ,
261+ max_resident_bytes : u64 ,
262+ }
263+ #[ derive( Debug , Deserialize , PartialEq ) ]
264+ struct Config {
265+ service_ports : Vec < u16 > ,
266+ limits : Limits ,
267+ nested : Vec < Limits > ,
268+ }
269+ let json = serde_json:: json!( {
270+ "service_ports" : [ 8080 , 65535 ] ,
271+ "limits" : { "max_processes" : 128 , "max_resident_bytes" : 1_073_741_824 } ,
272+ "nested" : [ { "max_processes" : null, "max_resident_bytes" : 0 } ]
273+ } ) ;
274+ let expected: Config = serde_json:: from_value ( json. clone ( ) ) . unwrap ( ) ;
275+ let proto = json_object_to_struct ( json. as_object ( ) . unwrap ( ) . clone ( ) ) . unwrap ( ) ;
276+ let actual: Config =
277+ serde_json:: from_value ( struct_to_typed_json_value ( & proto) . unwrap ( ) ) . unwrap ( ) ;
278+ assert_eq ! ( actual, expected) ;
279+ // The general-purpose conversion retains its existing float semantics.
280+ assert ! ( struct_to_json_value( & proto) [ "service_ports" ] [ 0 ] . is_f64( ) ) ;
281+ }
282+
283+ #[ test]
284+ fn typed_configuration_rejects_nonfinite_numbers ( ) {
285+ for number in [ f64:: NAN , f64:: INFINITY , f64:: NEG_INFINITY ] {
286+ let mut proto = json_object_to_struct (
287+ serde_json:: json!( { "limits" : [ { "max_processes" : 128 } ] } )
288+ . as_object ( )
289+ . unwrap ( )
290+ . clone ( ) ,
291+ )
292+ . unwrap ( ) ;
293+ let Some ( prost_types:: value:: Kind :: ListValue ( list) ) =
294+ proto. fields . get_mut ( "limits" ) . unwrap ( ) . kind . as_mut ( )
295+ else {
296+ unreachable ! ( ) ;
297+ } ;
298+ let Some ( prost_types:: value:: Kind :: StructValue ( limits) ) = list. values [ 0 ] . kind . as_mut ( )
299+ else {
300+ unreachable ! ( ) ;
301+ } ;
302+ limits. fields . get_mut ( "max_processes" ) . unwrap ( ) . kind =
303+ Some ( prost_types:: value:: Kind :: NumberValue ( number) ) ;
304+ assert ! ( struct_to_typed_json_value( & proto) . is_err( ) ) ;
305+ assert ! ( struct_to_json_value( & proto) [ "limits" ] [ 0 ] [ "max_processes" ] . is_null( ) ) ;
306+ }
307+ }
308+
309+ #[ test]
310+ fn typed_configuration_preserves_numeric_boundaries ( ) {
311+ for integer in [
312+ 0 ,
313+ 9_007_199_254_740_992 ,
314+ 9_007_199_254_740_994 ,
315+ 1_u64 << 63 ,
316+ u64:: MAX - 2047 ,
317+ ] {
318+ let proto = json_object_to_struct (
319+ serde_json:: json!( { "limit" : integer} )
320+ . as_object ( )
321+ . unwrap ( )
322+ . clone ( ) ,
323+ )
324+ . unwrap ( ) ;
325+ let value = struct_to_typed_json_value ( & proto) . unwrap ( ) ;
326+ assert_eq ! (
327+ serde_json:: from_value:: <u64 >( value[ "limit" ] . clone( ) ) . unwrap( ) ,
328+ integer
329+ ) ;
330+ }
331+ let proto = json_object_to_struct (
332+ serde_json:: json!( { "limit" : i64 :: MIN } )
333+ . as_object ( )
334+ . unwrap ( )
335+ . clone ( ) ,
336+ )
337+ . unwrap ( ) ;
338+ let value = struct_to_typed_json_value ( & proto) . unwrap ( ) ;
339+ assert_eq ! (
340+ serde_json:: from_value:: <i64 >( value[ "limit" ] . clone( ) ) . unwrap( ) ,
341+ i64 :: MIN
342+ ) ;
343+ // Fractions and doubles outside the integer range must not be truncated
344+ // or saturated into a valid integer configuration value.
345+ for number in [ 1.5 , -1.0 , 18_446_744_073_709_551_616.0 , f64:: MAX ] {
346+ let proto = prost_types:: Struct {
347+ fields : [ (
348+ "limit" . to_owned ( ) ,
349+ prost_types:: Value {
350+ kind : Some ( prost_types:: value:: Kind :: NumberValue ( number) ) ,
351+ } ,
352+ ) ]
353+ . into ( ) ,
354+ } ;
355+ let value = struct_to_typed_json_value ( & proto) . unwrap ( ) ;
356+ assert ! ( serde_json:: from_value:: <u64 >( value[ "limit" ] . clone( ) ) . is_err( ) ) ;
357+ }
358+ let below_i64_min = prost_types:: Struct {
359+ fields : [ (
360+ "limit" . to_owned ( ) ,
361+ prost_types:: Value {
362+ kind : Some ( prost_types:: value:: Kind :: NumberValue (
363+ -9_223_372_036_854_777_856.0 ,
364+ ) ) ,
365+ } ,
366+ ) ]
367+ . into ( ) ,
368+ } ;
369+ let value = struct_to_typed_json_value ( & below_i64_min) . unwrap ( ) ;
370+ assert ! ( serde_json:: from_value:: <i64 >( value[ "limit" ] . clone( ) ) . is_err( ) ) ;
371+ }
372+
180373 #[ derive( Debug , Default , Deserialize ) ]
181374 #[ serde( default ) ]
182375 struct TestConfig {
0 commit comments