Skip to content
This repository has been archived by the owner on Jan 27, 2025. It is now read-only.

Fix function "uri_encode" to be backwards compatible #337

Merged
merged 4 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -715,8 +715,21 @@ upcase("<sourceField>")

Encodes a field value as URI. Aka percent-encoding.

Options:

- `plus_for_space`: Sets whether "space" (' ') will be substituted by a "plus" ('+') or be percent escaped ('%20'). Default: `true`)
dr0i marked this conversation as resolved.
Show resolved Hide resolved
- `safe_chars`: Sets characters that won't be escaped. Safe characters are the
ranges 0..9, a..z and A..Z. These are always safe and should not be specified.
Default safe characters are also ".", "-", "*", and "_".
dr0i marked this conversation as resolved.
Show resolved Hide resolved

```perl
uri_encode("<sourceField>"[, <options>...])
```

E.g.:

```perl
uri_encode("<sourceField>")
uri_encode("path.to.field", plus_for_space:"false", safe_chars:"")
```

### Selectors
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ subprojects {
'jquery': '3.3.1-1',
'junit_jupiter': '5.8.2',
'junit_platform': '1.4.2',
'metafacture': '5.7.0-rc1',
'metafacture': '5.7.0-rc2',
'mockito': '2.27.0',
'requirejs': '2.3.6',
'slf4j': '1.7.21',
Expand Down
3 changes: 2 additions & 1 deletion metafix/src/main/java/org/metafacture/metafix/FixMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,8 @@ public void apply(final Metafix metafix, final Record record, final List<String>
@Override
public void apply(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
final URLEncode urlEncoder = new URLEncode();
urlEncoder.setPlusForSpace(false);
withOption(options, "safe_chars", urlEncoder::setSafeChars);
withOption(options, "plus_for_space", urlEncoder::setPlusForSpace, this::getBoolean);

record.transform(params.get(0), urlEncoder::process);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4015,12 +4015,48 @@ public void shouldUriEncodePathSegment() {
),
i -> {
i.startRecord("1");
i.literal("id", "slash/990223521400206441:DE-A96:61 TYD 16(3)#!");
i.literal("id", "/DE-A96:% (3)#!");
i.endRecord();
},
o -> {
o.get().startRecord("1");
o.get().literal("id", "slash%2F990223521400206441%3ADE%2DA96%3A61%20TYD%2016%283%29%23%21");
o.get().literal("id", "%2FDE-A96%3A%25+%283%29%23%21");
o.get().endRecord();
}
);
}

@Test
public void shouldUriEncodePathSegmentWithoutPlusForSpace() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"uri_encode('id', plus_for_space:'false')"
),
i -> {
i.startRecord("1");
i.literal("id", "/DE-A96:% (3)#!");
i.endRecord();
},
o -> {
o.get().startRecord("1");
o.get().literal("id", "%2FDE-A96%3A%25%20%283%29%23%21");
o.get().endRecord();
}
);
}

@Test
public void shouldUriEncodePathSegmentWithoutSafeChars() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"uri_encode('id', safe_chars:'')"
),
i -> {
i.startRecord("1");
i.literal("id", "/DE-A96:% (3)#!");
i.endRecord();
},
o -> {
o.get().startRecord("1");
o.get().literal("id", "%2FDE%2DA96%3A%25+%283%29%23%21");
o.get().endRecord();
}
);
Expand Down
Loading