jaydenseric / graphql-multipart-request-spec

A spec for GraphQL multipart form requests (file uploads).
993 stars 54 forks source link

GraphQlRequest.ToInputs throws an InvalidCastException for nested types. #36

Closed sergeyovchinnikov closed 4 years ago

sergeyovchinnikov commented 4 years ago

Hallo,

i'm testing the lib with my GraphQL schema and using a Type where the file property is on the second level in a GraphQL Input object. Because the Inputs type contains internally the Dictionary and not anothe Inputs, the code throws an InvalidCastException. Proposal: change the GetInputs function and cast to the Dictionary<string,object>

    public Inputs GetInputs()
    {
        var variables = Variables?.ToInputs();

        // the following implementation seems brittle because of a lot of casting
        // and it depends on the types that ToInputs() creates.

        foreach (var info in TokensToReplace)
        {
            int i = 0;
            object o = variables;

            foreach (var p in info.Parts)
            {
                var isLast = i++ == info.Parts.Count - 1;

                if (p is string s)
                {
                    if (!(o is IDictionary<string, object> dict))
                        break;
                    if (isLast)
                    {
                        dict[s] = info.File;
                    }
                    else
                    {
                        o = dict[s];
                    }
                }
                else if (p is int index)
                {
                    if (!(o is List<object> list))
                        break;

                    if (isLast)
                    {
                        list[index] = info.File;
                    }
                    else
                    {
                        o = list[index];
                    }
                }
            }
        }

        return variables;
    }
jaydenseric commented 4 years ago

Did you accidentally file this issue in the wrong repo? This is a specification, there is no code per se here.

Proposal: change the GetInputs function

There is no code with a GetInputs function in this repo. I'm not even certain what language that code you shared is.

sergeyovchinnikov commented 4 years ago

Sorry, I mean the GraphQL Upload.Net

пт, 24 июл. 2020 г., 13:02 Jayden Seric notifications@github.com:

Did you accidentally file this issue in the wrong repo? This is a specification, there is no code per se here.

Proposal: change the GetInputs function

There is no code with a GetInputs function in this repo. I'm not even certain what language that code you shared is.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/jaydenseric/graphql-multipart-request-spec/issues/36#issuecomment-663485348, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACQ3G3GXJM56RUFYF7AFNVDR5FS4RANCNFSM4PGTJITA .

jaydenseric commented 4 years ago

Ok! Good luck with the issue then :)