Repository navigation
Custom scalars on inputs are run through Jackson causing IllegalArgumentException #436
Copy link
Copy link
Open
Description
This seems to be a regression of #198 as far as I can tell. It seems to be the same issue that was supposedly fixed in 5.7, however, I'm still experiencing it on 6.2
Using a custom scalar type that is non-serializable by Jackson causes IllegalArgumentException when it is used as Input on a mutation. The scalar is run but the resulting object is still unnecessarily serialized by Jackson. The example given is for java LocalDate, however, the issue appears with my own custom classes as well so simply adding the Jackson time module is no help.
Java version: 14
Graphql-java-tools version 6.2.0 (Also happened on 6.0.2 and 6.1)
Graphql-java version 15.0
graphql-java-spring-boot-starter-webmvc 2.0
Custom scalar:
public class LocalDateScalar {
private static final String NAME = "LocalDate";
private static final String DESCRIPTION = "Java LocalDate";
private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ISO_DATE;
private static final Coercing<LocalDate, String> COERCING = new Coercing<LocalDate, String>() {
private LocalDate convertImpl(Object pInput) {
if (pInput instanceof StringValue) {
return LocalDate.parse(((StringValue) pInput).getValue(), FORMATTER);
} else {
return null;
}
}
@Override
public String serialize(Object pDataFetcherResult) throws CoercingSerializeException {
if (pDataFetcherResult instanceof LocalDate) {
return FORMATTER.format((LocalDate) pDataFetcherResult);
}
throw new CoercingParseValueException("Expected Date but got " + pDataFetcherResult.getClass().getName());
}
@Override
public LocalDate parseValue(Object pInput) throws CoercingParseValueException {
LocalDate result = convertImpl(pInput);
if (result == null) {
throw new CoercingSerializeException(
"Expected type 'Date' but was '" + pInput.getClass().getName() + "'."
);
} else {
return result;
}
}
@Override
public LocalDate parseLiteral(Object pInput) throws CoercingParseLiteralException {
LocalDate result = convertImpl(pInput);
if (result == null) {
throw new CoercingParseLiteralException(
"Expected type 'Date' but was '" + pInput.getClass().getName() + "'."
);
} else {
return result;
}
}
};
private LocalDateScalar() {
}
public static GraphQLScalarType get() {
return GraphQLScalarType.newScalar().coercing(COERCING).name(NAME).description(DESCRIPTION).build();
}
}Example schema
scalar LocalDate
type Mutation {
newThing(value: SomeInput): SomeResult
}
input SomeInput {
date: LocalDate
}Scalar registration
private GraphQLSchema buildSchema() {
return SchemaParser.newParser()
.file("schema.graphqls")
.resolvers(// resolvers)
.scalars(
LocalDateScalar.get())
.build()
.makeExecutableSchema();
}Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels
Activity
vojtapol commentedon Sep 17, 2020
Can you please attach the stack trace with the exception that you get?
jothebuzzyard commentedon Sep 17, 2020
Stacktrace provided. Sorry, I missed it at first, I was in a rush.
getnosleep commentedon May 11, 2021
Have the same Error. In my case there is following example:
GRAPHQLS
JAVA
FUN FACT:
The create-method and everything else works fine for objectA, but when I try with objectB following exception raises
EXCEPTION
I hope this helps figuring out where the problem is 😩
getnosleep commentedon May 11, 2021
In my case it was a TypeErasure and Deserialization problem on my own objects. So it's more like a Java feature 💯.
I solved it by adding @JsonDeserialize(as=PointDto.class) to SomeComparableSuperpointThatIsSuperOfEntityAndDto
Are you able to try this in your Some Object from SomeInput on LocalDate, like:
I used this import:
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;Maybe I'm completely wrong and this is not comparable in any way... If so, tell me and I'll delete this posts 😄