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

Fix Any unpacking to verify types #1023

Merged
merged 2 commits into from
Dec 2, 2024

Conversation

timostamm
Copy link
Member

The function anyUnpack is documented as:

Unpacks the message the Any represents.
Returns undefined if the Any is empty, or if it does not contain the type given by schema.

But instead of returning undefined, the function attempts to unpack an Any that contains a different type:

import {anyPack, anyUnpack} from "@bufbuild/protobuf/wkt";
import {create} from "@bufbuild/protobuf";
import {FooSchema, UserSchema} from "./gen/example_pb";

const user = create(UserSchema, {
  firstName: "John",
});
const any = anyPack(UserSchema, user);
const msg1 = anyUnpack(any, UserSchema); // ok
const msg2 = anyUnpack(any, FooSchema); // runtime error

anyUnpackTo has the same issue. This PR fixes the bug, and adds test coverage.

@timostamm timostamm merged commit eee4072 into main Dec 2, 2024
21 checks passed
@timostamm timostamm deleted the tstamm/Fix-Any-unpacking-to-verify-types branch December 2, 2024 11:01
@timostamm timostamm mentioned this pull request Dec 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants