MorphiaOrg / MorphiaOrg/morphia

Unable to read polymorphic types with Morphia with or without the default discriminator

Open
#3,411 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

question Waiting for Response
Dominant language
Java
Stars
1.7k
Forks
449
Avg merge
7m
Merged PRs (30d)
10

Description

Server Version: 8.0.4
Driver Version: 4.11.5
Morphia Version: 2.4.15

So I have a simple structure:

Interceptor.java

@Entity(discriminatorKey = "type")
public abstract class Interceptor {

    @Id
    protected String id;
    protected String type;

    public Interceptor(String type) {
        this.type = type;
    }
    
    // getters and setters omitted..
}

It's subclasses:

ScriptInterceptor.java

@Entity(discriminator = "script", discriminatorKey = "type")
public class ScriptInterceptor extends Interceptor {

    private String language;
    private String source;

    public ScriptInterceptor() {
        super("script");
    }
    
    // getters and setters omitted...
}

XsltInterceptor.java

@Entity(discriminator = "xslt", discriminatorKey = "type")
public class XsltInterceptor extends Interceptor {

    private String source;

    public XsltInterceptor() {
        super("xslt");
    }
    
    // getters and setters omitted...
}

The document is successfully inserted, but when reading it seems that Morphia is unable to deserialize it. The document looks like this:

Image

The error I'm getting is:

Caused by: org.bson.codecs.configuration.CodecConfigurationException: Failed to decode 'Interceptor'. Decoding errored with: my.package.ScriptInterceptor
............
Caused by: dev.morphia.mapping.MappingException: my.package.ScriptInterceptor
	at dev.morphia.mapping.DiscriminatorLookup.lookup(DiscriminatorLookup.java:69)
	at dev.morphia.mapping.codec.pojo.EntityDecoder.getCodecFromDocument(EntityDecoder.java:105)
	... 84 more
Caused by: java.lang.ClassNotFoundException: my.package.ScriptInterceptor
	at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
	at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:526)

I already tried to use the default discriminator (_t) but I got the very same error.

I'm really confused right now. Why is this happening?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the discriminator lookup and decoding paths named in the trace: dev/morphia/mapping/DiscriminatorLookup.java and dev/morphia/mapping/codec/pojo/EntityDecoder.java. Reproduce the read using the Interceptor, ScriptInterceptor, and XsltInterceptor mappings, then trace why the discriminator resolves to a class name that cannot be loaded. Done means polymorphic documents decode successfully with either discriminator configuration.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, mongodb
Domain
backend, databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.