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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

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.

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.

1 participant