Skip to content

Commit

Permalink
Fix update on embedded docs
Browse files Browse the repository at this point in the history
  • Loading branch information
SamuelTallet committed Mar 4, 2022
1 parent e2deb0f commit 0bd4d2f
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 20 deletions.
2 changes: 1 addition & 1 deletion index.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Capsule\Factory\ServerRequestFactory;
use Limber\Exceptions\NotFoundHttpException;

const VERSION = '1.2.7';
const VERSION = '1.2.8';

/**
* Absolute path, without trailing slash.
Expand Down
3 changes: 1 addition & 2 deletions source/js/jsonViewModified.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,8 @@ var JsonView = (function (exports) {
} else {

// XXX Modification made for MongoDB PHP GUI.
if ( node.key === '_id' ) {
if ( node.key === '_id' && node.depth === 2 ) {
MPG.documentId = node.value;
MPG.documentIdType = _typeof(node.value);
}
if ( node.depth >= 2 && node.depth <= 5 && node.key !== '_id' ) {
var documentFieldIsUpdatable = true;
Expand Down
14 changes: 1 addition & 13 deletions source/js/queryDatabase.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,6 @@ MPG.collectionFields = [];
*/
MPG.documentId = '';

/**
* Type of document ID.
* XXX Used by JsonView parser.
*
* @type {string}
*/
MPG.documentIdType = '';

/**
* Cached output.
*
Expand Down Expand Up @@ -517,11 +509,7 @@ MPG.eventListeners.addUpdate = function() {
documentFieldNewValue, documentField.dataset.documentFieldType
);

if ( MPG.documentIdType === 'number' ) {
var documentId = parseInt(documentField.dataset.documentId);
} else {
var documentId = documentField.dataset.documentId;
}
var documentId = documentField.dataset.documentId;

var requestBody = {
'databaseName': MPG.databaseName,
Expand Down
12 changes: 8 additions & 4 deletions source/php/MPG/DocumentsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -287,10 +287,14 @@ public function updateOne() : JsonResponse {
return new JsonResponse(400, ErrorNormalizer::normalize($th, __METHOD__));
}

if ( isset($decodedRequestBody['filter']['_id'])
&& preg_match(MongoDBHelper::OBJECT_ID_REGEX, $decodedRequestBody['filter']['_id']) ) {
$decodedRequestBody['filter']['_id'] =
new \MongoDB\BSON\ObjectId($decodedRequestBody['filter']['_id']);
if ( isset($decodedRequestBody['filter']['_id']) ) {

if ( preg_match(MongoDBHelper::OBJECT_ID_REGEX, $decodedRequestBody['filter']['_id']) ) {
$decodedRequestBody['filter']['_id'] = new \MongoDB\BSON\ObjectId($decodedRequestBody['filter']['_id']);
} elseif ( preg_match(MongoDBHelper::UINT_REGEX, $decodedRequestBody['filter']['_id']) ) {
$decodedRequestBody['filter']['_id'] = intval($decodedRequestBody['filter']['_id']);
}

}

foreach ($decodedRequestBody['update']['$set'] as &$updateValue) {
Expand Down
7 changes: 7 additions & 0 deletions source/php/MPG/MongoDBHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ class MongoDBHelper {
*/
public const OBJECT_ID_REGEX = '/^[a-f\d]{24}$/i';

/**
* Regular expression for an unsigned integer.
*
* @var string
*/
public const UINT_REGEX = '/^(0|[1-9][0-9]*)$/';

/**
* Regular expression for an ISO date-time.
*
Expand Down

0 comments on commit 0bd4d2f

Please sign in to comment.