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: Validator supports automatic switch #13923

Closed
wants to merge 4 commits into from
Closed

Conversation

aofall
Copy link
Contributor

@aofall aofall commented Mar 15, 2024

What is the purpose of the change

In the newer versions of Hibernate-validator, the package names of the relevant validation implementation classes have been migrated from javax to jakarta.

Dubbo has been support new validator with jakarta in #9552 , but the setting of default validator is implement with javax, if the user is not configured to use a new validator under the SpringBoot3, an NoClassDefFoundError will be throw.

If the validator supports automatic switch, this will reduce a lot of configuration processes for users.

related with #12401 #12465

Brief changelog

Verifying this change

Checklist

  • Make sure there is a GitHub_issue field for the change (usually before you start working on it). Trivial changes like typos do not require a GitHub issue. Your pull request should address just this issue, without pulling in other changes - one PR resolves one issue.
  • Each commit in the pull request should have a meaningful subject line and body.
  • Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
  • Check if is necessary to patch to Dubbo 3 if you are work on Dubbo 2.7
  • Write necessary unit-test to verify your logic correction, more mock a little better when cross module dependency exist. If the new feature or significant change is committed, please remember to add sample in dubbo samples project.
  • Add some description to dubbo-website project if you are requesting to add a feature.
  • GitHub Actions works fine on your own branch.
  • If this contribution is large, please follow the Software Donation Guide.

*/
@Override
protected Validator createValidator(URL url) {
return new JValidator(url);
List<Class<? extends Validator>> validatorList = Arrays.asList(JValidator.class, JValidatorNew.class);
Copy link
Member

Choose a reason for hiding this comment

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

Use Class.forName

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Did you meaning loading JValidator.class and JValidatorNew.class classes with Class.forName(String) like this? Or could you please provided more detail info

try {
    Class<? extends Validator> validCls = (Class<? extends Validator>) Class.forName("org.apache.dubbo.validation.support.jvalidation.JValidator");
    Class<? extends Validator> newValidCls = (Class<? extends Validator>) Class.forName("org.apache.dubbo.validation.support.jvalidation.JValidatorNew");
} catch (Throwable ignore) {
}

By the way, the JValidator.java#316 and JValidatorNew.java#316 need to be modified together?

Copy link
Member

Choose a reason for hiding this comment

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

Yes.
A more conscise way is to use org.apache.dubbo.common.extension.Activate#onClass and create two filter for javax and jakarta

@aofall aofall deleted the branch apache:3.2 April 9, 2024 06:02
@aofall aofall closed this Apr 9, 2024
@aofall aofall deleted the 3.2 branch April 9, 2024 06:02
Copy link

sonarqubecloud bot commented Apr 9, 2024

@aofall
Copy link
Contributor Author

aofall commented Apr 12, 2024

A more conscise way is to use org.apache.dubbo.common.extension.Activate#onClass and create two filter for javax and jakarta

@AlbumenJ I try to use Activate#onClass on two filter class for javax and jakarta, but found another problem.

The Validation extensions was mark the jvalidation by default extension by the @SPI("jvalidation") annotation on class.

If use @Activate(onClass = "javax.validation.Validation") on the org.apache.dubbo.validation.support.jvalidation.JValidation class, this extension will not loaded, and because the declare default extension, it will throw the IllegalStateException(No such extension jvalidation....) in org.apache.dubbo.common.extension.ExtensionLoader#createExtension if the config dubbo.provider/consumer.validation is true (because the named true not a extension and try to loaded the default extension jvalidation).

If remove the default extension declared in org.apache.dubbo.validation.Validation, the org.apache.dubbo.validation.Validation#getValidator will attempt to load the extension with the corresponding name from the relevant configurations what dubbo.provider/consumer.validation named , but the official document of provider and consumer defines the parameter type of validation as Boolean, it's unexpect for dubbo-samples

As long as fill in any characters into dubbo.provider/consumer.validation configuration, verification will be enabled and loaded the default extension jvalidation

@AlbumenJ
Copy link
Member

AlbumenJ commented May 6, 2024

A more conscise way is to use org.apache.dubbo.common.extension.Activate#onClass and create two filter for javax and jakarta

@AlbumenJ I try to use Activate#onClass on two filter class for javax and jakarta, but found another problem.

The Validation extensions was mark the jvalidation by default extension by the @SPI("jvalidation") annotation on class.

If use @Activate(onClass = "javax.validation.Validation") on the org.apache.dubbo.validation.support.jvalidation.JValidation class, this extension will not loaded, and because the declare default extension, it will throw the IllegalStateException(No such extension jvalidation....) in org.apache.dubbo.common.extension.ExtensionLoader#createExtension if the config dubbo.provider/consumer.validation is true (because the named true not a extension and try to loaded the default extension jvalidation).

If remove the default extension declared in org.apache.dubbo.validation.Validation, the org.apache.dubbo.validation.Validation#getValidator will attempt to load the extension with the corresponding name from the relevant configurations what dubbo.provider/consumer.validation named , but the official document of provider and consumer defines the parameter type of validation as Boolean, it's unexpect for dubbo-samples

As long as fill in any characters into dubbo.provider/consumer.validation configuration, verification will be enabled and loaded the default extension jvalidation

@aofall How about make org.apache.dubbo.validation.support.jvalidation.JValidation as an adapter? And it do not mean javax or jakarta. It just choose the real implement.

@aofall
Copy link
Contributor Author

aofall commented May 8, 2024

OK. I will try it

@aofall
Copy link
Contributor Author

aofall commented May 14, 2024

@aofall How about make org.apache.dubbo.validation.support.jvalidation.JValidation as an adapter? And it do not mean javax or jakarta. It just choose the real implement.

@AlbumenJ

I found the dubbo-filter/dubbo-filter-validation module has been moved to dubbo-plugin/dubbo-filter-validation in 3,3 branch.

I think this follow class can extracted into a separate module like dubbo-filter-validation-api

org.apache.dubbo.validation.Validation
org.apache.dubbo.validation.Validator
org.apache.dubbo.validation.MethodValidated
org.apache.dubbo.validation.filter.ValidationFilter
org.apache.dubbo.validation.support.AbstractValidation

And make the real implement like dubbo-spi-extension. Such as dubbo-filter-validation-javax and dubbo-filter-validation-jakarta. And create an adapter module like dubbo-filter-validation-adapter and set it as the default validtion extensions that supports automatic switch javax and jakarta.

Maybe there are some big changes, if the above plan is passed, will it be better to develop on the 3.3 branch?

@aofall
Copy link
Contributor Author

aofall commented May 14, 2024

@AlbumenJ
Copy link
Member

Maybe there are some big changes, if the above plan is passed, will it be better to develop on the 3.3 branch?

Yes

https://github.com/aofall/dubbo/tree/3.3-validation/dubbo-plugin/dubbo-filter-validation

Can we merge these classes into one module?

@aofall
Copy link
Contributor Author

aofall commented May 15, 2024

Move to #14194

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