javalin-openapi does not work when controller class has more than one member
See original GitHub issueActual behavior (the bug) I have a simple controller I’d like to generate documentation for:
App Setup:
fun main() {
Javalin.create {
it.registerPlugin(getConfiguredOpenApiPlugin())
it.defaultContentType = "application/json"
}.routes {
post("user", MyController::createUser)
}.start(7001)
}
fun getConfiguredOpenApiPlugin() = OpenApiPlugin(
OpenApiOptions(
Info().apply {
version("1.0")
description("User API")
}
).apply {
path("/swagger-docs")
swagger(SwaggerOptions("/swagger-ui"))
}
)
Controller:
data class User(
val id: Int,
val name: String,
val email: String
)
object MyController {
@OpenApi(
operationId = "myOperation",
description = "my description",
requestBody = OpenApiRequestBody(content = [OpenApiContent(from = User::class)], required = true),
responses = [OpenApiResponse(status = "200", content = [OpenApiContent(from = User::class)])]
)
fun createUser(ctx: Context) {
ctx.result("done")
}
}
This works as expected, and swagger-docs generates a schema for the User which is tied to the endpoint definition. However, if I add another function or even just a val to MyController
, the annotation isn’t processed correctly and there is no schema, api definition, etc. Eg:
object MyController {
val myProperty = "property"
... rest the same
}
Expected behavior I expect the generation to still work if my controller has other members.
To Reproduce Copy code above.
Additional context Consuming these dependencies:
implementation("io.javalin:javalin:4.6.4")
implementation("io.javalin:javalin-openapi:4.6.4")
Issue Analytics
- State:
- Created a year ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
OpenAPI & spring-doc not finding all mappings in a controller ...
The issue you are facing is caused by the fact that you have used level-1 references for paths-to-match and that's causing Springdoc to ......
Read more >OpenAPI bugs and feature requests · Issue #1296 - GitHub
Issues related to the OpenAPI plugin make up a disproportionate amount of Javalin issues, so I'm going to close them and gather them...
Read more >Documenting endpoints with OpenAPI 3 - Javalin
This tutorial will teach you how to use the Javalin OpenAPI plugin to create an OpenAPI spec (previously known as a “Swagger spec”)....
Read more >Bringing together OpenAPI 3 and Spring Boot by Badr ...
Spring I/O Bridge (online conference) - 15 May 2020GitHub repo: https://github.com/springdoc/springdoc- openapi -demos.
Read more >Get Started with Swashbuckle and ASP.NET Core - codeburst
An application can have more than one OpenAPI document, so we use different document names to differentiate them. On the other hand, ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Built-in OpenApi module has dozen of issues:
The one you’re reporting is probably another effect of those two:
There’s no fix tbh for all those issues, that’s why I’ve ended up with custom openapi implementation that will officially replace current module in Javalin 5.x. It’s also built on top of the annotation processor, not reflections:
You can also use earlier version of this plugin for Javalin 4.x:
Plugin works properly and it’s relatively easy to extend, example of production ready code:
All of that is generated by the new integration, so I think you can give it a try and report some missing features in its repository. Ofc plugin also works with Java:
Keep in mind that API in 5.x has been slightly improved, so setup might be a little bit different. Take a look on 4.x branch for valid description.
Reposilite 3.x I’ve linked you above with examples is already based on top of Javalin 5.x and it works well in production for quite a lof of people. The only disadvantage is that your project may randomly stop compiling in case of some breaking cosmetic changes, but overall I think it’s worth to give it a try, you’ll get access to latest features and you don’t really have to care about migration from 4.x in the future. In case of any issues you can always address it here in a new thread or ask on Javalin’s Discord server.