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

fix: add validation for batchKey requiring either body or query parameters #3218

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
3 changes: 3 additions & 0 deletions src/core/blueprint/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ pub enum BlueprintError {
#[error("Batching capability was used without enabling it in upstream")]
IncorrectBatchingUsage,

#[error("batchKey requires either body or query parameters")]
BatchKeyRequiresEitherBodyOrQuery,

#[error("script is required")]
ScriptIsRequired,

Expand Down
6 changes: 6 additions & 0 deletions src/core/blueprint/operators/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ pub fn compile_http(
.unit()
.trace("query"),
)
.and(
Valid::<(), BlueprintError>::fail(BlueprintError::BatchKeyRequiresEitherBodyOrQuery)
.when(|| {
!http.batch_key.is_empty() && (http.body.is_none() && http.query.is_empty())
}),
)
.and(Valid::succeed(http.url.as_str()))
.zip(mustache_headers)
.and_then(|(base_url, headers)| {
Expand Down
9 changes: 9 additions & 0 deletions tests/core/snapshots/batching-validation.md_error.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@ source: tests/core/spec.rs
expression: errors
---
[
{
"message": "batchKey requires either body or query parameters",
"trace": [
"Query",
"posts",
"@http"
],
"description": null
},
{
"message": "Request body batching requires exactly one dynamic value in the body.",
"trace": [
Expand Down
5 changes: 2 additions & 3 deletions tests/core/snapshots/test-batch-operator-post.md_error.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ expression: errors
---
[
{
"message": "Request body batching requires exactly one dynamic value in the body.",
"message": "batchKey requires either body or query parameters",
"trace": [
"Query",
"user",
"@http",
"body"
"@http"
],
"description": null
}
Expand Down
7 changes: 7 additions & 0 deletions tests/execution/batching-validation.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ type User {
name: String
}

type Post {
id: Int
title: String
body: String
}

type Query {
user(id: Int!): User
@http(
Expand All @@ -22,6 +28,7 @@ type Query {
body: {uId: "{{.args.id}}", userId: "{{.args.id}}"}
batchKey: ["id"]
)
posts: [Post] @http(url: "http://jsonplaceholder.typicode.com/posts", batchKey: ["id"])
userWithId(id: Int!): User
@http(
url: "http://jsonplaceholder.typicode.com/users"
Expand Down