Skip to content

Commit e448bee

Browse files
committed
Adjust URI scalar
1 parent 335c46b commit e448bee

File tree

2 files changed

+51
-55
lines changed

2 files changed

+51
-55
lines changed

src/main/java/graphql/scalars/uri/UriScalar.java

Lines changed: 33 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import graphql.schema.GraphQLScalarType;
1313

1414
import java.io.File;
15-
import java.net.MalformedURLException;
1615
import java.net.URI;
1716
import java.net.URISyntaxException;
1817
import java.net.URL;
@@ -31,86 +30,83 @@ private UriScalar() {
3130
public static final GraphQLScalarType INSTANCE;
3231

3332
static {
34-
Coercing<URL, URL> coercing = new Coercing<>() {
33+
Coercing<URI, URI> coercing = new Coercing<>() {
3534
@Override
36-
public URL serialize(Object input, GraphQLContext graphQLContext, Locale locale) throws CoercingSerializeException {
37-
Optional<URL> url;
35+
public URI serialize(Object input, GraphQLContext graphQLContext, Locale locale) throws CoercingSerializeException {
36+
Optional<URI> uri;
3837
if (input instanceof String) {
39-
url = Optional.of(parseURL(input.toString(), CoercingSerializeException::new));
38+
uri = Optional.of(parseURI(input.toString(), CoercingSerializeException::new));
4039
} else {
41-
url = toURL(input);
40+
uri = toURI(input);
4241
}
43-
if (url.isPresent()) {
44-
return url.get();
42+
if (uri.isPresent()) {
43+
return uri.get();
4544
}
4645
throw new CoercingSerializeException(
47-
"Expected a 'URL' like object but was '" + typeName(input) + "'."
46+
"Expected a 'URI' like object but was '" + typeName(input) + "'."
4847
);
4948
}
5049

5150
@Override
52-
public URL parseValue(Object input, GraphQLContext graphQLContext, Locale locale) throws CoercingParseValueException {
53-
String urlStr;
51+
public URI parseValue(Object input, GraphQLContext graphQLContext, Locale locale) throws CoercingParseValueException {
52+
String uriStr;
5453
if (input instanceof String) {
55-
urlStr = String.valueOf(input);
54+
uriStr = String.valueOf(input);
5655
} else {
57-
Optional<URL> url = toURL(input);
58-
if (url.isEmpty()) {
56+
Optional<URI> uri = toURI(input);
57+
if (uri.isEmpty()) {
5958
throw new CoercingParseValueException(
60-
"Expected a 'URL' like object but was '" + typeName(input) + "'."
59+
"Expected a 'URI' like object but was '" + typeName(input) + "'."
6160
);
6261
}
63-
return url.get();
62+
return uri.get();
6463
}
65-
return parseURL(urlStr, CoercingParseValueException::new);
64+
return parseURI(uriStr, CoercingParseValueException::new);
6665
}
6766

6867
@Override
69-
public URL parseLiteral(Value<?> input, CoercedVariables variables, GraphQLContext graphQLContext, Locale locale) throws CoercingParseLiteralException {
68+
public URI parseLiteral(Value<?> input, CoercedVariables variables, GraphQLContext graphQLContext, Locale locale) throws CoercingParseLiteralException {
7069
if (!(input instanceof StringValue)) {
7170
throw new CoercingParseLiteralException(
7271
"Expected AST type 'StringValue' but was '" + typeName(input) + "'."
7372
);
7473
}
75-
return parseURL(((StringValue) input).getValue(), CoercingParseLiteralException::new);
74+
return parseURI(((StringValue) input).getValue(), CoercingParseLiteralException::new);
7675
}
7776

7877
@Override
7978
public Value<?> valueToLiteral(Object input, GraphQLContext graphQLContext, Locale locale) {
80-
URL url = serialize(input, graphQLContext, locale);
81-
return StringValue.newStringValue(url.toExternalForm()).build();
79+
URI uri = serialize(input, graphQLContext, locale);
80+
return StringValue.newStringValue(uri.toString()).build();
8281
}
8382

8483

85-
private URL parseURL(String input, Function<String, RuntimeException> exceptionMaker) {
84+
private URI parseURI(String input, Function<String, RuntimeException> exceptionMaker) {
8685
try {
87-
return new URI(input).toURL();
88-
} catch (URISyntaxException | IllegalArgumentException | MalformedURLException e) {
89-
throw exceptionMaker.apply("Invalid URL value : '" + input + "'.");
86+
return new URI(input);
87+
} catch (URISyntaxException e) {
88+
throw exceptionMaker.apply("Invalid URI value : '" + input + "'.");
9089
}
9190
}
9291
};
9392

9493
INSTANCE = GraphQLScalarType.newScalar()
95-
.name("Url")
96-
.description("A Url scalar")
94+
.name("Uri")
95+
.description("A Uri scalar")
9796
.coercing(coercing)
9897
.build();
9998
}
10099

101-
private static Optional<URL> toURL(Object input) {
102-
if (input instanceof URL) {
103-
return Optional.of((URL) input);
104-
} else if (input instanceof URI) {
100+
private static Optional<URI> toURI(Object input) {
101+
if (input instanceof URI) {
102+
return Optional.of((URI) input);
103+
} else if (input instanceof URL) {
105104
try {
106-
return Optional.of(((URI) input).toURL());
107-
} catch (MalformedURLException ignored) {
105+
return Optional.of(((URL) input).toURI());
106+
} catch (URISyntaxException ignored) {
108107
}
109108
} else if (input instanceof File) {
110-
try {
111-
return Optional.of(((File) input).toURI().toURL());
112-
} catch (MalformedURLException ignored) {
113-
}
109+
return Optional.of(((File) input).toURI());
114110
}
115111
return Optional.empty();
116112
}

src/test/groovy/graphql/scalars/uri/UriScalarTest.groovy

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ class UriScalarTest extends AbstractScalarTest {
2525
result == expectedResult
2626
where:
2727
input | expectedResult
28-
new URL("http://www.graphql-java.com/") | new URL("http://www.graphql-java.com/")
29-
new URI("http://www.graphql-java.com/") | new URL("http://www.graphql-java.com/")
30-
new File("/this/that") | new URL("file:/this/that")
31-
"http://www.graphql-java.com/" | new URL("http://www.graphql-java.com/")
28+
new URL("http://www.graphql-java.com/") | new URI("http://www.graphql-java.com/")
29+
new URI("http://www.graphql-java.com/") | new URI("http://www.graphql-java.com/")
30+
new File("/this/that") | new URI("file:/this/that")
31+
"http://www.graphql-java.com/" | new URI("http://www.graphql-java.com/")
3232
}
3333

3434
@Unroll
@@ -53,9 +53,9 @@ class UriScalarTest extends AbstractScalarTest {
5353
then:
5454
thrown(exceptionClas)
5555
where:
56-
input || exceptionClas
57-
666 || CoercingSerializeException
58-
"not/a/url" || CoercingSerializeException
56+
input || exceptionClas
57+
666 || CoercingSerializeException
58+
"1:not/a/uri" || CoercingSerializeException
5959
}
6060

6161
@Unroll
@@ -66,10 +66,10 @@ class UriScalarTest extends AbstractScalarTest {
6666
result == expectedResult
6767
where:
6868
input | expectedResult
69-
new URL("http://www.graphql-java.com/") | new URL("http://www.graphql-java.com/")
70-
new URI("http://www.graphql-java.com/") | new URL("http://www.graphql-java.com/")
71-
new File("/this/that") | new URL("file:/this/that")
72-
"http://www.graphql-java.com/" | new URL("http://www.graphql-java.com/")
69+
new URL("http://www.graphql-java.com/") | new URI("http://www.graphql-java.com/")
70+
new URI("http://www.graphql-java.com/") | new URI("http://www.graphql-java.com/")
71+
new File("/this/that") | new URI("file:/this/that")
72+
"http://www.graphql-java.com/" | new URI("http://www.graphql-java.com/")
7373
}
7474

7575
@Unroll
@@ -79,9 +79,9 @@ class UriScalarTest extends AbstractScalarTest {
7979
then:
8080
thrown(exceptionClas)
8181
where:
82-
input || exceptionClas
83-
666 || CoercingParseValueException
84-
"not/a/url" || CoercingParseValueException
82+
input || exceptionClas
83+
666 || CoercingParseValueException
84+
"1:not/a/url" || CoercingParseValueException
8585
}
8686

8787
@Unroll
@@ -92,7 +92,7 @@ class UriScalarTest extends AbstractScalarTest {
9292
result == expectedResult
9393
where:
9494
input | expectedResult
95-
new StringValue("http://www.graphql-java.com/") | new URL("http://www.graphql-java.com/")
95+
new StringValue("http://www.graphql-java.com/") | new URI("http://www.graphql-java.com/")
9696
}
9797

9898
@Unroll
@@ -102,9 +102,9 @@ class UriScalarTest extends AbstractScalarTest {
102102
then:
103103
thrown(exceptionClas)
104104
where:
105-
input | exceptionClas
106-
new BooleanValue(true) | CoercingParseLiteralException
107-
new StringValue("1:not/a/uri") | CoercingParseLiteralException
105+
input | exceptionClas
106+
new BooleanValue(true) | CoercingParseLiteralException
107+
new StringValue("1:not/a/url") | CoercingParseLiteralException
108108
}
109109

110110

0 commit comments

Comments
 (0)