Skip to content

Commit

Permalink
qe: Fix batch results processing for enums (#4072)
Browse files Browse the repository at this point in the history
* qe: Fix batch results processing for enums

When JSON protocol is used and batching is happening, if the unique
value is enum, we incorrectly compared the results with the inputs:
inputs are PrismaValue::String, while outputs are PrismaValue::Enum.

Fix prisma/prisma#20227

* Fix mongo tests
  • Loading branch information
SevInf committed Jul 18, 2023
1 parent e06258d commit 6bdd55c
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,46 @@ mod singular_batch {
Ok(())
}

fn enum_id() -> String {
let schema = indoc! {
r#"
enum IdEnum {
A
B
}
model TestModel {
#id(id, IdEnum, @id)
}
"#
};

schema.to_owned()
}

#[connector_test(schema(enum_id), capabilities(Enums))]
async fn batch_enum(runner: Runner) -> TestResult<()> {
run_query!(&runner, r#"mutation { createOneTestModel(data: { id: "A" }) { id } }"#);
run_query!(&runner, r#"mutation { createOneTestModel(data: { id: "B" }) { id } }"#);

let (res, compact_doc) = compact_batch(
&runner,
vec![
r#"{ findUniqueTestModel(where: { id: "A" }) { id } }"#.to_string(),
r#"{ findUniqueTestModel(where: { id: "B" }) { id } }"#.to_string(),
],
)
.await?;

insta::assert_snapshot!(
res.to_string(),
@r###"{"batchResult":[{"data":{"findUniqueTestModel":{"id":"A"}}},{"data":{"findUniqueTestModel":{"id":"B"}}}]}"###
);
assert!(compact_doc.is_compact());

Ok(())
}

// Regression test for https://github.com/prisma/prisma/issues/16548
#[connector_test(schema(schemas::generic))]
async fn repro_16548(runner: Runner) -> TestResult<()> {
Expand Down
5 changes: 5 additions & 0 deletions query-engine/request-handlers/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ impl<'a> RequestHandler<'a> {
/// - DateTime/String: User-input: DateTime / Response: String
/// - Int/BigInt: User-input: Int / Response: BigInt
/// - (JSON protocol only) Custom types (eg: { "$type": "BigInt", value: "1" }): User-input: Scalar / Response: Object
/// - (JSON protocol only) String/Enum: User-input: String / Response: Enum
/// This should likely _not_ be used outside of this specific context.
fn compare_values(left: &ArgumentValue, right: &ArgumentValue) -> bool {
match (left, right) {
Expand All @@ -249,6 +250,10 @@ impl<'a> RequestHandler<'a> {
| (ArgumentValue::Scalar(PrismaValue::BigInt(i2)), ArgumentValue::Scalar(PrismaValue::Int(i1))) => {
*i1 == *i2
}
(ArgumentValue::Scalar(PrismaValue::Enum(s1)), ArgumentValue::Scalar(PrismaValue::String(s2)))
| (ArgumentValue::Scalar(PrismaValue::String(s1)), ArgumentValue::Scalar(PrismaValue::Enum(s2))) => {
*s1 == *s2
}
(ArgumentValue::Object(t1), t2) | (t2, ArgumentValue::Object(t1)) => match Self::unwrap_value(t1) {
Some(t1) => Self::compare_values(t1, t2),
None => left == right,
Expand Down

0 comments on commit 6bdd55c

Please sign in to comment.