fix: make sure we do not change casing for fields that are user inputs#1299
fix: make sure we do not change casing for fields that are user inputs#1299alexander-fenster merged 12 commits intomainfrom
Conversation
src/transcoding.ts
Outdated
| // input and revert back to its original form | ||
| if ( | ||
| fieldsToChangeWithFunc && | ||
| !fieldsToChangeWithFunc?.includes(convertedField) |
There was a problem hiding this comment.
This is going to take O(N²): for each field to check, includes() will iterate the whole fieldsToChangeWithFunc. If you change fieldsToChangeWithFunc to a Set<string>, the .has() check will be faster.
| // request is supposed to have keys in camelCase. | ||
| const snakeRequest = requestChangeCaseAndCleanup(request, camelToSnakeCase); | ||
| let fieldsToChange = undefined; | ||
| if (requestFields) { |
There was a problem hiding this comment.
Let's make requestFields required. I think transcode() is only called from here, and it's completely reasonable to assume this parameter is always required.
transcode is not exported publicly so it's an internal change that won't be breaking.
There was a problem hiding this comment.
This would break a lot of tests, so it implies a different expected behavior. Is there a specific reason you want to make it required?
There was a problem hiding this comment.
Nope, just to save on some if statements. It's ok to leave it as is.
src/transcoding.ts
Outdated
| if ( | ||
| field && | ||
| fields[field] && | ||
| fields[field].resolvedType && |
There was a problem hiding this comment.
This all can be shortened to (fields?.[field]?.resolvedType as Type)?.fields
There was a problem hiding this comment.
actually, something like
const fields = (fields?.[field]?.resolvedType as Type)?.fields;
if (fields) {
getAllFieldNames(fields, fieldNames);
}
src/transcoding.ts
Outdated
| const snakeRequest = requestChangeCaseAndCleanup( | ||
| request, | ||
| camelToSnakeCase, | ||
| fieldsToChange |
There was a problem hiding this comment.
The set should be built here, otherwise, you keep making the set from the list on each invocation of the recursive function. You can just do it once here and pass it to the requestChangeCaseAndCleanup.
There was a problem hiding this comment.
@alexander-fenster, Set does not have the map function we use in line 246. So our options are to either take a Set as a parameter and iterate through the elements with a loop and reassign, or else what we have here - what would you prefer?
|
Waiting for this PR to get merged 🤞 |
| // input and revert back to its original form | ||
| if ( | ||
| fieldsToChange && | ||
| fieldsToChange?.size !== 0 && |
There was a problem hiding this comment.
The size check is technically not needed, .has() is a no-op for an empty set.
There was a problem hiding this comment.
This was breaking tests, because it would enter this comparison if there was an empty set, and, since it would find no match, this would evaluate to true, instead of false: !fieldsToChange?.has(convertedField)
thus reverting convertedField back to field.
src/transcoding.ts
Outdated
| } | ||
|
|
||
| // This function gets all the fields recursively | ||
| export function getAllFieldNames( |
There was a problem hiding this comment.
Are we actually using this function anywhere else? If not, let's not export it.
alexander-fenster
left a comment
There was a problem hiding this comment.
LGTM! I left minor comments, it's OK to merge.
Fixes googleapis/google-cloud-node-core#314