From 49b1d0afb9e290ef86f2a8209f234e514ffebbd5 Mon Sep 17 00:00:00 2001
From: tada5hi <peter.placzek1996@gmail.com>
Date: Fri, 28 Oct 2022 21:05:33 +0200
Subject: [PATCH] fix: only apply query parameter if options are defined

---
 package-lock.json         | 14 +++++++-------
 package.json              |  2 +-
 src/query/module.ts       | 37 +++++++++++++++++++++++++++++++++++++
 src/query/utils/index.ts  |  1 +
 src/query/utils/option.ts | 20 ++++++++++++++++++++
 5 files changed, 66 insertions(+), 8 deletions(-)
 create mode 100644 src/query/utils/option.ts

diff --git a/package-lock.json b/package-lock.json
index c316a138..67c054d8 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -11,7 +11,7 @@
             "dependencies": {
                 "@faker-js/faker": "^7.6.0",
                 "locter": "^0.3.2",
-                "rapiq": "^0.3.1",
+                "rapiq": "^0.4.1",
                 "reflect-metadata": "^0.1.13",
                 "yargs": "^17.6.0"
             },
@@ -12540,9 +12540,9 @@
             }
         },
         "node_modules/rapiq": {
-            "version": "0.3.1",
-            "resolved": "https://registry.npmjs.org/rapiq/-/rapiq-0.3.1.tgz",
-            "integrity": "sha512-3wwkLq27fYwKS4R0crrYj/5YkcZQQQyGkCj8z8K7Fe3R22jdXXBeVyDGJUB3IN+kSifqlt03834lSilLmMbMrQ==",
+            "version": "0.4.1",
+            "resolved": "https://registry.npmjs.org/rapiq/-/rapiq-0.4.1.tgz",
+            "integrity": "sha512-0U9uDbBitAyUEnm0ZS+8nwIB137hth0aVAMUXMn1KxRCs8edelehmhpdes360sNk9AEMTx9AZaBLLlsywKS8lQ==",
             "dependencies": {
                 "minimatch": "^5.1.0",
                 "smob": "^0.0.6"
@@ -24272,9 +24272,9 @@
             "dev": true
         },
         "rapiq": {
-            "version": "0.3.1",
-            "resolved": "https://registry.npmjs.org/rapiq/-/rapiq-0.3.1.tgz",
-            "integrity": "sha512-3wwkLq27fYwKS4R0crrYj/5YkcZQQQyGkCj8z8K7Fe3R22jdXXBeVyDGJUB3IN+kSifqlt03834lSilLmMbMrQ==",
+            "version": "0.4.1",
+            "resolved": "https://registry.npmjs.org/rapiq/-/rapiq-0.4.1.tgz",
+            "integrity": "sha512-0U9uDbBitAyUEnm0ZS+8nwIB137hth0aVAMUXMn1KxRCs8edelehmhpdes360sNk9AEMTx9AZaBLLlsywKS8lQ==",
             "requires": {
                 "minimatch": "^5.1.0",
                 "smob": "^0.0.6"
diff --git a/package.json b/package.json
index 13fb99a5..a99747d8 100644
--- a/package.json
+++ b/package.json
@@ -49,7 +49,7 @@
     "dependencies": {
         "@faker-js/faker": "^7.6.0",
         "locter": "^0.3.2",
-        "rapiq": "^0.3.1",
+        "rapiq": "^0.4.1",
         "reflect-metadata": "^0.1.13",
         "yargs": "^17.6.0"
     },
diff --git a/src/query/module.ts b/src/query/module.ts
index 2a964fc7..436a0ba1 100644
--- a/src/query/module.ts
+++ b/src/query/module.ts
@@ -8,6 +8,7 @@ import {
     applyQuerySortParseOutput,
 } from './parameter';
 import { QueryApplyOptions, QueryApplyOutput } from './type';
+import { isQueryOptionDefined } from './utils';
 
 export function applyQueryParseOutput<T extends ObjectLiteral = ObjectLiteral>(
     query: SelectQueryBuilder<T>,
@@ -49,10 +50,46 @@ export function applyQuery<T extends ObjectLiteral = ObjectLiteral>(
     input: unknown,
     options?: QueryApplyOptions<T>,
 ) : QueryApplyOutput {
+    options = options || {};
+
     if (options.defaultAlias) {
         options.defaultPath = options.defaultAlias;
     }
 
+    if (
+        typeof options.fields === 'undefined' ||
+        !isQueryOptionDefined(options.fields, ['allowed', 'default'])
+    ) {
+        options.fields = false;
+    }
+
+    if (
+        typeof options.filters === 'undefined' ||
+        !isQueryOptionDefined(options.fields, ['allowed', 'default'])
+    ) {
+        options.filters = false;
+    }
+
+    if (
+        typeof options.pagination === 'undefined'
+    ) {
+        options.pagination = false;
+    }
+
+    if (
+        typeof options.relations === 'undefined' ||
+        !isQueryOptionDefined(options.relations, ['allowed'])
+    ) {
+        options.relations = false;
+    }
+
+    if (
+        typeof options.sort === 'undefined' ||
+        !isQueryOptionDefined(options.sort, ['allowed', 'default'])
+    ) {
+        options.sort = false;
+    }
+
     const output = applyQueryParseOutput(query, parseQuery(input, options));
 
     return {
diff --git a/src/query/utils/index.ts b/src/query/utils/index.ts
index cc41237f..c2aa7a98 100644
--- a/src/query/utils/index.ts
+++ b/src/query/utils/index.ts
@@ -1,2 +1,3 @@
 export * from './alias';
 export * from './key';
+export * from './option';
diff --git a/src/query/utils/option.ts b/src/query/utils/option.ts
new file mode 100644
index 00000000..3afe2141
--- /dev/null
+++ b/src/query/utils/option.ts
@@ -0,0 +1,20 @@
+import { hasOwnProperty } from '../../utils';
+
+export function isQueryOptionDefined(
+    input: Record<string, any> | boolean,
+    option: string | string[],
+) {
+    if (typeof input === 'boolean') {
+        return false;
+    }
+
+    const options = Array.isArray(option) ? option : [option];
+
+    for (let i = 0; i < options.length; i++) {
+        if (hasOwnProperty(input, options[i])) {
+            return true;
+        }
+    }
+
+    return false;
+}