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 initialization of ValidatedService.validatorFactory #1784

Merged
merged 3 commits into from
Feb 2, 2024

Conversation

guillermocalvo
Copy link
Contributor

Fixes grails/grails-core#13349

Initialization of ValidatedService.validatorFactory was accidentally bypassed in a recent PR:

ValidatorFactory getValidatorFactory() {
    if(validatorFactory == null) {
        // validator factory initialized here
    }
    return validatorFactory
}

As a result of that, data services cannot validate arguments anymore, throwing NPE.

@CLAassistant
Copy link

CLAassistant commented Jan 31, 2024

CLA assistant check
All committers have signed the CLA.

@matrei
Copy link
Contributor

matrei commented Jan 31, 2024

@guillermocalvo Good catch! The field:

private ValidatorFactor validatorFactory

should probably be renamed to something else, like: factoryInstance or validatorFactoryInstance to prevent this in the future.

@matrei
Copy link
Contributor

matrei commented Jan 31, 2024

So this code actually compiles without @CompileDynamic:

    private ValidatorFactory validatorFactoryInstance

    // @CompileDynamic <-- Not needed
    private Map<Method, ExecutableValidator> executableValidatorMap = new LinkedHashMap<Method, ExecutableValidator>().withDefault {
        validatorFactory.getValidator().forExecutables()
    }

    /**
     * @return The validator factory for this service
     */
    ValidatorFactory getValidatorFactory() {
        if(validatorFactoryInstance == null) {

            Configuration configuration
            if(datastore != null) {
                configuration = JavaxValidatorRegistry.buildConfigurationFor(
                        datastore.mappingContext,
                        datastore.mappingContext.validatorRegistry.messageSource
                )
            }
            else {
                configuration = Validation.byDefaultProvider()
                                            .configure()
                configuration = configuration.ignoreXmlConfiguration()
            }
            if(parameterNameProvider != null) {
                configuration = configuration.parameterNameProvider(parameterNameProvider)
            }
            validatorFactoryInstance = configuration.buildValidatorFactory()
        }
        return validatorFactoryInstance
    }

@puneetbehl puneetbehl merged commit bc13343 into 8.0.x Feb 2, 2024
7 checks passed
@puneetbehl puneetbehl deleted the grails-core-issues-13349 branch February 2, 2024 04:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Grails 6.1.1 seems to break data services (NullPointerException)
4 participants