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

Cannot correct get response if pass an array with Single array in a PostAsync call, C# #1927

Closed
jillzhoucnca opened this issue Oct 26, 2022 · 2 comments · Fixed by #1928
Closed
Assignees
Labels
Csharp Pull requests that update .net code Go Java type:bug A broken experience TypeScript Pull requests that update Javascript code

Comments

@jillzhoucnca
Copy link

This is a part in create Post request function:

        requestInfo.SetContentFromScalar(RequestAdapter, "application/json-patch+json", body.ToArray());

in the SetContentFromScalar, if our body is a single value like["abc"]. it cannot be passed successfully. Because of this part of code

 public void SetContentFromScalar<T>(IRequestAdapter requestAdapter, string contentType, params T[] items)
        {
            using var writer = getSerializationWriter(requestAdapter, contentType, items);
            if(items.Count() == 1)
                switch(items[0])
                {
                    case string s:
                        writer.WriteStringValue(null, s);
                        break;
                    case bool b:
                        writer.WriteBoolValue(null, b);
                        break;
                    case byte b:
                        writer.WriteByteValue(null, b);
                        break;
                    case sbyte b:
                        writer.WriteSbyteValue(null, b);
                        break;
                    case int i:
                        writer.WriteIntValue(null, i);
                        break;
                    case float f:
                        writer.WriteFloatValue(null, f);
                        break;
                    case long l:
                        writer.WriteLongValue(null, l);
                        break;
                    case double d:
                        writer.WriteDoubleValue(null, d);
                        break;
                    case Guid g:
                        writer.WriteGuidValue(null, g);
                        break;
                    case DateTimeOffset dto:
                        writer.WriteDateTimeOffsetValue(null, dto);
                        break;
                    case TimeSpan timeSpan:
                        writer.WriteTimeSpanValue(null, timeSpan);
                        break;
                    case Date date:
                        writer.WriteDateValue(null, date);
                        break;
                    case Time time:
                        writer.WriteTimeValue(null, time);
                        break;
                    case null:
                        writer.WriteNullValue(null);
                        break;
                    default:
                        throw new InvalidOperationException($"error serialization data value with unknown type {items[0]?.GetType()}");
                }
            else
                writer.WriteCollectionOfPrimitiveValues(null, items);
            Headers.Add(ContentTypeHeader, contentType);
            Content = writer.GetSerializedContent();
        }

If I provide more than one value, it can give us the correct response.
@andrueastman

@baywet baywet added Csharp Pull requests that update .net code Java TypeScript Pull requests that update Javascript code Go type:bug A broken experience labels Oct 26, 2022
@baywet
Copy link
Member

baywet commented Oct 26, 2022

The solution here is to break the set content from scalar method in 2 overloads:

  • one that takes a single parameter
  • one that takes an enumerable (no params)

On the generation front we probably don't need to call to array anymore for collections.

The same problem might also apply to set content from parsable.

@andrueastman can you fix it for dotnet please? Once it's done I'll create additional issues for other languages.

@andrueastman
Copy link
Member

Thanks for handling this one @baywet

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Csharp Pull requests that update .net code Go Java type:bug A broken experience TypeScript Pull requests that update Javascript code
Projects
None yet
3 participants