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

Fixes for complex predefined rule types #210

Merged
merged 2 commits into from
Nov 20, 2024

Conversation

jchadwick-buf
Copy link
Member

Updates the protovalidate version to 0.8.2 and fixes three closely-related bugs:

  • rule is inadvertently typed incorrectly when it is a repeated rule field on a repeated field
  • getCELType inadvertently returns an incorrectly-named message type when dealing with a message field (using the full name of the field instead of the full name of the type)
  • getCELType returns a message type instead of a wrapper type for wrappers like google.protobuf.Int32Value

These bugs are subtle because they don't occur in normal circumstances, but the new predefined rule tests added in v0.8.2 to close the bugs on protovalidate-go inadvertently revealed several protovalidate-java bugs. This validates that more work on the conformance test can still be quite valuable in finding difficult bugs, even if we don't know exactly what we're looking for.

@jchadwick-buf jchadwick-buf requested review from jhump and Alfus November 20, 2024 18:22
@@ -243,7 +243,7 @@ private Env getRuleEnv(
Variable.RULES_NAME,
Decls.newObjectType(constraintMessage.getDescriptorForType().getFullName())),
Decls.newVar(
Variable.RULE_NAME, DescriptorMappings.getCELType(constraintFieldDesc, forItems))));
Variable.RULE_NAME, DescriptorMappings.getCELType(constraintFieldDesc, false))));
Copy link
Member Author

Choose a reason for hiding this comment

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

Explanation: The forItems in this context is for the field, not the rule. We never act on the nested items of the rule individually, so forItems should always be false for the rule type.

return Decls.newWrapperType(Decls.String);
case "google.protobuf.UInt32Value":
case "google.protobuf.UInt64Value":
return Decls.newWrapperType(Decls.Uint);
Copy link
Member Author

Choose a reason for hiding this comment

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

Explanation: In most cases, the wrapper type is "unwrapped" by having its constraints processed in processWrapperConstraints; in other cases (e.g. nested in a message), it is handled by cel-java. However, in the case of the rule, we might be dealing with a wrapper or repeated wrapper directly. Therefore,getCELType needs to be aware of wrappers.

default:
return Decls.newObjectType(fieldDescriptor.getFullName());
return Decls.newObjectType(fqn);
Copy link
Member Author

@jchadwick-buf jchadwick-buf Nov 20, 2024

Choose a reason for hiding this comment

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

Explanation: newObjectType sets the typeName field passed to setMessageType. I think in most cases this field doesn't actually matter (presumably because cel-java is doing protobuf reflection on the runtime value anyways) but having this set wrong makes the error messages unintelligible. Set it to the fqn of the message type instead of the field descriptor full name.

@jchadwick-buf jchadwick-buf merged commit 460804a into main Nov 20, 2024
4 checks passed
@jchadwick-buf jchadwick-buf deleted the jchadwick/predefined-fixes branch November 20, 2024 18:51
@jchadwick-buf jchadwick-buf changed the title Fixes for complex predefined field types Fixes for complex predefined rule types Nov 20, 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