public ApiMessageAndVersion toRecord(Uuid topicId, int partitionId, ImageWriterOptions options) {
        PartitionRecord record = new PartitionRecord().
            setPartitionId(partitionId).
            setTopicId(topicId).
            setReplicas(Replicas.toList(replicas)).
            setIsr(Replicas.toList(isr)).
            setRemovingReplicas(Replicas.toList(removingReplicas)).
            setAddingReplicas(Replicas.toList(addingReplicas)).
            setLeader(leader).
            setLeaderRecoveryState(leaderRecoveryState.value()).
            setLeaderEpoch(leaderEpoch).
            setPartitionEpoch(partitionEpoch);
        if (options.metadataVersion().isElrSupported()) {
            // The following are tagged fields, we should only set them when there are some contents, in order to save
            // spaces.
            if (elr.length > 0) record.setEligibleLeaderReplicas(Replicas.toList(elr));
            if (lastKnownElr.length > 0) record.setLastKnownElr(Replicas.toList(lastKnownElr));
        }
        if (options.metadataVersion().isDirectoryAssignmentSupported()) {
            record.setDirectories(Uuid.toList(directories));
        } else {
            for (Uuid directory : directories) {
                if (!DirectoryId.MIGRATING.equals(directory)) {
                    options.handleLoss("the directory assignment state of one or more replicas");
                    break;
                }
            }
        }
        return new ApiMessageAndVersion(record, options.metadataVersion().partitionRecordVersion());
    }
@Test
    public void testPartitionRegistrationToRecord_ElrShouldBeNullIfEmpty() {
        PartitionRegistration.Builder builder = new PartitionRegistration.Builder().
            setReplicas(new int[]{0, 1, 2, 3, 4}).
            setDirectories(DirectoryId.migratingArray(5)).
            setIsr(new int[]{0, 1}).
            setLeader(0).
            setLeaderRecoveryState(LeaderRecoveryState.RECOVERED).
            setLeaderEpoch(0).
            setPartitionEpoch(0);
        PartitionRegistration partitionRegistration = builder.build();
        Uuid topicID = Uuid.randomUuid();
        PartitionRecord expectRecord = new PartitionRecord().
            setTopicId(topicID).
            setPartitionId(0).
            setReplicas(Arrays.asList(0, 1, 2, 3, 4)).
            setIsr(Arrays.asList(0, 1)).
            setLeader(0).
            setLeaderRecoveryState(LeaderRecoveryState.RECOVERED.value()).
            setLeaderEpoch(0).
            setDirectories(Arrays.asList(DirectoryId.migratingArray(5))).
            setPartitionEpoch(0);
        List<UnwritableMetadataException> exceptions = new ArrayList<>();
        ImageWriterOptions options = new ImageWriterOptions.Builder().
            setMetadataVersion(MetadataVersion.IBP_4_0_IV0).
            setLossHandler(exceptions::add).
            build();
        assertEquals(new ApiMessageAndVersion(expectRecord, (short) 2), partitionRegistration.toRecord(topicID, 0, options));
        assertEquals(Replicas.toList(Replicas.NONE), Replicas.toList(partitionRegistration.addingReplicas));
        assertTrue(exceptions.isEmpty());
    }