"Expecting a top level declaration" after Kotlin upgrade


#1

Hello there! :slight_smile:

I wrote some code for Kotlin 1.1.51 and it worked.

Basically the code implements a sample Kotlin RESTful service with Spring and Swagger2 in it. The working code can be seen at https://git.bitwell.fi/projects/SAM/repos/kotlin-spring-restful-webservice/browse

I cloned it out for maintenance, updated all deps to latest, including Kotlin to 1.2.30, and that does not build any more.

The failing file is as follows:

package fi.bitwell.poundam.service

import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import springfox.documentation.builders.PathSelectors
import springfox.documentation.builders.RequestHandlerSelectors
import springfox.documentation.spi.DocumentationType
import springfox.documentation.spring.web.plugins.Docket
import springfox.documentation.swagger2.annotations.EnableSwagger2


@Configuration
@EnableSwagger2
open class SwaggerConfig {
	@Bean
	open fun api(): Docket = Docket(DocumentationType.SWAGGER_2)
							.select()
							.apis(RequestHandlerSelectors.any())
							.paths(PathSelectors.any())
							.build()
	}
}

Kotlin compiler says that "expecting a top level declaration. But if I move that fun to top level the @Bean annotation or some other annotation is told to encompatible with a top level fun.

I’m clueless now what is wrong with that. Any ideas?


#2

I don’t see anything why this could shouldn’t work (especially if it did work on 1.1.51). First try to make sure you do a clean compile. After that, what line is the compiler complaining about?


#3

There is a closing curly-brace in there that should not be there :slight_smile:


#4

Indeed, m_thread! Thanks for the good catch!

Kotlin Compiler is funny at times, as error messages are really frustrating sometimes, while it indeed pointed at the last curly.

I’m watching the code now at a different compiler which has the old version Kotlin, and no error was visible but I could catch that immediately with your help!

Trying to upgrade Kotlin again with that computer and see how it goes.