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

Deserializing nested records fails #190

Open
PaintNinja opened this issue Jan 2, 2025 · 0 comments
Open

Deserializing nested records fails #190

PaintNinja opened this issue Jan 2, 2025 · 0 comments

Comments

@PaintNinja
Copy link
Contributor

ObjectDeserializer#deserializeToRecord supports Java records, but doesn't support nesting. For example:

public final class Test {
    private Test() {}

    public record EarlyWindow(int width, int height, boolean maximized) {}
    public record ConfigRecord(boolean versionCheck, int maxThreads, EarlyWindow earlyWindow) {}

    public static void main(String[] args) {
        var obj = ObjectDeserializer.standard();
        var config = Config.inMemory();
        config.set("versionCheck", true);
        config.set("maxThreads", -1);
        config.set("earlyWindow.width", 854);
        config.set("earlyWindow.height", 480);
        config.set("earlyWindow.maximized", false);
        var parsed = obj.deserializeToRecord(config, ConfigRecord.class);
        System.out.println(parsed);
    }
}

The exception is:

Exception in thread "main" com.electronwill.nightconfig.core.serde.SerdeException: Failed to create an instance of record class ga.ozli.novaconfig.internal.Test$ConfigRecord
	at com.electronwill.nightconfig.core.serde.ConfigToPojoDeserializer.deserializeToRecord(ConfigToPojoDeserializer.java:86)
	at com.electronwill.nightconfig.core.serde.ConfigToPojoDeserializer.deserialize(ConfigToPojoDeserializer.java:35)
	at com.electronwill.nightconfig.core.serde.ConfigToPojoDeserializer.deserialize(ConfigToPojoDeserializer.java:19)
	at com.electronwill.nightconfig.core.serde.DeserializerContext.deserializeValue(DeserializerContext.java:36)
	at com.electronwill.nightconfig.core.serde.ObjectDeserializer.deserializeToRecord(ObjectDeserializer.java:120)
	at ga.ozli.novaconfig.internal.Test.main(Test.java:20)
Caused by: java.lang.IllegalArgumentException: argument type mismatch
	at java.base/jdk.internal.reflect.DirectConstructorHandleAccessor.newInstance(DirectConstructorHandleAccessor.java:65)
	at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:502)
	at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:486)
	at com.electronwill.nightconfig.core.serde.ConfigToPojoDeserializer.deserializeToRecord(ConfigToPojoDeserializer.java:84)
	... 5 more
Caused by: java.lang.ClassCastException: Cannot cast com.electronwill.nightconfig.core.SimpleConfig to ga.ozli.novaconfig.internal.Test$EarlyWindow
	at java.base/java.lang.Class.cast(Class.java:4067)
	at java.base/jdk.internal.reflect.DirectConstructorHandleAccessor.newInstance(DirectConstructorHandleAccessor.java:62)
	... 8 more
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants