Skip to content

No Object Id found for an instance of 'class', to assign to property '@id' #550

@Asayu233

Description

@Asayu233

Search before asking

  • I searched in the issues and found nothing similar.

Describe the bug

When running the following code, it throws MismatchedInputException. I don't know what happened.

Version Information

2.18.3

Reproduction

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;

import java.util.List;
import java.util.Map;

@JsonIdentityInfo(generator = ObjectIdGenerators.UUIDGenerator.class)
class Ability {
    private final String name;
    private final Map<String, List<Modifier>> modifiers;

    @JsonCreator
    public Ability(@JsonProperty("name") String name,
                   @JsonProperty("modifiers") Map<String, List<Modifier>> modifiers) {
        this.name = name;
        this.modifiers = modifiers;
    }

    public String getName() {
        return name;
    }

    public Map<String, List<Modifier>> getModifiers() {
        return modifiers;
    }
}

class Modifier {
    private final String name;
    private final Ability parent;

    @JsonCreator
    public Modifier(@JsonProperty("name") String name,
                    @JsonProperty("parent") Ability parent) {
        this.name = name;
        this.parent = parent;
    }

    public String getName() {
        return name;
    }

    public Ability getParent() {
        return parent;
    }
}

public class Main {
    public static void main(String[] args) {
        String yaml = """
            &12345678-aaaa-bbbb-cccc-87654321abcd
            name: TestAbility
            modifiers:
              TestModifier:
              - name: TestModifier
                parent: *12345678-aaaa-bbbb-cccc-87654321abcd
            """;

        ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
        try {
            mapper.readValue(yaml, Ability.class);
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}

Expected behavior

No response

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    to-evaluateIssue that has been received but not yet evaluatedyamlIssue related to YAML format backend

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions