matrei commented on code in PR #16222: URL: https://github.com/apache/grails-core/pull/16222#discussion_r3863354284
########## grails-controllers/src/test/groovy/org/grails/compiler/web/ControllerActionTransformerSerializableParameterSpec.groovy: ########## @@ -0,0 +1,171 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.grails.compiler.web + +import grails.compiler.ast.ClassInjector +import grails.util.BuildSettings +import grails.util.GrailsWebMockUtil +import grails.web.servlet.context.GrailsWebApplicationContext +import org.codehaus.groovy.control.CompilationUnit +import org.springframework.web.context.WebApplicationContext +import org.springframework.web.context.request.RequestContextHolder + +import spock.lang.Specification + +import org.grails.compiler.injection.GrailsAwareClassLoader + +/** + * Verifies that an action parameter declared {@code Serializable} is bound from the request the same + * way a {@code String} one is. + * + * <p>{@code Serializable} is the type a domain class identifier is declared as when the action does not + * know the type itself - {@code Long} under Hibernate, {@code String} under MongoDB - and it is what + * {@code GormEntity.get(Serializable)} accepts. It used to be treated as a command object type, and + * being an interface it could not be one, so the parameter was reported and then bound to + * {@code null}.</p> + */ +class ControllerActionTransformerSerializableParameterSpec extends Specification { + + GrailsAwareClassLoader gcl + + void setup() { + System.properties[BuildSettings.CONVERT_CLOSURES_KEY] = 'true' + gcl = new GrailsAwareClassLoader() + ControllerActionTransformer transformer = new ControllerActionTransformer() { + @Override + boolean shouldInject(URL url) { + true + } + } + transformer.setCompilationUnit(new CompilationUnit()) + gcl.classInjectors = [transformer] as ClassInjector[] + def webRequest = GrailsWebMockUtil.bindMockWebRequest() + def appCtx = new GrailsWebApplicationContext() + webRequest.servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, appCtx) + } + + void cleanup() { + RequestContextHolder.resetRequestAttributes() + System.properties[BuildSettings.CONVERT_CLOSURES_KEY] = 'false' + } + + void 'a Serializable parameter is bound from the request parameter of the same name'() { + given: + def controller = serializableIdController() + + when: + controller.params.id = '42' + + then: + controller.show().value == '42' + } + + void 'a Serializable parameter is null where the request does not carry it'() { + given: + def controller = serializableIdController() + + expect: + controller.show().value == null + } + + void 'a Serializable parameter honours @RequestParameter'() { + given: + def cls = gcl.parseClass(''' + import grails.web.RequestParameter + + @grails.artefact.Artefact('Controller') + class RenamedIdController { + def show(@RequestParameter('personId') Serializable id) { + [value: id] + } + } + ''') + def controller = cls.newInstance() + + when: + controller.params['personId'] = 'abc' + + then: + controller.show().value == 'abc' + } + + void 'an action taking a Serializable parameter still generates the no-argument entry point'() { + given: + def cls = serializableIdController().getClass() + + expect: + cls.getMethod('show') + cls.getMethod('show', Serializable) + } + + void 'a command object that implements Serializable is still bound as a command object'() { + when: 'the command object is the declared type, not Serializable itself' + def cls = gcl.parseClass(''' + @grails.artefact.Artefact('Controller') + class WidgetController { + def save(WidgetCommand widget) { + [name: widget?.name] + } + + def $newCommand() { + new WidgetCommand(name: 'Sally') + } + } + + class WidgetCommand implements Serializable { + String name + } + ''') + def controller = cls.newInstance() Review Comment: `newInstance` is deprecated ########## grails-controllers/src/test/groovy/org/grails/compiler/web/ControllerActionTransformerSerializableParameterSpec.groovy: ########## @@ -0,0 +1,171 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.grails.compiler.web + +import grails.compiler.ast.ClassInjector +import grails.util.BuildSettings +import grails.util.GrailsWebMockUtil +import grails.web.servlet.context.GrailsWebApplicationContext +import org.codehaus.groovy.control.CompilationUnit +import org.springframework.web.context.WebApplicationContext +import org.springframework.web.context.request.RequestContextHolder + +import spock.lang.Specification + +import org.grails.compiler.injection.GrailsAwareClassLoader + +/** + * Verifies that an action parameter declared {@code Serializable} is bound from the request the same + * way a {@code String} one is. + * + * <p>{@code Serializable} is the type a domain class identifier is declared as when the action does not + * know the type itself - {@code Long} under Hibernate, {@code String} under MongoDB - and it is what + * {@code GormEntity.get(Serializable)} accepts. It used to be treated as a command object type, and + * being an interface it could not be one, so the parameter was reported and then bound to + * {@code null}.</p> + */ +class ControllerActionTransformerSerializableParameterSpec extends Specification { + + GrailsAwareClassLoader gcl + + void setup() { + System.properties[BuildSettings.CONVERT_CLOSURES_KEY] = 'true' + gcl = new GrailsAwareClassLoader() + ControllerActionTransformer transformer = new ControllerActionTransformer() { + @Override + boolean shouldInject(URL url) { + true + } + } + transformer.setCompilationUnit(new CompilationUnit()) + gcl.classInjectors = [transformer] as ClassInjector[] + def webRequest = GrailsWebMockUtil.bindMockWebRequest() + def appCtx = new GrailsWebApplicationContext() + webRequest.servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, appCtx) + } + + void cleanup() { + RequestContextHolder.resetRequestAttributes() + System.properties[BuildSettings.CONVERT_CLOSURES_KEY] = 'false' + } + + void 'a Serializable parameter is bound from the request parameter of the same name'() { + given: + def controller = serializableIdController() + + when: + controller.params.id = '42' + + then: + controller.show().value == '42' + } + + void 'a Serializable parameter is null where the request does not carry it'() { + given: + def controller = serializableIdController() + + expect: + controller.show().value == null + } + + void 'a Serializable parameter honours @RequestParameter'() { + given: + def cls = gcl.parseClass(''' + import grails.web.RequestParameter + + @grails.artefact.Artefact('Controller') + class RenamedIdController { + def show(@RequestParameter('personId') Serializable id) { + [value: id] + } + } + ''') + def controller = cls.newInstance() + + when: + controller.params['personId'] = 'abc' + + then: + controller.show().value == 'abc' + } + + void 'an action taking a Serializable parameter still generates the no-argument entry point'() { + given: + def cls = serializableIdController().getClass() + + expect: + cls.getMethod('show') + cls.getMethod('show', Serializable) + } + + void 'a command object that implements Serializable is still bound as a command object'() { + when: 'the command object is the declared type, not Serializable itself' + def cls = gcl.parseClass(''' + @grails.artefact.Artefact('Controller') + class WidgetController { + def save(WidgetCommand widget) { + [name: widget?.name] + } + + def $newCommand() { + new WidgetCommand(name: 'Sally') + } + } + + class WidgetCommand implements Serializable { + String name + } + ''') + def controller = cls.newInstance() + def command = controller.$newCommand() + + then: 'it went down the command object path, which is what makes it validateable' + command.validate() + command.name == 'Sally' + } + + void 'a Serializable parameter compiles under static compilation'() { + when: + def cls = gcl.parseClass(''' + @grails.artefact.Artefact('Controller') + @groovy.transform.CompileStatic + class StaticIdController { + Map show(Serializable id) { + [value: id] + } + } + ''') + def controller = cls.newInstance() + controller.params.id = '7' + + then: + controller.show().value == '7' + } + + private serializableIdController() { + gcl.parseClass(''' + @grails.artefact.Artefact('Controller') + class SerializableIdController { + def show(Serializable id) { + [value: id] + } + } + ''').newInstance() Review Comment: `newInstance` is deprecated ########## grails-core/src/main/groovy/org/grails/compiler/injection/EntityASTTransformation.java: ########## @@ -74,13 +74,17 @@ public void applyTransformation(SourceUnit sourceUnit, ClassNode classNode) { } GrailsASTUtils.markApplied(classNode, EntityASTTransformation.class); - GrailsDomainClassInjector domainInjector = new DefaultGrailsDomainClassInjector(); - domainInjector.performInjectionOnAnnotatedEntity(classNode); - ClassInjector[] classInjectors = GrailsAwareInjectionOperation.getClassInjectors(); final List<ClassInjector> domainInjectors = ArtefactTypeAstTransformation.findInjectors(DomainClassArtefactHandler.TYPE, classInjectors); + // The discovered injectors run first so that GORM's own transformation, where it is on the + // classpath, decides the type of the injected id: it knows which GORM implementation the + // entity is mapped with, and this one does not. DefaultGrailsDomainClassInjector then fills in + // whatever is still missing - which is everything, for an entity GORM did not process, and + // nothing for one it did, since each of its injections is guarded on the property already + // being there. The same order holds for entities under grails-app/domain, where + // GrailsAwareInjectionOperation sorts GormTransformer ahead of this injector by name. Review Comment: The correct ordering described here is purely incidental which feels kind of dangerous as something that could blow up in the future. ########## grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/core/GrailsGormIdTypeProvider.groovy: ########## @@ -0,0 +1,76 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.grails.gradle.plugin.core + +import groovy.transform.CompileStatic + +import org.gradle.api.InvalidUserDataException +import org.gradle.api.tasks.Input +import org.gradle.process.CommandLineArgumentProvider + +import grails.util.BuildSettings + +/** + * Publishes the nested {@code grails { gorm { defaultIdType } }} setting (see + * {@link BuildSettings#GORM_DEFAULT_ID_TYPE}) to the Groovy compiler's worker JVM as a system property, + * so that GORM's entity transformation can decide the type of the {@code id} it adds to a domain class + * that declares none. + * + * <p>The lazy {@link GrailsGormOptions} property is read in {@link #asArguments} (at compile time, not + * configuration time). The effective value is also exposed as an {@link Input} getter so that changing + * it invalidates the compile task: the identity type is compiled into the domain class, and a stale + * class file would otherwise keep the type the previous setting asked for.</p> + * + * @since 8.0 + */ +@CompileStatic +class GrailsGormIdTypeProvider implements CommandLineArgumentProvider { + + private final GrailsGormOptions gorm + + GrailsGormIdTypeProvider(GrailsGormOptions gorm) { + this.gorm = gorm + } + + /** + * The configured identity type, validated. Stating a value that GORM does not recognise fails the + * build here, where the setting is, rather than silently compiling every domain class with a + * {@code Long} id. + */ + @Input + String getDefaultIdType() { + String value = gorm.defaultIdType.getOrElse(BuildSettings.GORM_DEFAULT_ID_TYPE_LONG) + if (!(value in [BuildSettings.GORM_DEFAULT_ID_TYPE_LONG, BuildSettings.GORM_DEFAULT_ID_TYPE_NATIVE])) { Review Comment: These are compared case-sensitively, whereas GormEntityTransformation (lines 537-544) compares case-insensitively. Thus `defaultIdType = 'Native'` is accepted by the compiler when invoked directly, but rejected by the Gradle plugin. Either the compiler or provider should normalize/validate consistently. Should we make both paths enforce the same contract? ########## grails-controllers/src/test/groovy/org/grails/compiler/web/ControllerActionTransformerSerializableParameterSpec.groovy: ########## @@ -0,0 +1,171 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.grails.compiler.web + +import grails.compiler.ast.ClassInjector +import grails.util.BuildSettings +import grails.util.GrailsWebMockUtil +import grails.web.servlet.context.GrailsWebApplicationContext +import org.codehaus.groovy.control.CompilationUnit +import org.springframework.web.context.WebApplicationContext +import org.springframework.web.context.request.RequestContextHolder + +import spock.lang.Specification + +import org.grails.compiler.injection.GrailsAwareClassLoader + +/** + * Verifies that an action parameter declared {@code Serializable} is bound from the request the same + * way a {@code String} one is. + * + * <p>{@code Serializable} is the type a domain class identifier is declared as when the action does not + * know the type itself - {@code Long} under Hibernate, {@code String} under MongoDB - and it is what + * {@code GormEntity.get(Serializable)} accepts. It used to be treated as a command object type, and + * being an interface it could not be one, so the parameter was reported and then bound to + * {@code null}.</p> + */ +class ControllerActionTransformerSerializableParameterSpec extends Specification { + + GrailsAwareClassLoader gcl + + void setup() { + System.properties[BuildSettings.CONVERT_CLOSURES_KEY] = 'true' + gcl = new GrailsAwareClassLoader() + ControllerActionTransformer transformer = new ControllerActionTransformer() { + @Override + boolean shouldInject(URL url) { + true + } + } + transformer.setCompilationUnit(new CompilationUnit()) + gcl.classInjectors = [transformer] as ClassInjector[] + def webRequest = GrailsWebMockUtil.bindMockWebRequest() + def appCtx = new GrailsWebApplicationContext() + webRequest.servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, appCtx) + } + + void cleanup() { + RequestContextHolder.resetRequestAttributes() + System.properties[BuildSettings.CONVERT_CLOSURES_KEY] = 'false' + } + + void 'a Serializable parameter is bound from the request parameter of the same name'() { + given: + def controller = serializableIdController() + + when: + controller.params.id = '42' + + then: + controller.show().value == '42' + } + + void 'a Serializable parameter is null where the request does not carry it'() { + given: + def controller = serializableIdController() + + expect: + controller.show().value == null + } + + void 'a Serializable parameter honours @RequestParameter'() { + given: + def cls = gcl.parseClass(''' + import grails.web.RequestParameter + + @grails.artefact.Artefact('Controller') + class RenamedIdController { + def show(@RequestParameter('personId') Serializable id) { + [value: id] + } + } + ''') + def controller = cls.newInstance() Review Comment: `newInstance` is deprecated ########## grails-controllers/src/test/groovy/org/grails/compiler/web/ControllerActionTransformerSerializableParameterSpec.groovy: ########## @@ -0,0 +1,171 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.grails.compiler.web + +import grails.compiler.ast.ClassInjector +import grails.util.BuildSettings +import grails.util.GrailsWebMockUtil +import grails.web.servlet.context.GrailsWebApplicationContext +import org.codehaus.groovy.control.CompilationUnit +import org.springframework.web.context.WebApplicationContext +import org.springframework.web.context.request.RequestContextHolder + +import spock.lang.Specification + +import org.grails.compiler.injection.GrailsAwareClassLoader + +/** + * Verifies that an action parameter declared {@code Serializable} is bound from the request the same + * way a {@code String} one is. + * + * <p>{@code Serializable} is the type a domain class identifier is declared as when the action does not + * know the type itself - {@code Long} under Hibernate, {@code String} under MongoDB - and it is what + * {@code GormEntity.get(Serializable)} accepts. It used to be treated as a command object type, and + * being an interface it could not be one, so the parameter was reported and then bound to + * {@code null}.</p> + */ +class ControllerActionTransformerSerializableParameterSpec extends Specification { + + GrailsAwareClassLoader gcl + + void setup() { + System.properties[BuildSettings.CONVERT_CLOSURES_KEY] = 'true' + gcl = new GrailsAwareClassLoader() + ControllerActionTransformer transformer = new ControllerActionTransformer() { + @Override + boolean shouldInject(URL url) { + true + } + } + transformer.setCompilationUnit(new CompilationUnit()) + gcl.classInjectors = [transformer] as ClassInjector[] + def webRequest = GrailsWebMockUtil.bindMockWebRequest() + def appCtx = new GrailsWebApplicationContext() + webRequest.servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, appCtx) + } + + void cleanup() { + RequestContextHolder.resetRequestAttributes() + System.properties[BuildSettings.CONVERT_CLOSURES_KEY] = 'false' + } + + void 'a Serializable parameter is bound from the request parameter of the same name'() { + given: + def controller = serializableIdController() + + when: + controller.params.id = '42' + + then: + controller.show().value == '42' + } + + void 'a Serializable parameter is null where the request does not carry it'() { + given: + def controller = serializableIdController() + + expect: + controller.show().value == null + } + + void 'a Serializable parameter honours @RequestParameter'() { + given: + def cls = gcl.parseClass(''' + import grails.web.RequestParameter + + @grails.artefact.Artefact('Controller') + class RenamedIdController { + def show(@RequestParameter('personId') Serializable id) { + [value: id] + } + } + ''') + def controller = cls.newInstance() + + when: + controller.params['personId'] = 'abc' + + then: + controller.show().value == 'abc' + } + + void 'an action taking a Serializable parameter still generates the no-argument entry point'() { + given: + def cls = serializableIdController().getClass() + + expect: + cls.getMethod('show') + cls.getMethod('show', Serializable) + } + + void 'a command object that implements Serializable is still bound as a command object'() { + when: 'the command object is the declared type, not Serializable itself' + def cls = gcl.parseClass(''' + @grails.artefact.Artefact('Controller') + class WidgetController { + def save(WidgetCommand widget) { + [name: widget?.name] + } + + def $newCommand() { + new WidgetCommand(name: 'Sally') + } + } + + class WidgetCommand implements Serializable { + String name + } + ''') + def controller = cls.newInstance() + def command = controller.$newCommand() + + then: 'it went down the command object path, which is what makes it validateable' + command.validate() + command.name == 'Sally' + } + + void 'a Serializable parameter compiles under static compilation'() { + when: + def cls = gcl.parseClass(''' + @grails.artefact.Artefact('Controller') + @groovy.transform.CompileStatic + class StaticIdController { + Map show(Serializable id) { + [value: id] + } + } + ''') + def controller = cls.newInstance() Review Comment: `newInstance` is deprecated -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
