Skip to content

Custom scalars on inputs are run through Jackson causing IllegalArgumentException #436

Open
@thecrazything

Description

@thecrazything

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();
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions