Code review comment: I copied this pattern from the Smallrye Fault Tolerance extension, and I just check almost all extensions directly used `Thread.currentThread().getContextClassLoader()` so it should be safe to use it.

Code before review:
private MongoClientSettings createMongoConfiguration(MongoClientConfig config) {
if (config == null) {
throw new RuntimeException("mongo config is missing for creating mongo client.");
}
checkCodec();
CodecRegistry defaultCodecRegistry = MongoClientSettings.getDefaultCodecRegistry();
MongoClientSettings.Builder settings = MongoClientSettings.builder();
ConnectionString connectionString;
Optional<String> maybeConnectionString = config.connectionString;
if (maybeConnectionString.isPresent()) {
connectionString = new ConnectionString(maybeConnectionString.get());
settings.applyConnectionString(connectionString);
}
List<CodecProvider> providers = new ArrayList<>();
if (!codecProviders.isEmpty()) {
providers.addAll(getCodecProviders(codecProviders));
}
PojoCodecProvider.Builder pojoCodecProviderBuilder = PojoCodecProvider.builder()
.automatic(true)
.conventions(Conventions.DEFAULT_CONVENTIONS);
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
if (classLoader == null) {
classLoader = AbstractMongoClientProducer.class.getClassLoader();
}
for (String bsonDiscriminator : bsonDiscriminators) {
try {
pojoCodecProviderBuilder
.register(ClassModel.builder(Class.forName(bsonDiscriminator, true, classLoader))
.enableDiscriminator(true).build());
} catch (ClassNotFoundException e) {
}
}
providers.add(pojoCodecProviderBuilder.build());
CodecRegistry registry = CodecRegistries.fromRegistries(defaultCodecRegistry,
CodecRegistries.fromProviders(providers));
settings.codecRegistry(registry);
config.applicationName.ifPresent(settings::applicationName);
if (config.credentials != null) {
MongoCredential credential = createMongoCredential(config);
if (credential != null) {
settings.credential(credential);
}
}
if (config.writeConcern != null) {
WriteConcernConfig wc = config.writeConcern;
WriteConcern concern = (wc.safe ? WriteConcern.ACKNOWLEDGED : WriteConcern.UNACKNOWLEDGED)
.withJournal(wc.journal);
if (wc.wTimeout.isPresent()) {
concern = concern.withWTimeout(wc.wTimeout.get().toMillis(), TimeUnit.MILLISECONDS);
}
Optional<String> maybeW = wc.w;
if (maybeW.isPresent()) {
concern = concern.withW(maybeW.get());
}
settings.writeConcern(concern);
settings.retryWrites(wc.retryWrites);
}
if (config.tls) {
settings.applyToSslSettings(new SslSettingsBuilder(config, disableSslSupport));
}
settings.applyToClusterSettings(new ClusterSettingBuilder(config));
settings.applyToConnectionPoolSettings(new ConnectionPoolSettingsBuilder(config));
settings.applyToServerSettings(new ServerSettingsBuilder(config));
settings.applyToSocketSettings(new SocketSettingsBuilder(config));
if (config.readPreference.isPresent()) {
settings.readPreference(ReadPreference.valueOf(config.readPreference.get()));
}
return settings.build();
}

Line under review: if (classLoader == null) {

Code after addressing the review:
private MongoClientSettings createMongoConfiguration(MongoClientConfig config) {
if (config == null) {
throw new RuntimeException("mongo config is missing for creating mongo client.");
}
checkCodec();
CodecRegistry defaultCodecRegistry = MongoClientSettings.getDefaultCodecRegistry();
MongoClientSettings.Builder settings = MongoClientSettings.builder();
ConnectionString connectionString;
Optional<String> maybeConnectionString = config.connectionString;
if (maybeConnectionString.isPresent()) {
connectionString = new ConnectionString(maybeConnectionString.get());
settings.applyConnectionString(connectionString);
}
List<CodecProvider> providers = new ArrayList<>();
if (!codecProviders.isEmpty()) {
providers.addAll(getCodecProviders(codecProviders));
}
PojoCodecProvider.Builder pojoCodecProviderBuilder = PojoCodecProvider.builder()
.automatic(true)
.conventions(Conventions.DEFAULT_CONVENTIONS);
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
for (String bsonDiscriminator : bsonDiscriminators) {
try {
pojoCodecProviderBuilder
.register(ClassModel.builder(Class.forName(bsonDiscriminator, true, classLoader))
.enableDiscriminator(true).build());
} catch (ClassNotFoundException e) {
}
}
providers.add(pojoCodecProviderBuilder.build());
CodecRegistry registry = CodecRegistries.fromRegistries(defaultCodecRegistry,
CodecRegistries.fromProviders(providers));
settings.codecRegistry(registry);
config.applicationName.ifPresent(settings::applicationName);
if (config.credentials != null) {
MongoCredential credential = createMongoCredential(config);
if (credential != null) {
settings.credential(credential);
}
}
if (config.writeConcern != null) {
WriteConcernConfig wc = config.writeConcern;
WriteConcern concern = (wc.safe ? WriteConcern.ACKNOWLEDGED : WriteConcern.UNACKNOWLEDGED)
.withJournal(wc.journal);
if (wc.wTimeout.isPresent()) {
concern = concern.withWTimeout(wc.wTimeout.get().toMillis(), TimeUnit.MILLISECONDS);
}
Optional<String> maybeW = wc.w;
if (maybeW.isPresent()) {
concern = concern.withW(maybeW.get());
}
settings.writeConcern(concern);
settings.retryWrites(wc.retryWrites);
}
if (config.tls) {
settings.applyToSslSettings(new SslSettingsBuilder(config, disableSslSupport));
}
settings.applyToClusterSettings(new ClusterSettingBuilder(config));
settings.applyToConnectionPoolSettings(new ConnectionPoolSettingsBuilder(config));
settings.applyToServerSettings(new ServerSettingsBuilder(config));
settings.applyToSocketSettings(new SocketSettingsBuilder(config));
if (config.readPreference.isPresent()) {
settings.readPreference(ReadPreference.valueOf(config.readPreference.get()));
}
return settings.build();
}