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

handle duplicate types #45

Merged
merged 4 commits into from
Jan 22, 2024
Merged

Conversation

achim-k
Copy link
Contributor

@achim-k achim-k commented Jan 15, 2024

Public-Facing Changes

Fix exception when message definition contains duplicate types

Description

This PR removes duplicate types (with the same name) to ensure that at max one result is returned when searching a type by its name. This fixes an exception that occurred when the message definition contained duplicate types due to a bug in mcap convert.

@achim-k achim-k requested review from jtbandes and snosenzo January 15, 2024 14:25
// Filter out duplicate types.
// This will avoid that searching a type by name will return more than one result.
const seenTypes = new Set<string>();
const uniqueTypes = types.filter((definition) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

why are we deduping instead of making the search function able to return multiple or return the first that matches?
Should having duplicate types be a schema error?

Copy link
Member

Choose a reason for hiding this comment

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

+1 - seems weird to me that we would have duplicate types. At least it seems like we should confirm they are identical and throw an error if not (or always throw an error)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Turns out that duplicates were erroneously produced by mcap convert, so we have to handle that case. I changed it now to a deep comparison instead of only checking the name.

Copy link
Contributor

@defunctzombie defunctzombie left a comment

Choose a reason for hiding this comment

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

Is this a bug in ROS?

How do we know which definition of the type name is the correct one if it appears twice in the combined definitions format? Like what if the same name had different definitions should we be throwing an error if they don't match?

We've never seen this duplication in a combined definition string have we? I'm trying to understand if this is bad data and we've correctly errored loudly or how this should be handled. My current feedback is that I view the input as malformed.

Separate from that - is it that parsing is failing on this input or something upstream in studio that made this assumption? Maybe it is correct that the type appears twice from the parse output and it is upstream's responsibility to understand that is allowed?

// This will avoid that searching a type by name will return more than one result.
const seenTypes: MessageDefinition[] = [];
const uniqueTypes = types.filter((definition) => {
return seenTypes.find((otherDefinition) => isMsgDefEqual(definition, otherDefinition))
Copy link
Contributor

Choose a reason for hiding this comment

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

is this deduplication as performant as it could be or as performant as _.uniqBy? I guess it's only run once per schema on startup so it's not that big of a deal.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

is this deduplication as performant as it could be or as performant as _.uniqBy?

Probably not. OK to add lodash as a dependency? If so then I'll change it to use `_.uniqBy

Copy link
Contributor

Choose a reason for hiding this comment

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

Can you please not add a dependency to this project for a bug fix?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah I'd avoid adding the dep. Just wanted to see if we could get this down to a single iteration through the types, but it's not strictly necessary

@snosenzo snosenzo self-requested a review January 22, 2024 15:52
@snosenzo
Copy link
Contributor

Actually it doesn't look like to me that the comments were addressed. Why shouldn't we error on these schemas instead of deduping them?

@achim-k
Copy link
Contributor Author

achim-k commented Jan 22, 2024

Actually it doesn't look like to me that the comments were addressed. Why shouldn't we error on these schemas instead of deduping them?

It turned out that mcap convert was erroneously producing schemas with duplicate types. This change is mainly necessary to handle existing mcap files that were converted like that.

src/parse.ts Outdated
Comment on lines 83 to 84
// Filter out duplicate types.
// This will avoid that searching a type by name will return more than one result.
Copy link
Member

Choose a reason for hiding this comment

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

Would be helpful to add some historical context about mcap convert, to explain why we shouldn't delete this code

@achim-k achim-k merged commit a1b8678 into main Jan 22, 2024
2 checks passed
@achim-k achim-k deleted the achim/fg-6275-rosmsg-handle-duplicate-types branch January 22, 2024 21:17
@achim-k achim-k mentioned this pull request Jan 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

5 participants