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

Dynamically define ApiGroups does not work #2584

Open
christian-draeger opened this issue Apr 26, 2024 · 0 comments
Open

Dynamically define ApiGroups does not work #2584

christian-draeger opened this issue Apr 26, 2024 · 0 comments

Comments

@christian-draeger
Copy link

christian-draeger commented Apr 26, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant