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

StackOverflowError when using Kotlin companion object's fields #1122

Closed
komarovd95 opened this issue Mar 25, 2021 · 1 comment
Closed

StackOverflowError when using Kotlin companion object's fields #1122

komarovd95 opened this issue Mar 25, 2021 · 1 comment
Labels
bug Something isn't working

Comments

@komarovd95
Copy link

Describe the bug
Hi there. I faced a problem with generating OpenAPI specification using Kotlin.

I have a parameter in the RestController's method annotated with ParameterObject. The type of this parameter is a Kotlin class with companion object which have a non-const members (fields and/or properties). In my case it has read-only property of type java.time.Duration. When I'm accessing an OpenAPI specification via /v3/api-docs endpoint I'm getting a StackOverflowError at line MethodParameterPojoExtractor:113.

Annotating this property with a JvmField annotation nor JvmStatic didn't help.

I did some debug and realized that the MethodParameterPojoExtractor#extractFrom method is falling into recursion with the Duration.ZERO field which is static too.

To Reproduce
I'm using:
Spring 5.2.4.RELEASE
Spring Boot 2.2.5.RELEASE
SpringDoc OpenAPI 1.5.6
Kotlin 1.4.31

There is an example controller with which it can be reproduced:

@Tag(name = "Some API")
@RestController
@RequestMapping("/example")
class ExampleController {

    @GetMapping(produces = [MediaType.TEXT_PLAIN])
    @Operation(summary = "My awesome operation")
    fun test(@ParameterObject request: TestRequest): ResponseEntity<String> {
        return ResponseEntity.ok("test")
    }
}

class TestRequest(
    @field:Parameter(description = "Some parameter")
    val param: String
) {

    companion object {
        val SOME_CONSTANT_DURATION = Duration.ofHours(1L)
    }
}

So if you try to access http://localhost:8080/v3/api-docs you will receive HTTP status code 500 and StackOverflowError stack trace in the logs.

Expected behavior
So I think it should be allowed to use such classes as a parameter annotated with ParameterObject.

Maybe it's not needed to collect static fields in the MethodParameterPojoExtractor#allFieldsOf method.

@bnasslahsen
Copy link
Contributor

@komarovd95,

This is related to Duration which is not resolved as a Swagger Primitive Type.
Make sure you declare it for the @ParameterObject.

static {
	SpringDocUtils.getConfig().addSimpleTypesForParameterObject(Duration.class);
}

It will be handled out of the box for the next release.

@bnasslahsen bnasslahsen added the bug Something isn't working label Jan 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants