Changeset 18824 in josm


Ignore:
Timestamp:
2023-09-07T19:36:52+02:00 (9 months ago)
Author:
taylor.smock
Message:

Fix #23153: Remote Control API call is adding hashtags many times

This occurs due to adding the hashtags to the comment multiple times.

This is fixed by doing the following:
1) When finding hashtags from a comment, only return the distinct hashtags
2) When adding hashtags from the dataset, only add hashtags that are not already

part of the comment.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/io/UploadDialogModel.java

    r18801 r18824  
    7676     */
    7777    String findHashTags(String comment) {
    78         String hashtags = Arrays.stream(comment.split("\\s", -1))
     78        String foundHashtags = Arrays.stream(comment.split("\\s", -1))
    7979            .map(s -> Utils.strip(s, ",;"))
    8080            .filter(s -> s.matches("#[a-zA-Z0-9][-_a-zA-Z0-9]+"))
    81             .collect(Collectors.joining(";"));
    82         return hashtags.isEmpty() ? null : hashtags;
     81            .distinct().collect(Collectors.joining(";"));
     82        return foundHashtags.isEmpty() ? null : foundHashtags;
    8383    }
    8484
     
    9696                Set<String> sanitizedHashtags = new LinkedHashSet<>();
    9797                for (String hashtag : hashtags.split(";", -1)) {
    98                     sanitizedHashtags.add(hashtag.startsWith("#") ? hashtag : "#" + hashtag);
     98                    if (comment == null || !comment.contains(hashtag)) {
     99                        sanitizedHashtags.add(hashtag.startsWith("#") ? hashtag : "#" + hashtag);
     100                    }
    99101                }
    100102                if (!sanitizedHashtags.isEmpty()) {
     
    108110    /**
    109111     * Inserts/updates/deletes a tag.
    110      *
     112     * <p>
    111113     * Existing keys are updated. Others are added. A value of {@code null}
    112114     * deletes the key.
     
    131133    /**
    132134     * Inserts/updates/deletes a tag.
    133      *
     135     * <p>
    134136     * Existing keys are updated. Others are added. A value of {@code null}
    135137     * deletes the key.
     
    147149    /**
    148150     * Inserts/updates/deletes all tags from {@code map}.
    149      *
     151     * <p>
    150152     * Existing keys are updated. Others are added. A value of {@code null}
    151153     * deletes the key.
  • trunk/src/org/openstreetmap/josm/gui/tagging/presets/TaggingPresetSelector.java

    r18823 r18824  
    2020import java.util.Set;
    2121import java.util.regex.Pattern;
    22 import java.util.stream.Collectors;
    2322
    2423import javax.swing.AbstractAction;
  • trunk/test/unit/org/openstreetmap/josm/gui/io/UploadDialogModelTest.java

    r18205 r18824  
    22package org.openstreetmap.josm.gui.io;
    33
    4 import static org.junit.jupiter.api.Assertions.assertNotNull;
     4import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
    55import static org.junit.jupiter.api.Assertions.assertNull;
    66import static org.junit.jupiter.api.Assertions.assertEquals;
    77
    8 import org.junit.jupiter.api.extension.RegisterExtension;
    98import org.junit.jupiter.api.Test;
    109
    1110import org.openstreetmap.josm.data.osm.DataSet;
    12 import org.openstreetmap.josm.testutils.JOSMTestRules;
     11import org.openstreetmap.josm.testutils.annotations.Main;
    1312
    1413/**
    1514 * Unit tests of {@link UploadDialogModel} class.
    1615 */
    17 public class UploadDialogModelTest {
    18     /**
    19      * Setup tests
    20      */
    21     @RegisterExtension
    22     public JOSMTestRules test = new JOSMTestRules().preferences().main();
    23 
     16@Main
     17class UploadDialogModelTest {
    2418    /**
    2519     * Test of {@link UploadDialogModel}.
     
    2721    @Test
    2822    void testUploadDialogModel() {
    29         assertNotNull(new UploadDialogModel());
     23        assertDoesNotThrow(UploadDialogModel::new);
    3024    }
    3125
     
    6559    }
    6660
     61    @Test
     62    void testNonRegression23153() {
     63        final DataSet dataSet = new DataSet();
     64        dataSet.getChangeSetTags().put("hashtags", "duplicate");
     65        final UploadDialogModel model = new UploadDialogModel();
     66        final String hashTags = model.findHashTags("#duplicate #duplicate hashtag");
     67        assertEquals("#duplicate", hashTags);
     68        final String commentHashtags = UploadDialogModel.addHashTagsFromDataSet("There should be no " + hashTags, dataSet);
     69        assertEquals("There should be no #duplicate", commentHashtags);
     70    }
     71
    6772}
Note: See TracChangeset for help on using the changeset viewer.