Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use the JSON path implementation from serde-json-bytes #5346

Merged
merged 9 commits into from
Jun 13, 2024
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: 6 additions & 4 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5515,9 +5515,9 @@ dependencies = [

[[package]]
name = "regex"
version = "1.10.3"
version = "1.10.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b62dbe01f0b06f9d8dc7d49e05a0785f153b00b2c227856282f671e0318c9b15"
checksum = "c117dbdfde9c8308975b6a18d71f3f385c89461f7b3fb054288ecf2a2058ba4c"
dependencies = [
"aho-corasick",
"memchr",
Expand Down Expand Up @@ -6215,12 +6215,14 @@ dependencies = [

[[package]]
name = "serde_json_bytes"
version = "0.2.2"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "feb260b2939374fad6f939f803662d4971d03395fcd03752b674bdba06565779"
checksum = "9eb67259abd83636e3b2f4700ddc3ecfdbfe7b1b89e824da42bfea6530caf03b"
dependencies = [
"bytes",
"indexmap 2.2.3",
"jsonpath-rust",
"regex",
"serde",
"serde_json",
]
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ serde_json = { version = "1.0.114", features = [
"preserve_order",
"float_roundtrip",
] }
serde_json_bytes = { version = "0.2.2", features = ["preserve_order"] }
serde_json_bytes = { version = "0.2.3", features = ["preserve_order"] }
sha1 = "0.10.6"
tempfile = "3.10.0"
tokio = { version = "1.36.0", features = ["full"] }
Expand Down
9 changes: 5 additions & 4 deletions apollo-router/src/plugin/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use std::str::FromStr;
use access_json::JSONQuery;
use http::header::HeaderName;
use http::HeaderValue;
use jsonpath_rust::JsonPathInst;
use regex::Regex;
use serde::de;
use serde::de::Error;
Expand Down Expand Up @@ -211,7 +210,9 @@ where
deserializer.deserialize_str(RegexVisitor)
}

pub(crate) fn deserialize_jsonpath<'de, D>(deserializer: D) -> Result<JsonPathInst, D::Error>
pub(crate) fn deserialize_jsonpath<'de, D>(
deserializer: D,
) -> Result<serde_json_bytes::path::JsonPathInst, D::Error>
where
D: serde::Deserializer<'de>,
{
Expand All @@ -221,7 +222,7 @@ where
struct JSONPathVisitor;

impl<'de> serde::de::Visitor<'de> for JSONPathVisitor {
type Value = JsonPathInst;
type Value = serde_json_bytes::path::JsonPathInst;

fn expecting(&self, formatter: &mut Formatter) -> std::fmt::Result {
write!(formatter, "a JSON path")
Expand All @@ -231,6 +232,6 @@ impl<'de> serde::de::Visitor<'de> for JSONPathVisitor {
where
E: serde::de::Error,
{
JsonPathInst::from_str(s).map_err(serde::de::Error::custom)
serde_json_bytes::path::JsonPathInst::from_str(s).map_err(serde::de::Error::custom)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1872,7 +1872,6 @@ mod tests {
use apollo_compiler::ast::Name;
use apollo_compiler::ast::NamedType;
use apollo_compiler::executable::SelectionSet;
use apollo_compiler::execution::JsonMap;
use http::HeaderMap;
use http::HeaderName;
use http::Method;
Expand All @@ -1883,6 +1882,7 @@ mod tests {
use schemars::gen::SchemaGenerator;
use serde::Deserialize;
use serde_json::json;
use serde_json_bytes::ByteString;
use serde_json_bytes::Value;

use super::*;
Expand All @@ -1902,6 +1902,8 @@ mod tests {
use crate::services::RouterResponse;
use crate::Context;

type JsonMap = serde_json_bytes::Map<ByteString, Value>;

#[derive(RustEmbed)]
#[folder = "src/plugins/telemetry/config_new/fixtures"]
struct Asset;
Expand Down
55 changes: 9 additions & 46 deletions apollo-router/src/plugins/telemetry/config_new/selectors.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use access_json::JSONQuery;
use derivative::Derivative;
use jsonpath_rust::JsonPathFinder;
use jsonpath_rust::JsonPathInst;
use opentelemetry_api::Value;
use schemars::JsonSchema;
use serde::Deserialize;
use serde_json_bytes::path::JsonPathInst;
use serde_json_bytes::ByteString;
use sha2::Digest;

Expand Down Expand Up @@ -818,17 +817,7 @@ impl Selector for SupergraphSelector {
default,
..
} => if let Some(data) = &response.data {
let data: serde_json::Value = serde_json::to_value(data.clone()).ok()?;
let mut val =
JsonPathFinder::new(Box::new(data), Box::new(response_data.clone())).find();
if let serde_json::Value::Array(array) = &mut val {
if array.len() == 1 {
val = array
.pop()
.expect("already checked the array had a length of 1; qed");
}
}

let val = response_data.find(data);
val.maybe_to_otel_value()
} else {
None
Expand All @@ -840,16 +829,8 @@ impl Selector for SupergraphSelector {
..
} => {
let errors = response.errors.clone();
let data: serde_json::Value = serde_json::to_value(errors).ok()?;
let mut val =
JsonPathFinder::new(Box::new(data), Box::new(response_errors.clone())).find();
if let serde_json::Value::Array(array) = &mut val {
if array.len() == 1 {
val = array
.pop()
.expect("already checked the array had a length of 1; qed");
}
}
let data: serde_json_bytes::Value = serde_json_bytes::to_value(errors).ok()?;
let val = response_errors.find(&data);

val.maybe_to_otel_value()
}
Expand Down Expand Up @@ -1088,17 +1069,7 @@ impl Selector for SubgraphSelector {
default,
..
} => if let Some(data) = &response.response.body().data {
let data: serde_json::Value = serde_json::to_value(data.clone()).ok()?;
let mut val =
JsonPathFinder::new(Box::new(data), Box::new(subgraph_response_data.clone()))
.find();
if let serde_json::Value::Array(array) = &mut val {
if array.len() == 1 {
val = array
.pop()
.expect("already checked the array had a length of 1; qed");
}
}
let val = subgraph_response_data.find(data);

val.maybe_to_otel_value()
} else {
Expand All @@ -1111,17 +1082,9 @@ impl Selector for SubgraphSelector {
..
} => {
let errors = response.response.body().errors.clone();
let data: serde_json::Value = serde_json::to_value(errors).ok()?;
let mut val =
JsonPathFinder::new(Box::new(data), Box::new(subgraph_response_error.clone()))
.find();
if let serde_json::Value::Array(array) = &mut val {
if array.len() == 1 {
val = array
.pop()
.expect("already checked the array had a length of 1; qed");
}
}
let data: serde_json_bytes::Value = serde_json_bytes::to_value(errors).ok()?;

let val = subgraph_response_error.find(&data);

val.maybe_to_otel_value()
}
Expand Down Expand Up @@ -1176,7 +1139,6 @@ mod test {
use std::sync::Arc;

use http::StatusCode;
use jsonpath_rust::JsonPathInst;
use opentelemetry::baggage::BaggageExt;
use opentelemetry::trace::SpanContext;
use opentelemetry::trace::SpanId;
Expand All @@ -1188,6 +1150,7 @@ mod test {
use opentelemetry::KeyValue;
use opentelemetry_api::StringValue;
use serde_json::json;
use serde_json_bytes::path::JsonPathInst;
use tower::BoxError;
use tracing::span;
use tracing::subscriber;
Expand Down
2 changes: 1 addition & 1 deletion apollo-router/src/plugins/telemetry/config_new/spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,13 @@ mod test {
use std::sync::Arc;

use http::header::USER_AGENT;
use jsonpath_rust::JsonPathInst;
use opentelemetry_semantic_conventions::trace::GRAPHQL_DOCUMENT;
use opentelemetry_semantic_conventions::trace::HTTP_REQUEST_METHOD;
use opentelemetry_semantic_conventions::trace::NETWORK_PROTOCOL_VERSION;
use opentelemetry_semantic_conventions::trace::URL_PATH;
use opentelemetry_semantic_conventions::trace::USER_AGENT_ORIGINAL;
use parking_lot::Mutex;
use serde_json_bytes::path::JsonPathInst;

use crate::context::CONTAINS_GRAPHQL_ERROR;
use crate::graphql;
Expand Down
5 changes: 4 additions & 1 deletion apollo-router/tests/type_conditions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
//! Please ensure that any tests added to this file use the tokio multi-threaded test executor.
//!

use apollo_compiler::execution::JsonMap;
use apollo_router::graphql::Request;
use apollo_router::graphql::Response;
use apollo_router::plugin::test::MockSubgraph;
Expand All @@ -11,8 +10,12 @@ use apollo_router::MockedSubgraphs;
use apollo_router::TestHarness;
use serde::Deserialize;
use serde_json::json;
use serde_json_bytes::ByteString;
use serde_json_bytes::Value;
use tower::ServiceExt;

type JsonMap = serde_json_bytes::Map<ByteString, Value>;

#[derive(Deserialize)]
struct SubgraphMock {
mocks: Vec<RequestAndResponse>,
Expand Down
2 changes: 1 addition & 1 deletion examples/async-auth/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ http = "0.2"
schemars = { version = "0.8", features = ["url"] }
serde = "1"
serde_json = "1"
serde_json_bytes = "0.2"
serde_json_bytes.workspace = true
tokio = { version = "1", features = ["full"] }
tower = { version = "0.4", features = ["full"] }
Loading