Skip to content
This repository was archived by the owner on Nov 18, 2025. It is now read-only.

fix: make sure we do not change casing for fields that are user inputs#1299

Merged
alexander-fenster merged 12 commits intomainfrom
fixCamelCase
Aug 17, 2022
Merged

fix: make sure we do not change casing for fields that are user inputs#1299
alexander-fenster merged 12 commits intomainfrom
fixCamelCase

Conversation

@sofisl
Copy link
Contributor

@sofisl sofisl commented Jul 21, 2022

@sofisl sofisl requested a review from a team as a code owner July 21, 2022 22:32
@product-auto-label product-auto-label bot added the size: m Pull request size is medium. label Jul 21, 2022
// input and revert back to its original form
if (
fieldsToChangeWithFunc &&
!fieldsToChangeWithFunc?.includes(convertedField)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cool!

// request is supposed to have keys in camelCase.
const snakeRequest = requestChangeCaseAndCleanup(request, camelToSnakeCase);
let fieldsToChange = undefined;
if (requestFields) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, just to save on some if statements. It's ok to leave it as is.

if (
field &&
fields[field] &&
fields[field].resolvedType &&
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This all can be shortened to (fields?.[field]?.resolvedType as Type)?.fields

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually, something like

const fields = (fields?.[field]?.resolvedType as Type)?.fields;
if (fields) {
  getAllFieldNames(fields, fieldNames);
}

const snakeRequest = requestChangeCaseAndCleanup(
request,
camelToSnakeCase,
fieldsToChange
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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?

@ahtasham637
Copy link

Waiting for this PR to get merged 🤞

// input and revert back to its original form
if (
fieldsToChange &&
fieldsToChange?.size !== 0 &&
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The size check is technically not needed, .has() is a no-op for an empty set.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

}

// This function gets all the fields recursively
export function getAllFieldNames(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we actually using this function anywhere else? If not, let's not export it.

Copy link
Contributor

@alexander-fenster alexander-fenster left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! I left minor comments, it's OK to merge.

@alexander-fenster alexander-fenster merged commit 9b73ddd into main Aug 17, 2022
@alexander-fenster alexander-fenster deleted the fixCamelCase branch August 17, 2022 21:39
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

size: m Pull request size is medium.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Creating VM instance with labels fails

3 participants