Skip to content

Dynamically define ApiGroups does not work #2584

Closed
@christian-draeger

Description

@christian-draeger

Describe the bug

I am writing a custom spring boot starter that i want to reuse among several projects.
for this purpose i want to dynamically configure grouped open apis.
when i add a bean that returns GroupedOpenApi it works, but when i add bean that returns List<GroupedOpenApi> it does nothing. is there an option to configure a list of GroupedOpenApi dynamically?

To Reproduce
Steps to reproduce the behavior:

  • spring-boot version: 3.2.4
  • springdoc-openapi modules used:
    • org.springdoc:springdoc-openapi-starter-common:2.3.0
    • org.springdoc:springdoc-openapi-starter-webmvc-ui:2.3.0

the properties i pass:

    api-title: "My Custom API"
    api-version: "2.0.0"
    api-groups:
      - name: Greeting-API-V1
        path: "/api/greeting/v1/hello/**"
        packages: "com.tsi.dv.backend.controller.greeting"
      - name: Some Other Api
        path: "/api/some-other/**"
      - name: Even Another Api
        packages: "com.tsi.dv.example.foobar"

my beans:

const val SWAGGER_SECURITY_SCHEME_NAME = "Bearer Authentication"
private const val DV_APIDOC_PROPERTY_PREFIX = "dv.swagger"

@ConditionalOnWebApplication
@ConditionalOnProperty(prefix = DV_APIDOC_PROPERTY_PREFIX, name = ["enabled"], havingValue = "true")
@AutoConfiguration
@EnableConfigurationProperties(DvSwaggerConfigurationProperties::class)
@SecurityScheme(
    name = SWAGGER_SECURITY_SCHEME_NAME,
    type = SecuritySchemeType.HTTP,
    bearerFormat = "JWT",
    scheme = "bearer"
)
open class DvSwaggerConfig(
    private val properties: DvSwaggerConfigurationProperties
) {

    init {
        logger.info { "----> DvSwaggerConfig loaded" }
    }

    @Bean("dvOpenAPI")
    open fun customOpenAPI(): OpenAPI {
        return OpenAPI()
            .components(Components())
            .info(Info().title(properties.apiTitle).version(properties.apiVersion))
    }

     // NOT WORKING
    @Bean("dvSwaggerApiGroups")
    open fun apiGroups(): List<GroupedOpenApi> {
        return properties.apiGroups.map { group ->
            GroupedOpenApi.builder().group(group.name).let {
                if (group.path != null) {
                    it.pathsToMatch(group.path)
                }
                if (group.packages != null) {
                    it.packagesToScan(group.packages)
                } else {
                    it.packagesToScan("com.tsi.dv")
                }.build()
            }
        }
    }

    // WORKS
    @Bean("dvSwaggerApiGroup")
    open fun apiGroup(): GroupedOpenApi {
        val group = properties.apiGroups.first()
        return GroupedOpenApi.builder()
            .group(group.name)
            .pathsToMatch(group.path)
            .packagesToScan(group.packages)
            .build()
    }
}

@ConfigurationProperties(prefix = DV_APIDOC_PROPERTY_PREFIX)
data class DvSwaggerConfigurationProperties(
    var enabled: Boolean = true,
    var apiTitle: String = "API Doc",
    var apiVersion: String = "v1",
    var apiGroups: List<ApiGroup> = listOf()
)

data class ApiGroup(
    var name: String,
    var path: String? = null,
    var packages: String? = null
)

Expected behavior

  • i would expect that "dvSwaggerApiGroups" bean would create groups based on the List properties.

Screenshots
// introducing group by "dvSwaggerApiGroup" works, "dvSwaggerApiGroups" not.
image

this also happens if i remove "dvSwaggerApiGroup".
image

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions