Skip to content

Commit

Permalink
fix (core): EmptyResource URI with MediaType with parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
vorburger committed Jan 17, 2024
1 parent 6aa2ba6 commit 2442583
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,14 @@ public class EmptyResource implements ReadableResource {
// TODO Perhaps rename this to VoidResource with void:/ URI?

static final String SCHEME = "empty";

private static final URI EMPTY_URI = URI.create(SCHEME + ":?");
private final MediaType mediaType;

private final URI uri;

public EmptyResource(MediaType mediaType) {
this.mediaType = mediaType;
// TODO Why withoutParameters()? Remove!
this.uri = URI.create(SCHEME + ":" + mediaType.withoutParameters());
this.uri = URIs.addMediaType(EMPTY_URI, mediaType);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,20 @@ public static URI addMediaType(URI uri, MediaType mediaType) {
}

private static URI addQueryParameter(URI uri, String key, String value) {
String connector;
if (uri.getQuery() != null && uri.getQuery().contains(key)) {
return uri;
} else if (uri.getQuery() == null) {
return URI.create(uri.toString() + "?" + key + "=" + encodeQueryParameterValue(value));
if (!uri.getSchemeSpecificPart().contains("?")) {
connector = "?";
} else {
// Special case of "scheme:?"-like URIs with "empty" query
connector = "";
}
} else {
return URI.create(uri.toString() + "&" + key + "=" + encodeQueryParameterValue(value));
connector = "&";
}
return URI.create(uri + connector + key + "=" + encodeQueryParameterValue(value));
}

private static String encodeQueryParameterValue(String value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.junit.Test;

import java.io.IOException;
import java.net.URI;

public class EmptyResourceTest {
@Test
Expand All @@ -32,6 +33,7 @@ public void testEmptyResource() throws IOException {
assertThat(e.byteSource().isEmpty()).isTrue();
assertThat(e.charSource().isEmpty()).isTrue();
assertThat(e.mediaType()).isEqualTo(YamlMediaType.YAML_UTF_8);
assertThat(e.uri().toString()).isEqualTo("empty:application/yaml");
assertThat(e.uri())
.isEqualTo(URI.create("empty:?mediaType=application%2Fyaml%3Bcharset%3Dutf-8"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ public void testAddMediaType() throws URISyntaxException {
var uri2expected = URI.create("scheme:something?mediaType=text%2Fplain%3Bcharset%3Dutf-8");
assertThat(uri2).isEqualTo(uri2expected);
assertThat(URIs.getMediaType(uri2expected)).isEqualTo(mt2);

var uri3 = URIs.addMediaType(URI.create("scheme:?"), mt1);
var uri3expected = URI.create("scheme:?mediaType=image%2Fgif");
assertThat(uri3).isEqualTo(uri3expected);
}

@Test
Expand Down
2 changes: 1 addition & 1 deletion docs/use/list/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Note how the section above showed some additional entity kinds, in addition to t
It's possible to list only those built-in entity kinds, using an "empty" model URI, like this:

```bash cd .././.././..
$ ./enola list --model empty:application/json enola.entity_kind
$ ./enola list --model "empty:?mediaType=application/json" enola.entity_kind
...
```

Expand Down
2 changes: 1 addition & 1 deletion docs/use/list/schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@
Because the "schemas" are internally Entities (of _Kind_ `enola.schema`), they can also [be listed](index.md):

```bash cd .././.././..
$ ./enola list --model empty:application/json --format=yaml enola.schema
$ ./enola list --model "empty:?mediaType=application/json" --format=yaml enola.schema
...
```

0 comments on commit 2442583

Please sign in to comment.