Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ async-graphql = { version = "7.0.3", features = [
"apollo_tracing",
"opentelemetry",
]}
convert_case = "0.6.0"

[dev-dependencies]
criterion = "0.5.1"
Expand Down
4 changes: 2 additions & 2 deletions src/grpc/protobuf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ impl ProtobufOperation {
}

#[cfg(test)]
mod tests {
pub mod tests {
// TODO: Rewrite protobuf tests
use std::path::PathBuf;

Expand Down Expand Up @@ -232,7 +232,7 @@ mod tests {
test_file
}

async fn get_proto_file(name: &str) -> Result<FileDescriptorSet> {
pub async fn get_proto_file(name: &str) -> Result<FileDescriptorSet> {
let runtime = crate::runtime::test::init(None);
let reader = ConfigReader::init(runtime);

Expand Down
2 changes: 1 addition & 1 deletion src/grpc/tests/proto/news.proto
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ message News {
int32 id = 1;
string title = 2;
string body = 3;
string postImage = 4;
string post_image = 4;
}

service NewsService {
Expand Down
2 changes: 1 addition & 1 deletion src/grpc/tests/proto/news_no_pkg.proto
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ message News {
int32 id = 1;
string title = 2;
string body = 3;
string postImage = 4;
string post_image = 4;
}

service NewsService {
Expand Down
37 changes: 36 additions & 1 deletion src/json/json_schema.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::collections::HashMap;

use convert_case::{Case, Casing};
use prost_reflect::{FieldDescriptor, Kind, MessageDescriptor};
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -158,7 +159,10 @@ impl TryFrom<&MessageDescriptor> for JsonSchema {
for field in fields {
let field_schema = JsonSchema::try_from(&field)?;

map.insert(field.name().to_string(), field_schema);
// the snake_case for field names is automatically converted to camelCase
// by prost on serde serialize/deserealize and in graphql type name should be in
// camelCase as well, so convert field.name to camelCase here
map.insert(field.name().to_case(Case::Camel), field_schema);
}

Ok(JsonSchema::Obj(map))
Expand Down Expand Up @@ -210,9 +214,14 @@ impl TryFrom<&FieldDescriptor> for JsonSchema {

#[cfg(test)]
mod tests {
use std::collections::HashMap;

use async_graphql::Name;
use indexmap::IndexMap;

use crate::blueprint::GrpcMethod;
use crate::grpc::protobuf::tests::get_proto_file;
use crate::grpc::protobuf::ProtobufSet;
use crate::json::JsonSchema;
use crate::valid::{Valid, Validator};

Expand Down Expand Up @@ -274,4 +283,30 @@ mod tests {
let result = schema.validate(&value);
assert_eq!(result, Valid::succeed(()));
}

#[tokio::test]
async fn test_from_protobuf_conversion() -> anyhow::Result<()> {
let grpc_method = GrpcMethod::try_from("news.NewsService.GetNews").unwrap();

let file = ProtobufSet::from_proto_file(&get_proto_file("news.proto").await?)?;
let service = file.find_service(&grpc_method)?;
let operation = service.find_operation(&grpc_method)?;

let schema = JsonSchema::try_from(&operation.output_type)?;

assert_eq!(
schema,
JsonSchema::Obj(HashMap::from_iter([
(
"postImage".to_owned(),
JsonSchema::Opt(JsonSchema::Str.into())
),
("title".to_owned(), JsonSchema::Opt(JsonSchema::Str.into())),
("id".to_owned(), JsonSchema::Opt(JsonSchema::Num.into())),
("body".to_owned(), JsonSchema::Opt(JsonSchema::Str.into())),
]))
);

Ok(())
}
}