isXander / isXander/YetAnotherConfigLib

Inherited @SerialEntry fields are not saved in json

Open
#129 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Java
Stars
155
Forks
68
PR merge metrics
No merged PRs in 30d

Description

In the previous version of yacl, where config instances were built like so:
GsonConfigInstance.createBuilder(c) .setPath( Path.of(CONFIG_PATH,"cfg.json")).build();
If there was an abstract class A with a @ConfigEntry field, and it was extended by class B, then GsonConfigInstance.createBuilder(B.class) would, when built and saved, end up saving an instance of the @ConfigEntry fields in the abstract class. However, in the new version, where a config class handler is created as follows:
ConfigClassHandler.createBuilder(c)
Inherited fields are not saved in the config. Is this intended behavior for the new version?

A simple mixin can be used to emulate the behavior of previous versions:

@Mixin(targets = "dev/isxander/yacl3/config/v2/impl/ConfigClassHandlerImpl")
public class ConfigClassHandlerImplMixin {
    private static List<Field> getAllFields(Class<?> type) {
        List<Field> fields = new ArrayList<Field>();
        for (Class<?> c = type; c != null; c = c.getSuperclass()) {
            fields.addAll(Arrays.asList(c.getDeclaredFields()));
        }
        return fields;
    }

    @Redirect(method= "<init>(Ljava/lang/Class;Lnet/minecraft/util/Identifier;Ljava/util/function/Function;)V",
            at = @At(
                    value = "INVOKE", target = "Ljava/lang/Class;getDeclaredFields()[Ljava/lang/reflect/Field;"
            ))
    public Field[] redir(Class instance){
        List<Field> fs = getAllFields(instance);
        return fs.toArray(new Field[fs.size()]);
    }
}

Contributor guide

No contributing guide indexed for this repository

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 at ConfigClassHandler.createBuilder(c) and ConfigClassHandlerImpl’s constructor, especially the Class.getDeclaredFields() call shown in the issue. Reproduce the abstract A and extended B example and compare it with the previous version’s handling of inherited @ConfigEntry fields. Done when the intended behavior is confirmed and saving a B instance consistently handles inherited fields.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 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.