From 5485d92a0a257151a4d06f0eb4c50b9edb6826c1 Mon Sep 17 00:00:00 2001 From: Kirill Ivanov <8144358+bragov4ik@users.noreply.github.com> Date: Thu, 4 Jul 2024 18:50:15 +0300 Subject: [PATCH] refactor(stats)!: change generated message field cases (#975) * refactor: change codegen to set snake case for api consistency BREAKING CHANGE: change of message field name in API * refactor(template): also set snake case for consistency * test(stats): test more cases of serialization --- .../{{project-name}}-proto/build.rs | 4 ++-- stats/stats-proto/build.rs | 4 ++-- stats/stats-proto/src/tests.rs | 22 ++++++++++++++++++- stats/stats-proto/swagger/stats.swagger.yaml | 2 +- 4 files changed, 26 insertions(+), 6 deletions(-) diff --git a/service-template/{{project-name}}-proto/build.rs b/service-template/{{project-name}}-proto/build.rs index e25714474..d5c012d22 100644 --- a/service-template/{{project-name}}-proto/build.rs +++ b/service-template/{{project-name}}-proto/build.rs @@ -14,10 +14,10 @@ fn compile( .compile_well_known_types() .protoc_arg("--openapiv2_out=swagger/v1") .protoc_arg("--openapiv2_opt") - .protoc_arg("grpc_api_configuration=proto/v1/api_config_http.yaml,output_format=yaml,allow_merge=true,merge_file_name={{project-name}}") + .protoc_arg("grpc_api_configuration=proto/v1/api_config_http.yaml,output_format=yaml,allow_merge=true,merge_file_name={{project-name}},json_names_for_fields=false") .bytes(["."]) .btree_map(["."]) - .type_attribute(".", "#[actix_prost_macros::serde]") + .type_attribute(".", "#[actix_prost_macros::serde(rename_all=\"snake_case\")]") // .field_attribute( // ".blockscout.{{projectName}}.v1..", // "#[serde(default)]" diff --git a/stats/stats-proto/build.rs b/stats/stats-proto/build.rs index f91a660ca..a5d718363 100644 --- a/stats/stats-proto/build.rs +++ b/stats/stats-proto/build.rs @@ -14,9 +14,9 @@ fn compile( .compile_well_known_types() .protoc_arg("--openapiv2_out=swagger") .protoc_arg("--openapiv2_opt") - .protoc_arg("grpc_api_configuration=proto/api_config_http.yaml,output_format=yaml,allow_merge=true,merge_file_name=stats") + .protoc_arg("grpc_api_configuration=proto/api_config_http.yaml,output_format=yaml,allow_merge=true,merge_file_name=stats,json_names_for_fields=false") .bytes(["."]) - .type_attribute(".", "#[actix_prost_macros::serde]") + .type_attribute(".", "#[actix_prost_macros::serde(rename_all=\"snake_case\")]") .field_attribute( ".blockscout.stats.v1.HealthCheckRequest.service", "#[serde(default)]" diff --git a/stats/stats-proto/src/tests.rs b/stats/stats-proto/src/tests.rs index e65463e30..1295ded44 100644 --- a/stats/stats-proto/src/tests.rs +++ b/stats/stats-proto/src/tests.rs @@ -6,7 +6,7 @@ const PRECISE_POINT_1: &str = r#" { "date": "2024-03-14", "value": "188542399", - "isApproximate": false + "is_approximate": false } "#; @@ -17,6 +17,14 @@ const PRECISE_POINT_2: &str = r#" } "#; +const IMPRECISE_POINT: &str = r#" +{ + "date": "2024-03-14", + "value": "188542399", + "is_approximate": true +} +"#; + #[test] fn is_approximate_serialization() { // deserialize @@ -24,6 +32,8 @@ fn is_approximate_serialization() { assert!(!point.is_approximate); let point: proto::Point = serde_json::from_str(PRECISE_POINT_2).unwrap(); assert!(!point.is_approximate); + let point: proto::Point = serde_json::from_str(IMPRECISE_POINT).unwrap(); + assert!(point.is_approximate); // serialize let point = proto::Point { @@ -36,4 +46,14 @@ fn is_approximate_serialization() { serialized_point.replace([' ', '\n'], ""), PRECISE_POINT_2.replace([' ', '\n'], "") ); + let point = proto::Point { + date: "2024-03-14".to_owned(), + value: "188542399".to_owned(), + is_approximate: true, + }; + let serialized_point = serde_json::to_string(&point).unwrap(); + assert_eq!( + serialized_point.replace([' ', '\n'], ""), + IMPRECISE_POINT.replace([' ', '\n'], "") + ); } diff --git a/stats/stats-proto/swagger/stats.swagger.yaml b/stats/stats-proto/swagger/stats.swagger.yaml index 6398e9a32..bb0359eb4 100644 --- a/stats/stats-proto/swagger/stats.swagger.yaml +++ b/stats/stats-proto/swagger/stats.swagger.yaml @@ -190,6 +190,6 @@ definitions: type: string value: type: string - isApproximate: + is_approximate: type: boolean title: All integers are encoded as strings to prevent data loss