Skip to content

Commit

Permalink
fix: add validation for batchKey requiring either body or query param…
Browse files Browse the repository at this point in the history
…eters (#3218)
  • Loading branch information
laststylebender14 authored Dec 9, 2024
1 parent dfa6e27 commit c4a572a
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 3 deletions.
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

0 comments on commit c4a572a

Please sign in to comment.