package com.alibaba.fastjson2.issues_2700;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONWriter;
import com.google.common.collect.Lists;
import lombok.Data;
import org.junit.jupiter.api.Test;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class Issue2712 {
    @Test
    void test() {
        ClueListInfo info1 = new ClueListInfo();
        info1.setClueId(1L);
        info1.setName("Clue 01");

        ClueListInfo info2 = new ClueListInfo();
        info2.setClueId(2L);
        info2.setName("Clue 02");

        ContactInfo contactInfo = new ContactInfo();
        contactInfo.setId(1L);
        contactInfo.setName("contact");
        ArrayList<ContactInfo> contacts = Lists.newArrayList(contactInfo);

        info1.setContacts(contacts);
        info2.setContacts(contacts);
        List<ClueListInfo> infos = Lists.newArrayList(info1, info2);

        String jsonString = JSON.toJSONString(infos, JSONWriter.Feature.ReferenceDetection);
        assertEquals("[{\"clueId\":1,\"contacts\":[{\"id\":1,\"name\":\"contact\"}],\"name\":\"Clue 01\"},{\"clueId\":2,\"contacts\":{\"$ref\":\"$[0].contacts\"},\"name\":\"Clue 02\"}]", jsonString);
        List<ClueListInfo> clueListInfos = JSON.parseArray(jsonString, ClueListInfo.class);
        assertEquals(clueListInfos.get(0).getContacts(), clueListInfos.get(1).getContacts());
    }

    @Data
    public static class ClueListInfo
            implements Serializable {
        private static final long serialVersionUID = 8170584149020082450L;

        private Long clueId;

        private String name;

        private List<ContactInfo> contacts;
    }

    @Data
    public static class ContactInfo
            implements Serializable {
        private static final long serialVersionUID = -5313505758293424804L;

        private Long id;

        private String name;
    }
}
