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

fix: EXPOSED-714 [exposed-spring-boot-starter] Fix NPE from DatabaseInitializer when non-object Table is defined #2382

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

PeraSite
Copy link

@PeraSite PeraSite commented Jan 29, 2025

Description

If spring.exposed.generate-ddl option is enabled, DatabaseInitializer discover exposed tables using following function.

fun discoverExposedTables(applicationContext: ApplicationContext, excludedPackages: List<String>): List<Table> {
    val provider = ClassPathScanningCandidateComponentProvider(false)
    provider.addIncludeFilter(AssignableTypeFilter(Table::class.java))
    excludedPackages.forEach { provider.addExcludeFilter(RegexPatternTypeFilter(Pattern.compile(it.replace(".", "\\.") + ".*"))) }
    val packages = AutoConfigurationPackages.get(applicationContext)
    val components = packages.map { provider.findCandidateComponents(it) }.flatten()
    return components.map { Class.forName(it.beanClassName).kotlin.objectInstance as Table }
    //                                                                             ^ Here
}

The discoverExposedTables function finds that

  1. Class that assignable from Table type
  2. Not in the excludedPackages package that cames from Spring property

And directly cast bean's objectInstance to Table.

The problem is that found bean can be a normal class not singleton object. For example, If I created open class like this

open class BaseTable() : Table()

and getting objectInstance of BaseTable class returns null.

Casting null to Table throws NPE like this.

java.lang.NullPointerException: null cannot be cast to non-null type org.jetbrains.exposed.sql.Table
	at org.jetbrains.exposed.spring.DatabaseInitializerKt.discoverExposedTables(DatabaseInitializer.kt:56) ~[exposed-spring-boot-starter-0.58.0.jar:na]

Type of Change

  • Bug fix

Affected databases:

  • All

Checklist

  • Unit tests are in place
  • The build is green (including the Detekt check)
  • All public methods affected by my PR has up to date API docs
  • Documentation for my change is up to date

Related Issues

EXPOSED-714 Throws NullPointerException when Table class isn't object

@bog-walk bog-walk self-assigned this Jan 29, 2025
@bog-walk bog-walk changed the title fix: EXPOSED-714 Fix NPE when non-object Table is defined fix: EXPOSED-714 [exposed-spring-boot-starter] Fix NPE from DatabaseInitializer when non-object Table is defined Jan 29, 2025
Copy link
Member

@bog-walk bog-walk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for reporting this issue & working on a fix so quickly @PeraSite !

@bog-walk bog-walk requested a review from e5l January 29, 2025 22:09
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

Successfully merging this pull request may close these issues.

2 participants