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 @GrailsCompileStatic on domain objects with inner classes #13013

Merged

Conversation

darxriggs
Copy link
Contributor

Fixes #12461.

Make sure that for every class a new scope is added to and removed from the stack.

Previously a new scope was added for nearly every class but not removed.
This had the effect that the wrong (empty) scope (from the inner class)
was used for the entity class. This was then missing any of its `mapping`,
`constraints` and `namedQueries` closures.
@darxriggs
Copy link
Contributor Author

darxriggs commented May 30, 2023

I would like to have some suggestions on the tests. They are covering the changes and would fail without them. But I think there should be better ways. Maybe they could even be integrated into the existing GrailsCompileStaticCompilationErrorsSpec and GrailsTypeCheckedCompilationErrorsSpec files.

@Issue('https://github.com/grails/grails-core/issues/12461')
void 'a domain class marked with @GrailsCompileStatic containing an inner class and a "namedQueries" block'() {
setup:
SomeClass.getNamedQuery('test')
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is required in contrast to the other tests as the namedQueries closure seems only to be evaluated when the first named query is accessed and not when the class is compiled.

void 'a domain class marked with @GrailsCompileStatic containing an inner class and a "constraints" block'() {
expect: 'the configuration from the "constraints" closure is available'
SomeClass.constraints instanceof Closure
SomeClass.constraintsClosureCalled
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Another way could be to actually define a constraint, create an entity, call entity.validate() and check for e.g. SomeClass.getConstrainedProperties().containsKey('some') or even entity.errors['some'] or something else. But I think this would involve more components that could break in other ways.


expect: 'the configuration from the "namedQueries" closure is available'
SomeClass.namedQueries instanceof Closure
SomeClass.namedQueriesClosureCalled
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Another way could be to actually define a named query, call SomeClass.getNamedQuery('some') and verify that the result is not null. But I think this would involve more components that could break in other ways.

void 'a domain class marked with @GrailsCompileStatic containing an inner class and a "mapping" block'() {
expect: 'the configuration from the "mapping" closure is available'
SomeClass.mapping instanceof Closure
SomeClass.mappingClosureCalled
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have not found a working alternative in a DomainUnitTest to check any side effect on the entity instance or class based on something defined in a mapping block . The idea could be to set e.g. table, version or comment.

@darxriggs
Copy link
Contributor Author

@puneetbehl Could you have a look and suggest improvements for the tests or mention someone who could?

@puneetbehl puneetbehl merged commit a16e3fe into grails:6.0.x Jul 24, 2023
matrei pushed a commit to matrei/grails-core that referenced this pull request Jul 26, 2023
…#13013)

Make sure that for every class a new scope is added to and removed from the stack.

Previously a new scope was added for nearly every class but not removed.
This had the effect that the wrong (empty) scope (from the inner class)
was used for the entity class. This was then missing any of its `mapping`,
`constraints` and `namedQueries` closures.
puneetbehl added a commit that referenced this pull request Jul 27, 2023
Backport: Fix @GrailsCompileStatic on domain objects with inner classes (#13013)
@darxriggs darxriggs deleted the static-compilation-entity-with-inner-class branch July 30, 2023 11:30
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.

Validation is not applied when using @GrailsCompileStatic on domains with inner Enum
2 participants