Skip to content

Commit

Permalink
- adds support for ambiguous content type in CSharp
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Biret <[email protected]>
  • Loading branch information
baywet committed Oct 10, 2023
1 parent f5a1d7c commit a127d18
Show file tree
Hide file tree
Showing 12 changed files with 92 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ protected virtual int GetKindOrderHint(CodeParameterKind kind)
CodeParameterKind.Path => 4,
CodeParameterKind.RequestConfiguration => 5,
CodeParameterKind.RequestBody => 6,
CodeParameterKind.Serializer => 7,
CodeParameterKind.BackingStore => 8,
CodeParameterKind.SetterValue => 9,
CodeParameterKind.ParseNode => 10,
CodeParameterKind.Custom => 11,
_ => 12,
CodeParameterKind.RequestBodyContentType => 7,
CodeParameterKind.Serializer => 8,
CodeParameterKind.BackingStore => 9,
CodeParameterKind.SetterValue => 10,
CodeParameterKind.ParseNode => 11,
CodeParameterKind.Custom => 12,
_ => 13,
};
}
private const int OptionalWeight = 10000;
Expand Down
14 changes: 10 additions & 4 deletions src/Kiota.Builder/Writers/CSharp/CodeMethodWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ protected virtual void HandleMethodKind(CodeMethod codeElement, LanguageWriter w
var returnTypeWithoutCollectionInformation = conventions.GetTypeString(codeElement.ReturnType, codeElement, false);
var requestBodyParam = codeElement.Parameters.OfKind(CodeParameterKind.RequestBody);
var requestConfig = codeElement.Parameters.OfKind(CodeParameterKind.RequestConfiguration);
var requestParams = new RequestParams(requestBodyParam, requestConfig);
var requestContentType = codeElement.Parameters.OfKind(CodeParameterKind.RequestBodyContentType);
var requestParams = new RequestParams(requestBodyParam, requestConfig, requestContentType);
switch (codeElement.Kind)
{
case CodeMethodKind.Serializer:
Expand Down Expand Up @@ -90,7 +91,7 @@ protected virtual void HandleMethodKind(CodeMethod codeElement, LanguageWriter w
case CodeMethodKind.CommandBuilder:
var origParams = codeElement.OriginalMethod?.Parameters ?? codeElement.Parameters;
requestBodyParam = origParams.OfKind(CodeParameterKind.RequestBody);
requestParams = new RequestParams(requestBodyParam, null);
requestParams = new RequestParams(requestBodyParam, null, null);
WriteCommandBuilderBody(codeElement, parentClass, requestParams, isVoid, returnType, writer);
break;
case CodeMethodKind.Factory:
Expand Down Expand Up @@ -376,7 +377,7 @@ protected void WriteRequestExecutorBody(CodeMethod codeElement, RequestParams re
.Methods
.FirstOrDefault(x => x.IsOfKind(CodeMethodKind.RequestGenerator) && x.HttpMethod == codeElement.HttpMethod)
?.Name;
var parametersList = new CodeParameter?[] { requestParams.requestBody, requestParams.requestConfiguration }
var parametersList = new CodeParameter?[] { requestParams.requestBody, requestParams.requestContentType, requestParams.requestConfiguration }
.Select(static x => x?.Name).Where(static x => x != null).Aggregate(static (x, y) => $"{x}, {y}");
writer.WriteLine($"var requestInfo = {generatorMethodName}({parametersList});");
var errorMappingVarName = "default";
Expand Down Expand Up @@ -425,7 +426,12 @@ private void WriteRequestGeneratorBody(CodeMethod codeElement, RequestParams req
{
var suffix = requestParams.requestBody.Type.IsCollection ? "Collection" : string.Empty;
if (requestParams.requestBody.Type.Name.Equals(conventions.StreamTypeName, StringComparison.OrdinalIgnoreCase))
writer.WriteLine($"{RequestInfoVarName}.SetStreamContent({requestParams.requestBody.Name});");
{
if (requestParams.requestContentType is not null)
writer.WriteLine($"{RequestInfoVarName}.SetStreamContent({requestParams.requestBody.Name}, {requestParams.requestContentType.Name});");
else if (!string.IsNullOrEmpty(codeElement.RequestBodyContentType))
writer.WriteLine($"{RequestInfoVarName}.SetStreamContent({requestParams.requestBody.Name}, \"{codeElement.RequestBodyContentType}\");");
}
else if (currentClass.GetPropertyOfKind(CodePropertyKind.RequestAdapter) is CodeProperty requestAdapterProperty)
if (requestParams.requestBody.Type is CodeType bodyType && (bodyType.TypeDefinition is CodeClass || bodyType.Name.Equals("MultipartBody", StringComparison.OrdinalIgnoreCase)))
writer.WriteLine($"{RequestInfoVarName}.SetContentFromParsable({requestAdapterProperty.Name.ToFirstCharacterUpperCase()}, \"{codeElement.RequestBodyContentType}\", {requestParams.requestBody.Name});");
Expand Down
3 changes: 2 additions & 1 deletion src/Kiota.Builder/Writers/Go/CodeMethodWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ public override void WriteCodeElement(CodeMethod codeElement, LanguageWriter wri
writer.IncreaseIndent();
var requestOptionsParam = codeElement.Parameters.OfKind(CodeParameterKind.RequestConfiguration);
var requestBodyParam = codeElement.Parameters.OfKind(CodeParameterKind.RequestBody);
var requestParams = new RequestParams(requestBodyParam, requestOptionsParam);
var requestContentType = codeElement.Parameters.OfKind(CodeParameterKind.RequestBodyContentType);
var requestParams = new RequestParams(requestBodyParam, requestOptionsParam, requestContentType);
switch (codeElement.Kind)
{
case CodeMethodKind.Serializer:
Expand Down
11 changes: 6 additions & 5 deletions src/Kiota.Builder/Writers/Go/GoCodeParameterOrderComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ protected override int GetKindOrderHint(CodeParameterKind kind)
CodeParameterKind.Path => 4,
CodeParameterKind.RequestConfiguration => 5,
CodeParameterKind.RequestBody => 6,
CodeParameterKind.Serializer => 7,
CodeParameterKind.BackingStore => 8,
CodeParameterKind.SetterValue => 9,
CodeParameterKind.ParseNode => 10,
CodeParameterKind.Custom => 11,
CodeParameterKind.RequestBodyContentType => 7,
CodeParameterKind.Serializer => 8,
CodeParameterKind.BackingStore => 9,
CodeParameterKind.SetterValue => 10,
CodeParameterKind.ParseNode => 11,
CodeParameterKind.Custom => 12,
_ => 13,
};
}
Expand Down
3 changes: 2 additions & 1 deletion src/Kiota.Builder/Writers/Java/CodeMethodWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ public override void WriteCodeElement(CodeMethod codeElement, LanguageWriter wri
var inherits = parentClass.StartBlock.Inherits != null && !parentClass.IsErrorDefinition;
var requestBodyParam = codeElement.Parameters.OfKind(CodeParameterKind.RequestBody);
var configParam = codeElement.Parameters.OfKind(CodeParameterKind.RequestConfiguration);
var requestParams = new RequestParams(requestBodyParam, configParam);
var requestContentType = codeElement.Parameters.OfKind(CodeParameterKind.RequestBodyContentType);
var requestParams = new RequestParams(requestBodyParam, configParam, requestContentType);
AddNullChecks(codeElement, writer);
switch (codeElement.Kind)
{
Expand Down
3 changes: 2 additions & 1 deletion src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ public override void WriteCodeElement(CodeMethod codeElement, LanguageWriter wri
codeClass.IsOfKind(CodeClassKind.Model);
var requestBodyParam = codeElement.Parameters.OfKind(CodeParameterKind.RequestBody);
var config = codeElement.Parameters.OfKind(CodeParameterKind.RequestConfiguration);
var requestParams = new RequestParams(requestBodyParam, config);
var requestContentType = codeElement.Parameters.OfKind(CodeParameterKind.RequestBodyContentType);
var requestParams = new RequestParams(requestBodyParam, config, requestContentType);

WriteMethodPhpDocs(codeElement, writer);
WriteMethodsAndParameters(codeElement, writer, codeElement.IsOfKind(CodeMethodKind.Constructor, CodeMethodKind.ClientConstructor));
Expand Down
3 changes: 2 additions & 1 deletion src/Kiota.Builder/Writers/Python/CodeMethodWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ public override void WriteCodeElement(CodeMethod codeElement, LanguageWriter wri
var inherits = parentClass.StartBlock.Inherits != null && !parentClass.IsErrorDefinition;
var requestBodyParam = codeElement.Parameters.OfKind(CodeParameterKind.RequestBody);
var requestConfigParam = codeElement.Parameters.OfKind(CodeParameterKind.RequestConfiguration);
var requestParams = new RequestParams(requestBodyParam, requestConfigParam);
var requestContentType = codeElement.Parameters.OfKind(CodeParameterKind.RequestBodyContentType);
var requestParams = new RequestParams(requestBodyParam, requestConfigParam, requestContentType);
if (!codeElement.IsOfKind(CodeMethodKind.Setter) &&
!(codeElement.IsOfKind(CodeMethodKind.Constructor) && parentClass.IsOfKind(CodeClassKind.RequestBuilder)))
foreach (var parameter in codeElement.Parameters.Where(static x => !x.Optional).OrderBy(static x => x.Name))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ protected override int GetKindOrderHint(CodeParameterKind kind)
CodeParameterKind.Path => 4,
CodeParameterKind.RequestConfiguration => 5,
CodeParameterKind.RequestBody => 6,
CodeParameterKind.Serializer => 7,
CodeParameterKind.BackingStore => 8,
CodeParameterKind.SetterValue => 9,
CodeParameterKind.ParseNode => 10,
CodeParameterKind.Custom => 11,
CodeParameterKind.RequestBodyContentType => 7,
CodeParameterKind.Serializer => 8,
CodeParameterKind.BackingStore => 9,
CodeParameterKind.SetterValue => 10,
CodeParameterKind.ParseNode => 11,
CodeParameterKind.Custom => 12,
_ => 13,
};
}
Expand Down
2 changes: 1 addition & 1 deletion src/Kiota.Builder/Writers/RequestParams.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Kiota.Builder.CodeDOM;

namespace Kiota.Builder.Writers;
public record RequestParams(CodeParameter? requestBody, CodeParameter? requestConfiguration)
public record RequestParams(CodeParameter? requestBody, CodeParameter? requestConfiguration, CodeParameter? requestContentType)
{
public CodeProperty? Headers => requestConfiguration?.GetHeadersProperty();
public CodeProperty? QueryParameters => requestConfiguration?.GetQueryProperty();
Expand Down
3 changes: 2 additions & 1 deletion src/Kiota.Builder/Writers/Ruby/CodeMethodWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ public override void WriteCodeElement(CodeMethod codeElement, LanguageWriter wri
var inherits = parentClass.StartBlock.Inherits != null;
var requestBodyParam = codeElement.Parameters.OfKind(CodeParameterKind.RequestBody);
var config = codeElement.Parameters.OfKind(CodeParameterKind.RequestConfiguration);
var requestParams = new RequestParams(requestBodyParam, config);
var requestContentType = codeElement.Parameters.OfKind(CodeParameterKind.RequestBodyContentType);
var requestParams = new RequestParams(requestBodyParam, config, requestContentType);
WriteMethodPrototype(codeElement, writer);
AddNullChecks(codeElement, writer);
switch (codeElement.Kind)
Expand Down
3 changes: 2 additions & 1 deletion src/Kiota.Builder/Writers/TypeScript/CodeMethodWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public override void WriteCodeElement(CodeMethod codeElement, LanguageWriter wri
var inherits = parentClass.StartBlock.Inherits != null && !parentClass.IsErrorDefinition;
var requestBodyParam = codeElement.Parameters.OfKind(CodeParameterKind.RequestBody);
var requestConfigParam = codeElement.Parameters.OfKind(CodeParameterKind.RequestConfiguration);
var requestParams = new RequestParams(requestBodyParam, requestConfigParam);
var requestContentType = codeElement.Parameters.OfKind(CodeParameterKind.RequestBodyContentType);
var requestParams = new RequestParams(requestBodyParam, requestConfigParam, requestContentType);
WriteDefensiveStatements(codeElement, writer);
switch (codeElement.Kind)
{
Expand Down
50 changes: 50 additions & 0 deletions tests/Kiota.Builder.Tests/Writers/CSharp/CodeMethodWriterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1021,6 +1021,56 @@ public void WritesRequestGeneratorBodyForScalarCollection()
AssertExtensions.CurlyBracesAreClosed(result, 1);
}
[Fact]
public void WritesRequestGeneratorBodyKnownRequestBodyType()
{
setup();
method.Kind = CodeMethodKind.RequestGenerator;
method.HttpMethod = HttpMethod.Post;
AddRequestProperties();
AddRequestBodyParameters(false);
method.Parameters.OfKind(CodeParameterKind.RequestBody).Type = new CodeType
{
Name = new CSharpConventionService().StreamTypeName,
IsExternal = true,
};
method.RequestBodyContentType = "application/json";
writer.Write(method);
var result = tw.ToString();
Assert.Contains("SetStreamContent", result, StringComparison.OrdinalIgnoreCase);
Assert.Contains("application/json", result, StringComparison.OrdinalIgnoreCase);
AssertExtensions.CurlyBracesAreClosed(result, 1);
}
[Fact]
public void WritesRequestGeneratorBodyUnknownRequestBodyType()
{
setup();
method.Kind = CodeMethodKind.RequestGenerator;
method.HttpMethod = HttpMethod.Post;
AddRequestProperties();
AddRequestBodyParameters(false);
method.Parameters.OfKind(CodeParameterKind.RequestBody).Type = new CodeType
{
Name = new CSharpConventionService().StreamTypeName,
IsExternal = true,
};
method.AddParameter(new CodeParameter
{
Name = "requestContentType",
Type = new CodeType()
{
Name = "string",
IsExternal = true,
},
Kind = CodeParameterKind.RequestBodyContentType,
});
writer.Write(method);
var result = tw.ToString();
Assert.Contains("SetStreamContent", result, StringComparison.OrdinalIgnoreCase);
Assert.DoesNotContain("application/json", result, StringComparison.OrdinalIgnoreCase);
Assert.Contains(", requestContentType", result, StringComparison.OrdinalIgnoreCase);
AssertExtensions.CurlyBracesAreClosed(result, 1);
}
[Fact]
public void WritesInheritedDeSerializerBody()
{
setup(true);
Expand Down

0 comments on commit a127d18

Please sign in to comment.