Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: Custom scalar does not deserialize correctly with 4.6.0 #588

Closed
kmccarp opened this issue Aug 30, 2021 · 7 comments
Closed

bug: Custom scalar does not deserialize correctly with 4.6.0 #588

kmccarp opened this issue Aug 30, 2021 · 7 comments
Labels
bug Something isn't working

Comments

@kmccarp
Copy link

kmccarp commented Aug 30, 2021

We have a type defined like this:

input FooInput {
    bars: [BarInput!]
}

input BarInput {
    name: String!
    value: Object!
}

scalar Object

scalar Object is defined as follows:

@DgsComponent
public class ObjectScalar {

    @DgsRuntimeWiring
    public RuntimeWiring.Builder addScalar(RuntimeWiring.Builder builder) {
        return builder.scalar(ExtendedScalars.Object);
    }
}

Expected behavior

When calling a mutation with FooInput as input, I expect it to deserialize bars as List<BarInput>.

Actual behavior

When calling a mutation with FooInput as input, it deserializes bars as List<LinkedHashMap> instead of List<BarInput>.

Steps to reproduce

  1. Create types as defined in summary
  2. Create a mutation that takes FooInput as input
  3. Generate graphql types as necessary
  4. Use a graphql client to call your mutation
  5. See that BarInput is deserialized to LinkedHashMap (i.e. not successfully deserialized to BarInput)

Might be related to (or fixed by?) this: #344

@kmccarp kmccarp added the bug Something isn't working label Aug 30, 2021
@kmccarp kmccarp changed the title bug: Custom scalar bug: Custom scalar does not deserialize correctly with 4.6.0 Aug 30, 2021
@paulbakker
Copy link
Collaborator

My guess is that you forgot to use the collectionType argument which is described here.

The related change in 4.6.0 is this: #549. Prior to this change, some code that was incorrect was accidentally "working" because of the extra conversion Jackson was doing.
Can you try using @InputArgument(collectionType = BarInput.class) to confirm that works correctly?

I've added a test specifically for the Object scalar, but that seems to work fine and is probably unrelated to the issue you're seeing. #589.

@paulbakker
Copy link
Collaborator

Closing this as invalid, based on the test I've added (see comment above). Please re-open if that turns out to be incorrect.

@paulbakker paulbakker added the invalid This doesn't seem right label Aug 30, 2021
@nsnichols
Copy link

nsnichols commented Aug 30, 2021

The test scenario doesn't handle complex input objects with nested lists. If your top level object is not a list, but it contains a list, the objects in the list don't get converted to the correct types. They remain maps and then any logic that uses the values in the list blow up with a class cast exception.

I see this issue when there are no custom scalars involved.

input Complex {
    something: String!
    list: [InList!]!
}

input InList {
    field1: String!
    field2: String!
}

type Out {
    value: String!
}

type Mutation {
    complex(arg: Complex!): Out!
 }
    @DgsComponent
    public static class Mutation {
        @DgsMutation(field = DgsConstants.MUTATION.Complex)
        public Out executeMutation(@InputArgument("arg") Complex complex) {
            // Blows up on the next line
            var first = complex.getList().get(0);
            return new Out("whatever");
        }
    }

@paulbakker
Copy link
Collaborator

Thanks for the additional context, let me look into it further.

@kmccarp
Copy link
Author

kmccarp commented Aug 31, 2021

To add to @nsnichols 's comment, if you had two list types, collectionType cannot be specified for both:

input FooInput {
    bars: [BarInput!]
    bazs: [BazInput!]
}

input BarInput {
    name: String!
    value: Object!
}

input BazInput {
  id: String!
  value: String!
}

scalar Object

@paulbakker
Copy link
Collaborator

Thanks for clarifying the issue - I was obviously looking at the wrong thing and this bug was a huge oversight.
And yes, collectionType was clearly not the right answer here.

Anyway, I think I have a fix for the issue. It's more reflection magic, so we'll need to do some more testing, but it looks promising.

The fix is in #591

@paulbakker
Copy link
Collaborator

Fix is released in 4.7.0. Hopefully for real this time!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants