Skip to content

Wrong type inference in Kotlin while overriding RequestMappingHandlerMapping#getMappingForMethod #25657

@pavel-shatskikh

Description

@pavel-shatskikh

Hi!

Im trying override method org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerMapping#getMappingForMethod in Kotlin:

class CustomVersionRequestMappingHandlerMapping : RequestMappingHandlerMapping() {

    override fun getMappingForMethod(method: Method, handlerType: Class<*>): RequestMappingInfo {
        ...
    }

And kotlin compiler inference return type as non-nullable, but base abstract method in org.springframework.web.reactive.result.method.AbstractHandlerMethodMapping#getMappingForMethod declared as nullable:

@Nullable
protected abstract T getMappingForMethod(Method method, Class<?> handlerType);

So if I try change return type as nullable:

override fun getMappingForMethod(method: Method, handlerType: Class<*>): RequestMappingInfo? {

compiler fails with error:

Return type is 'RequestMappingInfo?', which is not a subtype of overridden protected/protected and package/ open fun getMappingForMethod(method: Method, handlerType: Class<*>): RequestMappingInfo defined in org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerMapping

It seems like Kotlin applies nullability rules from package-info file in this case:

@NonNullApi
@NonNullFields
package org.springframework.web.reactive.result.method.annotation;

and it is bug.

How can I fixed that?

Thank you.

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)status: backportedAn issue that has been backported to maintenance branchestype: bugA general bug

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions