Repository: meilisearch/meilisearch
File: meilisearch-http/tests/documents/add_documents.rs (Rust)

Code before:
    assert_eq!(
        response["message"],
        json!(
            r#"The `json` payload provided is malformed. `Couldn't serialize document value: key must be a string at line 1 column 14`."#
        )
    );
    assert_eq!(response["code"], json!("malformed_payload"));
    assert_eq!(response["type"], json!("invalid_request"));
    assert_eq!(
        response["link"],
        json!("https://docs.meilisearch.com/errors#malformed_payload")
    );

    // truncate

    // length = 100
    let long = String::from_utf8(
        "0123456789"
            .as_bytes()
            .iter()
            .cycle()
            .cloned()
            .take(100)
            .collect_vec(),
    )
    .unwrap();

    let document = format!("\"{}\"", long);
    let req = test::TestRequest::put()
        .uri("/indexes/dog/documents")
        .set_payload(document)
        .insert_header(("content-type", "application/json"))
        .to_request();
    let res = test::call_service(&app, req).await;
    let body = test::read_body(res).await;
    dbg!(&body);
    let response: Value = serde_json::from_slice(&body).unwrap_or_default();
    dbg!(&response);
    assert_eq!(status_code, 400);
    assert_eq!(
        response["message"],
        json!(
            r#"The `json` payload provided is malformed. `Couldn't serialize document value: invalid type: string "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789", expected a documents, or a sequence of documents. at line 1 column 102`."#
        )
    );
    assert_eq!(response["code"], json!("malformed_payload"));
    assert_eq!(response["type"], json!("invalid_request"));
    assert_eq!(
        response["link"],
        json!("https://docs.meilisearch.com/errors#malformed_payload")
    );

Review comment (suggestion):
```suggestion
    let long = "0123456789".repeat(10);
```

Code after:
        response["message"],
        json!(
            r#"The `json` payload provided is malformed. `Couldn't serialize document value: key must be a string at line 1 column 14`."#
        )
    );
    assert_eq!(response["code"], json!("malformed_payload"));
    assert_eq!(response["type"], json!("invalid_request"));
    assert_eq!(
        response["link"],
        json!("https://docs.meilisearch.com/errors#malformed_payload")
    );

    // truncate

    // length = 100
    let long = "0123456789".repeat(10);

    let document = format!("\"{}\"", long);
    let req = test::TestRequest::put()
        .uri("/indexes/dog/documents")
        .set_payload(document)
        .insert_header(("content-type", "application/json"))
        .to_request();
    let res = test::call_service(&app, req).await;
    let body = test::read_body(res).await;
    let response: Value = serde_json::from_slice(&body).unwrap_or_default();
    assert_eq!(status_code, 400);
    assert_eq!(
        response["message"],
        json!(
            r#"The `json` payload provided is malformed. `Couldn't serialize document value: invalid type: string "0123456789012345678901234567...890123456789", expected a documents, or a sequence of documents. at line 1 column 102`."#
        )
    );
    assert_eq!(response["code"], json!("malformed_payload"));
    assert_eq!(response["type"], json!("invalid_request"));
    assert_eq!(
        response["link"],
        json!("https://docs.meilisearch.com/errors#malformed_payload")
    );

    // add one more char to the long string to test if the truncating works.
    let document = format!("\"{}m\"", long);
    let req = test::TestRequest::put()
        .uri("/indexes/dog/documents")
        .set_payload(document)
        .insert_header(("content-type", "application/json"))
        .to_request();
    let res = test::call_service(&app, req).await;
    let body = test::read_body(res).await;
    let response: Value = serde_json::from_slice(&body).unwrap_or_default();
    assert_eq!(status_code, 400);