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

Kotlin - resource decorator issue with controllers returning a Kotlin data class #532

Open
dcoraboeuf opened this issue Aug 7, 2017 · 1 comment
Labels

Comments

@dcoraboeuf
Copy link
Contributor

Given a data class like:

data class MyType(val key: String, val name: String)

a controller returning this type:

@RestController
class MyController {
   @GetMapping("/my-type/{key}")
   fun getMyType(@PathVariable key: String): MyType {
      ...
   }
}

a resource decorator:

@Component
class MyTypeResourceDecorator : AbstractResourceDecorator<MyType>(MyType::class.java) {
    override fun links(myType: MyType, resourceContext: ResourceContext): List<Link> {
        return resourceContext.links()
                .self(on(MyTypeController::class.java).getMyType(myType.key))
    }
}

When tested, the call will fail because of:

Cannot subclass final class MyType

A current workaround is to adapt the controller to return a Resource instead:

@RestController
class MyController {
   @GetMapping("/my-type/{key}")
   fun getMyType(@PathVariable key: String): Resource<MyType> {
      ...
   }
}
@dcoraboeuf
Copy link
Contributor Author

Other (and simpler) workaround, using an open instead of a data class:

open class MyType(val key: String, val name: String)

Of course, this is not very satisfying, but some fix might be available in a future Spring Kotlin extension - see https://github.com/sdeleuze/spring-kotlin

dcoraboeuf added a commit that referenced this issue Nov 15, 2018
dcoraboeuf added a commit that referenced this issue Dec 9, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant