Skip to content
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

feat(smithy-client): support strict union parsing #2746

Merged
merged 1 commit into from
Sep 9, 2021

Conversation

adamthom-amzn
Copy link
Contributor

Description

Unions must be JSON objects that only have one key set.

Testing

Unit tests, and protocol tests against regenerated clients.


By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@adamthom-amzn adamthom-amzn requested review from JordonPhillips, gosar and a team September 2, 2021 23:59
@codecov-commenter
Copy link

codecov-commenter commented Sep 3, 2021

Codecov Report

❗ No coverage uploaded for pull request base (main@afeccd7). Click here to learn what that means.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##             main    #2746   +/-   ##
=======================================
  Coverage        ?   61.41%           
=======================================
  Files           ?      539           
  Lines           ?    27525           
  Branches        ?     6722           
=======================================
  Hits            ?    16905           
  Misses          ?    10620           
  Partials        ?        0           

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update afeccd7...be99b31. Read the comment docs.

Comment on lines 240 to 245
const setKeys = new Set<string>();
for (const [key, value] of Object.entries(asObject)) {
if (value !== null && value !== undefined) {
setKeys.add(key);
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Is the Set really needed? Since we are validating the object has exactly 1 key, and JS object doesn't allow multiple entries under same key, why not just Object.keys(asObject)?.length !== 1?

Copy link
Contributor Author

@adamthom-amzn adamthom-amzn Sep 8, 2021

Choose a reason for hiding this comment

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

You can have multiple keys where only one is not null - { a: 1, b: null, c: null } is a valid union (and a check of the # of keys would fail in this case, I think)

Set isn't being used here as a way to deduplicate. I just used it because order doesn't matter and object keys are expected to be unique anyway.

Copy link
Contributor

Choose a reason for hiding this comment

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

Since Object.entries already returns a collection, it's looks unnecessary to create a Set explicitly. Will this do?

const isDefined = (value: unknow): boolean => value !== null && value !== undefined;
if (Object.values(asObject).filter(isDefined).length !== 1) {
  throw new TypeError(`Unions must have exactly one non-null member`);
}
return asObject;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'd like to include which keys were non-null in the error message when more than one key is set, which I can't do if I just filter the values.

Copy link
Contributor

Choose a reason for hiding this comment

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

The suggested way is more idiomatic. But we don't feel strong about it.

Copy link
Contributor

@AllanZhengYP AllanZhengYP left a comment

Choose a reason for hiding this comment

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

The use of Set seems unnecessary.

Copy link
Contributor

@AllanZhengYP AllanZhengYP left a comment

Choose a reason for hiding this comment

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

Ship it!

Unions must be JSON objects that only have one key set.
Copy link
Contributor

@AllanZhengYP AllanZhengYP left a comment

Choose a reason for hiding this comment

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

Re-approve

@JordonPhillips JordonPhillips merged commit 1159680 into aws:main Sep 9, 2021
@github-actions
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs and link to relevant comments in this thread.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 24, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants