stargate / data-api

JSON document API for Apache Cassandra (formerly known as JSON API)
https://stargate.io
Apache License 2.0
14 stars 16 forks source link

`InsertMany` with `returnDocumentResponses` flag enabled invalid input processing. #1670

Open clun opened 2 weeks ago

clun commented 2 weeks ago

Given the Table:

CREATE TABLE default_keyspace.table_composite_pk (
    id text,
    name text,
    age int,
    PRIMARY KEY ((id, name))
)

1. Default insertion

{
  "insertMany": {
    "documents": [
      {
        "age": 22,
        "name": "John",
        "id": "Connor"
      },
      {
        "age": 50,
        "name": "Sara",
        "id": "Connor"
      },
      {
        "age": 50,
        "name": "Doctor",
        "id": "Silberman"
      }
    ],
    "options": {
      "ordered": false,
      "returnDocumentResponses": false
    }
  }
}
{
  "status": {
    "primaryKeySchema": {
      "name": {
        "type": "text"
      },
      "id": {
        "type": "text"
      }
    },
    "insertedIds": [
      [
        "John",
        "Connor"
      ],
      [
        "Sara",
        "Connor"
      ],
      [
        "Doctor",
        "Silberman"
      ]
    ]
  }
}

=> All good the insertedIds are populated

2. Default insertion with invalid input

[!NOTE] Notice the age provided as a string in the 3rd record

{
  "insertMany": {
    "documents": [
      {
        "age": 22,
        "name": "John",
        "id": "Connor"
      },
      {
        "age": 50,
        "name": "Sara",
        "id": "Connor"
      },
      {
        "age": "50",  # <- error introduced on purpose
        "name": "Doctor",
        "id": "Silberman"
      }
    ],
    "options": {
      "ordered": false,
      "returnDocumentResponses": false
    }
  }
}
{
  "status": {
    "primaryKeySchema": {
      "name": {
        "type": "text"
      },
      "id": {
        "type": "text"
      }
    },
    "insertedIds": [
      [
        "John",
        "Connor"
      ],
      [
        "Sara",
        "Connor"
      ]
    ]
  },
  "errors": [
    {
      "stackTrace": [],
      "message": "Only values that are supported by the column data type can be included when inserting a document into a table.\n\nThe table default_keyspace.table_composite_pk defines the columns: id(text), name(text), age(int).\nThe request included the following columns that had values that are invalid: age: Error trying to convert to targetCQLType `INT` from value.class `java.lang.String`, value \"50\". Root cause: no codec matching value type.\n\nResend the request using only supported column values.",
      "errorCode": "INVALID_COLUMN_VALUES",
      "errorMessage": "(INVALID_COLUMN_VALUES) Only values that are supported by the column data type can be included when inserting a document into a table.\n\nThe table default_keyspace.table_composite_pk defines the columns: id(text), name(text), age(int).\nThe request included the following columns that had values that are invalid: age: Error trying to convert to targetCQLType `INT` from value.class `java.lang.String`, value \"50\". Root cause: no codec matching value type.\n\nResend the request using only supported column values.",
      "suppressed": [],
      "localizedMessage": "Only values that are supported by the column data type can be included when inserting a document into a table.\n\nThe table default_keyspace.table_composite_pk defines the columns: id(text), name(text), age(int).\nThe request included the following columns that had values that are invalid: age: Error trying to convert to targetCQLType `INT` from value.class `java.lang.String`, value \"50\". Root cause: no codec matching value type.\n\nResend the request using only supported column values."
    }
  ]
}

All good 2 insertedID and an explicit error.

3. Insertion with returnDocumentResponse flag

{
  "insertMany": {
    "documents": [
      {
        "age": 22,
        "name": "John",
        "id": "Connor"
      },
      {
        "age": 50,
        "name": "Sara",
        "id": "Connor"
      },
      {
        "age": 50,
        "name": "Doctor",
        "id": "Silberman"
      }
    ],
    "options": {
      "ordered": false,
      "returnDocumentResponses": true
    }
  }
}
{
  "status": {
    "primaryKeySchema": {
      "name": {
        "type": "text"
      },
      "id": {
        "type": "text"
      }
    },
    "documentResponses": [
      {
        "status": "OK",
        "_id": [
          "John",
          "Connor"
        ]
      },
      {
        "status": "OK",
        "_id": [
          "Sara",
          "Connor"
        ]
      },
      {
        "status": "OK",
        "_id": [
          "Doctor",
          "Silberman"
        ]
      }
    ]
  }
}

All good, you can notice out insertedids is empty replaced by documentResponses.

4. Insertion with returnDocumentResponse flag and invalid input

[!NOTE] We are trying to create errors in returnDocumentResponses to see a status KO of the shape of the output

IN:

{
  "insertMany": {
    "documents": [
      {
        "age": 22,
        "name": "John",
        "id": "Connor"
      },
      {
        "age": 50,
        "name": "Sara",
        "id": "Connor"
      },
      {
        "age": "50",       # <- error introduced on purpose
        "name": "Doctor",
        "id": "Silberman"
      }
    ],
    "options": {
      "ordered": false,
      "returnDocumentResponses": true
    }
  }
}

Out

:boom:

com.datastax.astra.client.exception.DataAPIException: [CLIENT_HTTP] - {"errors":[{"message":"Server failed: root cause: (java.util.NoSuchElementException) No value present","errorCode":"SERVER_UNHANDLED_ERROR"}]} (http:500)