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

StackOverflowError when saving entity with recursive type #3234

Open
itdv opened this issue Sep 10, 2024 · 0 comments
Open

StackOverflowError when saving entity with recursive type #3234

itdv opened this issue Sep 10, 2024 · 0 comments
Labels

Comments

@itdv
Copy link

itdv commented Sep 10, 2024

Describe the bug
When attempting to save a recursive type entity or entity that includes a recursive type (i.e., a field of its own type) in Morphia 2.4.14, a StackOverflowError occurs. The issue can be reproduced with the following code.

import com.mongodb.client.MongoClients;
import dev.morphia.Datastore;
import dev.morphia.Morphia;
import dev.morphia.annotations.Entity;
import dev.morphia.annotations.Id;
import org.bson.types.ObjectId;

public class Main {

    @Entity
    public static class A {
        @Id
        private ObjectId id;
        B b;

        public A(ObjectId id, B b) {
            this.id = id;
            this.b = b;
        }

        public A() {
        }
    }

    @Entity
    public static class B {
        B innerB;
        public B(){
        }

        public B(B innerB) {
            this.innerB = innerB;
        }
    }


    public static void main(String[] args) {
        try (var mongoClient = MongoClients.create()) {
            // Create Datastore using Morphia
            Datastore ds = Morphia.createDatastore(mongoClient, "morphia-2-4-4-test");
            ds.getMapper().map(Main.A.class);  // map the entity class
            ds.getMapper().map(B.class);  // map the entity class

            B b = new B(null);
            A a = new A(new ObjectId(), b);

            ds.save(a);

        }
    }
}

(The same if A itself is a recursive type.)
The above code is running correctly with morphia 2.3.4, but in later versions, from 2.3.5 up to 2.4.14 StackOverflowError is thrown:

Exception in thread "main" java.lang.StackOverflowError
	at java.base/java.util.concurrent.ConcurrentHashMap.containsKey(ConcurrentHashMap.java:964)
	at dev.morphia.mapping.Mapper.isMapped(Mapper.java:416)
	at dev.morphia.mapping.codec.MorphiaCodecProvider.get(MorphiaCodecProvider.java:74)
	at org.bson.codecs.configuration.CodecProvider.get(CodecProvider.java:70)
	at org.bson.internal.ProvidersCodecRegistry.getFromCodecProvider(ProvidersCodecRegistry.java:95)
	at org.bson.internal.ProvidersCodecRegistry.lambda$get$0(ProvidersCodecRegistry.java:82)
	at java.base/java.util.Optional.orElseGet(Optional.java:364)
	at org.bson.internal.ProvidersCodecRegistry.get(ProvidersCodecRegistry.java:80)
	at org.bson.internal.ProvidersCodecRegistry.get(ProvidersCodecRegistry.java:50)
	at dev.morphia.mapping.codec.pojo.PropertyModel.configureCodec(PropertyModel.java:389)
	at dev.morphia.mapping.codec.pojo.PropertyModel.specializeCodec(PropertyModel.java:289)
	at dev.morphia.mapping.codec.pojo.MorphiaCodec.specializePropertyCodecs(MorphiaCodec.java:199)
	at dev.morphia.mapping.codec.pojo.MorphiaCodec.<init>(MorphiaCodec.java:71)
	at dev.morphia.mapping.codec.MorphiaCodecProvider.get(MorphiaCodecProvider.java:76)
	at org.bson.codecs.configuration.CodecProvider.get(CodecProvider.java:70)
	at org.bson.internal.ProvidersCodecRegistry.getFromCodecProvider(ProvidersCodecRegistry.java:95)
	at org.bson.internal.ProvidersCodecRegistry.lambda$get$0(ProvidersCodecRegistry.java:82)
	at java.base/java.util.Optional.orElseGet(Optional.java:364)
	at org.bson.internal.ProvidersCodecRegistry.get(ProvidersCodecRegistry.java:80)
	at org.bson.internal.ProvidersCodecRegistry.get(ProvidersCodecRegistry.java:50)
	at dev.morphia.mapping.codec.pojo.PropertyModel.configureCodec(PropertyModel.java:389)
	at dev.morphia.mapping.codec.pojo.PropertyModel.specializeCodec(PropertyModel.java:289)
	at dev.morphia.mapping.codec.pojo.MorphiaCodec.specializePropertyCodecs(MorphiaCodec.java:199)
	at dev.morphia.mapping.codec.pojo.MorphiaCodec.<init>(MorphiaCodec.java:71)
	at dev.morphia.mapping.codec.MorphiaCodecProvider.get(MorphiaCodecProvider.java:76)
	at org.bson.codecs.configuration.CodecProvider.get(CodecProvider.java:70)
	at org.bson.internal.ProvidersCodecRegistry.getFromCodecProvider(ProvidersCodecRegistry.java:95)
	at org.bson.internal.ProvidersCodecRegistry.lambda$get$0(ProvidersCodecRegistry.java:82)
	at java.base/java.util.Optional.orElseGet(Optional.java:364)
	at org.bson.internal.ProvidersCodecRegistry.get(ProvidersCodecRegistry.java:80)
	at org.bson.internal.ProvidersCodecRegistry.get(ProvidersCodecRegistry.java:50)
	at dev.morphia.mapping.codec.pojo.PropertyModel.configureCodec(PropertyModel.java:389)
	at dev.morphia.mapping.codec.pojo.PropertyModel.specializeCodec(PropertyModel.java:289)
	at dev.morphia.mapping.codec.pojo.MorphiaCodec.specializePropertyCodecs(MorphiaCodec.java:199)
	at dev.morphia.mapping.codec.pojo.MorphiaCodec.<init>(MorphiaCodec.java:71)
	at dev.morphia.mapping.codec.MorphiaCodecProvider.get(MorphiaCodecProvider.java:76)
	at org.bson.codecs.configuration.CodecProvider.get(CodecProvider.java:70)
	at org.bson.internal.ProvidersCodecRegistry.getFromCodecProvider(ProvidersCodecRegistry.java:95)
	at org.bson.internal.ProvidersCodecRegistry.lambda$get$0(ProvidersCodecRegistry.java:82)
	at java.base/java.util.Optional.orElseGet(Optional.java:364)
@itdv itdv added the bug label Sep 10, 2024
@itdv itdv changed the title StackOverflowError when saving entity with recursive type in Morphia 2.4.4 StackOverflowError when saving entity with recursive type Sep 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant