0

Not sure why, but I can't seem to return a data class that implements the expected interface.

interface

interface AuthServiceResponse {
    val statusCode: Int
    val data: AuthServiceResponseData
    val errors: List<AuthServiceResponseError>?
}

implementation

data class AuthServiceBasicResponse(override var statusCode: Int,
                                    override var data: AuthServiceResponseData,
                                    override var errors: List<AuthServiceResponseError>) : AuthServiceResponse

expecting the AuthServiceResponse interface

    @PostMapping
    fun loginUser(@RequestParam username: String,
                  @RequestParam password: String): Mono<AuthServiceResponse> {
        return authenticationService.loginUser(username, password)
    }

method that returns the AuthServiceBasicResponse class that implements the AuthServiceResponse interface

fun loginUser(username: String,
              password: String): Mono<AuthServiceBasicResponse> {
...
}

example screenshot

Clement
  • 4,491
  • 4
  • 39
  • 69
  • 2
    Related: https://stackoverflow.com/questions/2745265/is-listdog-a-subclass-of-listanimal-why-are-java-generics-not-implicitly-po, https://kotlinlang.org/docs/reference/generics.html#use-site-variance-type-projections – JB Nizet Apr 21 '19 at 12:10
  • omg yes. Totally forgot about variance. Thank you! – Clement Apr 21 '19 at 12:14

1 Answers1

0

Just for the sake of completeness, will post my solution here. Thanks a lot to JB Nizet in the comments above for pointing me in the right direction.

What I didn't account for was variance when using generics.

By using the out keyword, subclasses of AuthServiceResponse can be used in the return value.

@PostMapping
fun createUser(@RequestParam username: String,
               @RequestParam password: String): Mono<out AuthServiceResponse> {
    return authenticationService.createUser(username, password)
}

The Kotlin team has some good explanation here.

The post shared by JB Nizet here

Clement
  • 4,491
  • 4
  • 39
  • 69