Skip to content

Dynamic groups from application.yml #344

Closed
@YunaBraska

Description

@YunaBraska

I would love to have a dynamically loaded group config. I failed myself using the BeanFactoryPostProcessor. For some reason it could not find the path to the swagger-json file.

This is an example how it could look like:

group-config:
    -
      name: "external"
      packages-to-scan: "com.example.app.controler.api"
      paths: "/api/**"
    -
      name: "internal"
      packages-to-scan: "com.example.app.controler.web"
      paths: "/capi/**"

My approach:

@Bean
    public static BeanFactoryPostProcessor beanFactoryPostProcessor(
            final Environment environment) {
        return beanFactory -> {
            final BindResult<SpringDocGroupConfig> result = Binder.get(environment)
                    .bind("springdoc", SpringDocGroupConfig.class);
            final SpringDocGroupConfig groupConfig = result.get();
            // Use the properties to post-process the bean factory as needed
            if (groupConfig != null && groupConfig.getGroupConfig() != null) {
                groupConfig.getGroupConfig().forEach(config -> {
                    final GroupedOpenApi.Builder builder = GroupedOpenApi.builder();
                    if (hasText(config.getName())) {
                        builder.setGroup(config.getName());
                    }
                    if (config.getPaths() != null) {
                        builder.pathsToMatch(config.getPaths().toArray(new String[0]));
                    }
                    if (config.getPackagesToScan() != null) {
                        builder.packagesToScan(config.getPackagesToScan().toArray(new String[0]));
                    }
                    final GroupedOpenApi groupedOpenApi = builder.build();
                    beanFactory.registerSingleton(groupedOpenApi.getGroup(), groupedOpenApi);
                });
            }
        };
    }

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