Uploaded image for project: 'Java Driver'
  1. Java Driver
  2. JAVA-4495

Cannot search with a custom codec registry

    • Type: Icon: Bug Bug
    • Resolution: Works as Designed
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: 4.5.0
    • Component/s: Codecs

      Summary

      Using a custom codec registry when searching leads to a stack-overflow exception.

      Please provide the version of the driver. If applicable, please provide the MongoDB server version and topology (standalone, replica set, or sharded cluster).

      This happens only with driver 4.5.0. Driver 4.4.2 works fine.

      How to Reproduce

      `build.gradle`:

      plugins {
      	id 'application'
      }
      repositories {
      	mavenCentral()
      }
      dependencies {
      	implementation 'org.mongodb:mongodb-driver-reactivestreams:4.5.0'
      }
      application {
      	mainClass = 'company.Test'
      }
      

      `src/main/java/company/Test.java`:

      package company;
      
      import com.mongodb.client.model.Filters;
      import com.mongodb.client.result.InsertOneResult;
      import com.mongodb.reactivestreams.client.MongoClients;
      
      import org.bson.Document;
      import org.bson.codecs.Codec;
      import org.bson.codecs.configuration.CodecRegistry;
      import org.reactivestreams.Subscriber;
      import org.reactivestreams.Subscription;
      
      public class Test {
      	public static class CustomCodecRegistry implements CodecRegistry {
      		private final CodecRegistry codecRegistry;
      
      		public CustomCodecRegistry(CodecRegistry codecRegistry) {
      			this.codecRegistry = codecRegistry;
      		}
      
      		@Override
      		public <T> Codec<T> get(Class<T> clazz) {
      			return get(clazz, codecRegistry);
      		}
      
      		@Override
      		public <T> Codec<T> get(Class<T> clazz, CodecRegistry registry) {
      			return registry.get(clazz);
      		}
      	}
      
      	public static void main(String[] args) throws InterruptedException {
      		var client = MongoClients.create();
      
      		var collection = client.getDatabase("test").getCollection("collection");
      
      		collection.insertOne(new Document("bad", "42")).subscribe(new Subscriber<>() {
      			@Override
      			public void onSubscribe(Subscription s) {
      				s.request(1);
      			}
      
      			@Override
      			public void onNext(final InsertOneResult success) {
      			}
      
      			@Override
      			public void onError(Throwable t) {
      				t.printStackTrace();
      			}
      
      			@Override
      			public void onComplete() {
      				collection
      					.withCodecRegistry(new CustomCodecRegistry(collection.getCodecRegistry()))
      					.find(Filters.eq("bad", "42"))
      					.subscribe(new Subscriber<>() {
      						@Override
      						public void onSubscribe(Subscription s) {
      							s.request(1);
      						}
      
      						@Override
      						public void onNext(Document t) {
      							System.err.println(t);
      						}
      
      						@Override
      						public void onError(Throwable t) {
      							t.printStackTrace();
      						}
      
      						@Override
      						public void onComplete() {
      						}
      				});
      			}
      		});
      
      		Thread.sleep(5000);
      	}
      }
      

       Run `gradle run` on this and you'll see the error. Commenting-out the `.withCodecRegistry(new CustomCodecRegistry(collection.getCodecRegistry()))` line or changing the driver's version to `4.4.2` in `build.gradle` fixes the issue.

            Assignee:
            Unassigned Unassigned
            Reporter:
            alien Boris Petrov
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: