Skip to content

Commit

Permalink
- implements go multipart request body
Browse files Browse the repository at this point in the history
  • Loading branch information
baywet committed Aug 1, 2023
1 parent eeadd4f commit 806e329
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 14 deletions.
20 changes: 14 additions & 6 deletions src/Kiota.Builder/Refiners/GoRefiner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ public override Task Refine(CodeNamespace generatedCode, CancellationToken cance
"github.com/microsoft/kiota-serialization-json-go.JsonSerializationWriterFactory",
"github.com/microsoft/kiota-serialization-text-go.TextSerializationWriterFactory",
"github.com/microsoft/kiota-serialization-form-go.FormSerializationWriterFactory",
"github.com/microsoft/kiota-serialization-multipart-go.MultipartSerializationWriterFactory",
});
ReplaceDefaultDeserializationModules(
generatedCode,
Expand Down Expand Up @@ -506,9 +507,9 @@ private static void AddErrorImportForEnums(CodeElement currentElement)
};
private static readonly AdditionalUsingEvaluator[] defaultUsingEvaluators = {
new (static x => x is CodeProperty prop && prop.IsOfKind(CodePropertyKind.RequestAdapter),
"github.com/microsoft/kiota-abstractions-go", "RequestAdapter"),
AbstractionsNamespaceName, "RequestAdapter"),
new (static x => x is CodeMethod method && method.IsOfKind(CodeMethodKind.RequestGenerator),
"github.com/microsoft/kiota-abstractions-go", "RequestInformation", "HttpMethod", "RequestOption"),
AbstractionsNamespaceName, "RequestInformation", "HttpMethod", "RequestOption"),
new (static x => x is CodeMethod method && method.IsOfKind(CodeMethodKind.Constructor) &&
method.Parameters.Any(x => x.IsOfKind(CodeParameterKind.Path) &&
!typeToSkipStrConv.Contains(x.Type.Name)),
Expand Down Expand Up @@ -537,12 +538,16 @@ x.Type is CodeType pType &&
new (static x => x is CodeClass @class && @class.OriginalComposedType is CodeIntersectionType intersectionType && intersectionType.Types.Any(static y => !y.IsExternal) && intersectionType.DiscriminatorInformation.HasBasicDiscriminatorInformation,
"github.com/microsoft/kiota-abstractions-go/serialization", "MergeDeserializersForIntersectionWrapper"),
new (static x => x is CodeProperty prop && prop.IsOfKind(CodePropertyKind.Headers),
"github.com/microsoft/kiota-abstractions-go", "RequestHeaders"),
AbstractionsNamespaceName, "RequestHeaders"),
new (static x => x is CodeProperty prop && prop.IsOfKind(CodePropertyKind.BackingStore), "github.com/microsoft/kiota-abstractions-go/store","BackingStore"),
new (static x => x is CodeMethod method && method.IsOfKind(CodeMethodKind.ClientConstructor) &&
method.Parameters.Any(y => y.IsOfKind(CodeParameterKind.BackingStore)),
"github.com/microsoft/kiota-abstractions-go/store", "BackingStoreFactory"),
new (static x => x is CodeMethod method && method.IsOfKind(CodeMethodKind.RequestExecutor, CodeMethodKind.RequestGenerator) && method.Parameters.Any(static y => y.IsOfKind(CodeParameterKind.RequestBody) && y.Type.Name.Equals(MultipartBodyClassName, StringComparison.OrdinalIgnoreCase)),
AbstractionsNamespaceName, MultipartBodyClassName),
};
private const string MultipartBodyClassName = "MultipartBody";
private const string AbstractionsNamespaceName = "github.com/microsoft/kiota-abstractions-go";

private void CorrectImplements(ProprietableBlockDeclaration block)
{
Expand All @@ -552,9 +557,12 @@ private void CorrectImplements(ProprietableBlockDeclaration block)
private static void CorrectMethodType(CodeMethod currentMethod)
{
var parentClass = currentMethod.Parent as CodeClass;
if (currentMethod.IsOfKind(CodeMethodKind.RequestGenerator))
if (currentMethod.IsOfKind(CodeMethodKind.RequestGenerator, CodeMethodKind.RequestExecutor))
{
currentMethod.ReturnType.IsNullable = true;
if (currentMethod.IsOfKind(CodeMethodKind.RequestGenerator))
currentMethod.ReturnType.IsNullable = true;
if (currentMethod.Parameters.OfKind(CodeParameterKind.RequestBody) is CodeParameter bodyParam && bodyParam.Type.Name.Equals(MultipartBodyClassName, StringComparison.OrdinalIgnoreCase))
bodyParam.Type.IsNullable = false;
}
else if (currentMethod.IsOfKind(CodeMethodKind.Serializer))
currentMethod.Parameters.Where(static x => x.Type.Name.Equals("ISerializationWriter", StringComparison.Ordinal)).ToList().ForEach(x => x.Type.Name = "SerializationWriter");
Expand Down Expand Up @@ -591,7 +599,7 @@ private static void CorrectMethodType(CodeMethod currentMethod)
currentMethod.ReturnType = new CodeType { Name = "Parsable", IsNullable = false, IsExternal = true };
}
CorrectCoreTypes(parentClass, DateTypesReplacements, currentMethod.Parameters
.Select(x => x.Type)
.Select(static x => x.Type)
.Union(new[] { currentMethod.ReturnType })
.ToArray());
}
Expand Down
3 changes: 2 additions & 1 deletion src/Kiota.Builder/Refiners/GoReservedNamesProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public class GoReservedNamesProvider : IReservedNamesProvider
"type",
"var",
"vendor", // cannot be used as a package name
"BaseRequestBuilder"
"BaseRequestBuilder",
"MultipartBody",
});
public HashSet<string> ReservedNames => _reservedNames.Value;
}
2 changes: 1 addition & 1 deletion src/Kiota.Builder/Writers/Go/CodeMethodWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -807,7 +807,7 @@ private void WriteRequestGeneratorBody(CodeMethod codeElement, RequestParams req
var collectionSuffix = requestParams.requestBody.Type.IsCollection ? "Collection" : string.Empty;
if (requestParams.requestBody.Type.Name.Equals("binary", StringComparison.OrdinalIgnoreCase))
writer.WriteLine($"{RequestInfoVarName}.SetStreamContent({bodyParamReference})");
else if (requestParams.requestBody.Type is CodeType bodyType && (bodyType.TypeDefinition is CodeClass || bodyType.TypeDefinition is CodeInterface))
else if (requestParams.requestBody.Type is CodeType bodyType && (bodyType.TypeDefinition is CodeClass || bodyType.TypeDefinition is CodeInterface || bodyType.Name.Equals("MultipartBody", StringComparison.OrdinalIgnoreCase)))
{
if (bodyType.IsCollection)
{
Expand Down
20 changes: 14 additions & 6 deletions tests/Kiota.Builder.Tests/Writers/Go/CodeMethodWriterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -617,12 +617,6 @@ private void AddRequestBodyParameters(CodeMethod target = default, bool useCompl
}).First(),
} : stringType,
});
target.AddParameter(new CodeParameter
{
Name = "r",
Kind = CodeParameterKind.ResponseHandler,
Type = stringType,
});
}
[Fact]
public void WritesNullableVoidTypeForExecutor()
Expand Down Expand Up @@ -663,6 +657,20 @@ public void WritesRequestBodiesThrowOnNullHttpMethod()
Assert.Throws<InvalidOperationException>(() => writer.Write(method));
}
[Fact]
public void WritesRequestGeneratorBodyForMultipart()
{
setup();
method.Kind = CodeMethodKind.RequestGenerator;
method.HttpMethod = HttpMethod.Post;
AddRequestProperties();
AddRequestBodyParameters();
method.Parameters.First(static x => x.IsOfKind(CodeParameterKind.RequestBody)).Type = new CodeType { Name = "MultipartBody", IsExternal = true };
writer.Write(method);
var result = tw.ToString();
Assert.Contains("SetContentFromParsable", result);
AssertExtensions.CurlyBracesAreClosed(result);
}
[Fact]
public void WritesRequestExecutorBody()
{
setup();
Expand Down

0 comments on commit 806e329

Please sign in to comment.