-
Notifications
You must be signed in to change notification settings - Fork 1k
fix(deploy/functions): improve list param parsing robustness #8895
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
f51f761
87ae2c9
960ef0f
91e6fb9
22ff3f9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -268,7 +268,7 @@ | |
return pv; | ||
} | ||
|
||
setDelimiter(delimiter: string) { | ||
this.delimiter = delimiter; | ||
} | ||
|
||
|
@@ -291,15 +291,37 @@ | |
} | ||
|
||
asList(): string[] { | ||
// Handle something like "['a', 'b', 'c']" | ||
if (this.rawValue.includes("[")) { | ||
// Convert quotes to apostrophes | ||
const unquoted = this.rawValue.replace(/'/g, '"'); | ||
return JSON.parse(unquoted); | ||
let modifiedValue = this.rawValue; | ||
|
||
// Handle something like "["a", "b", "c"]" | ||
if (modifiedValue.startsWith("[") && modifiedValue.endsWith("]")) { | ||
try { | ||
const parsed = JSON.parse(modifiedValue); | ||
if (Array.isArray(parsed)) { | ||
// The return type is string[], so we must convert all elements to strings. | ||
return parsed.map((elem: any) => { | ||
if (elem === null) { | ||
// String(null) is "null", which is what we want. | ||
return "null"; | ||
} | ||
if (typeof elem === "object") { | ||
// String(obj) is "[object Object]", JSON.stringify is more useful. | ||
// Avoid inserting spaces after commas for objects/arrays | ||
return JSON.stringify(elem); | ||
} | ||
return String(elem); | ||
Comment on lines
+303
to
+312
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This block of code can be simplified. The if (typeof elem === "object") {
// This correctly handles objects, arrays, and null.
// For objects/arrays, JSON.stringify is more useful than the default String() conversion.
// For null, JSON.stringify(null) results in "null", which is what we want.
return JSON.stringify(elem);
}
return String(elem); |
||
}); | ||
} | ||
// It's valid JSON but not an array (e.g. a string literal "foo,bar"). | ||
// Fall through to splitting by delimiter. | ||
} catch (e) { | ||
// Malformed JSON, e.g. "[a, b, c]". Remove brackets and fall through. | ||
modifiedValue = modifiedValue.slice(1, -1); | ||
} | ||
} | ||
|
||
// Continue to handle something like "a,b,c" | ||
return this.rawValue.split(this.delimiter); | ||
return modifiedValue.split(this.delimiter); | ||
} | ||
|
||
asNumber(): number { | ||
|
@@ -413,7 +435,7 @@ | |
} | ||
if (paramDefault && !canSatisfyParam(param, paramDefault)) { | ||
throw new FirebaseError( | ||
"Parameter " + param.name + " has default value " + paramDefault + " of wrong type", | ||
); | ||
} | ||
paramValues[param.name] = await promptParam(param, firebaseConfig.projectId, paramDefault); | ||
|
@@ -460,7 +482,7 @@ | |
* to read its environment variables. They are instead provided through GCF's own | ||
* Secret Manager integration. | ||
*/ | ||
async function handleSecret(secretParam: SecretParam, projectId: string) { | ||
const metadata = await secretManager.getSecretMetadata(projectId, secretParam.name, "latest"); | ||
if (!metadata.secret) { | ||
const secretValue = await password({ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ha this is clever way to parse arrays.