Skip to content

Commit

Permalink
Enhance ResourceTransform
Browse files Browse the repository at this point in the history
Make the generated `BookController` extended from `RestfullController<Book>`,

```groovy
import grails.rest.*

@resource(uri='/books')
class Book {

    String title

    static constraints = {
        title blank:false
    }
}
```

Closes gh-606
  • Loading branch information
rainboyan committed Sep 30, 2024
1 parent 36df0c7 commit c8a2c2c
Showing 1 changed file with 9 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -28,6 +28,7 @@ import org.codehaus.groovy.ast.ClassHelper
import org.codehaus.groovy.ast.ClassNode
import org.codehaus.groovy.ast.ConstructorNode
import org.codehaus.groovy.ast.FieldNode
import org.codehaus.groovy.ast.GenericsType
import org.codehaus.groovy.ast.MethodNode
import org.codehaus.groovy.ast.ModuleNode
import org.codehaus.groovy.ast.Parameter
Expand All @@ -48,6 +49,7 @@ import org.codehaus.groovy.ast.stmt.BlockStatement
import org.codehaus.groovy.ast.stmt.EmptyStatement
import org.codehaus.groovy.ast.stmt.ExpressionStatement
import org.codehaus.groovy.ast.stmt.IfStatement
import org.codehaus.groovy.ast.tools.GenericsUtils
import org.codehaus.groovy.control.CompilationUnit
import org.codehaus.groovy.control.CompilePhase
import org.codehaus.groovy.control.SourceUnit
Expand Down Expand Up @@ -78,7 +80,6 @@ import static org.grails.compiler.injection.GrailsASTUtils.VOID_CLASS_NODE
import static org.grails.compiler.injection.GrailsASTUtils.ZERO_PARAMETERS
import static org.grails.compiler.injection.GrailsASTUtils.applyDefaultMethodTarget
import static org.grails.compiler.injection.GrailsASTUtils.buildThisExpression
import static org.grails.compiler.injection.GrailsASTUtils.nonGeneric
import static org.grails.compiler.injection.GrailsASTUtils.processVariableScopes

/**
Expand Down Expand Up @@ -131,13 +132,17 @@ class ResourceTransform implements ASTTransformation, CompilationUnitAware {
Expression superClassAttribute = annotationNode.getMember(ATTR_SUPER_CLASS)
if (superClassAttribute instanceof ClassExpression) {
superClassNode = ((ClassExpression) superClassAttribute).getType()
if (superClassNode.isUsingGenerics()) {
superClassNode.setGenericsTypes(new GenericsType(parent))
}
}
else {
superClassNode = ClassHelper.make(RestfulController)
superClassNode = GenericsUtils.makeClassSafe(RestfulController)
superClassNode.setGenericsTypes(new GenericsType(parent))
}

ModuleNode ast = source.getAST()
ClassNode newControllerClassNode = new ClassNode(className, Modifier.PUBLIC, nonGeneric(superClassNode, parent))
ClassNode newControllerClassNode = new ClassNode(className, Modifier.PUBLIC, superClassNode)

AnnotationNode transactionalAnn = new AnnotationNode(TransactionalTransform.MY_TYPE)
transactionalAnn.addMember(ATTR_READY_ONLY, ConstantExpression.PRIM_TRUE)
Expand Down

0 comments on commit c8a2c2c

Please sign in to comment.