Skip to content

Commit

Permalink
feat: adds binary return type for redirect responses
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Biret <[email protected]>
  • Loading branch information
baywet committed Aug 26, 2024
1 parent 7388241 commit 435d902
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 19 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fixed shorthand for refresh option in workspace experience. [#5240](https://github.com/microsoft/kiota/issues/5240)
- Fixed missing type options in help for plugin commands. [#5230](https://github.com/microsoft/kiota/issues/5230)
- Removed OpenAI plugins generation since the service does not support them anymore.
- Redirect status codes documenting an application/octet-stream content type now generate a stream return type. [#5246](https://github.com/microsoft/kiota/issues/5246)
- Fixed an issue where models would be missing when they had no properties and a single allOf entry. [#5014](https://github.com/microsoft/kiota/issues/5014)
- Reverts modification of responses in output openApi file when generating plugins [#4945](https://github.com/microsoft/kiota/issues/4945)
- Expand properties types with null type for Typescript. [#4993](https://github.com/microsoft/kiota-typescript/issues/1188)
Expand Down
36 changes: 17 additions & 19 deletions src/Kiota.Builder/KiotaBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1160,7 +1160,8 @@ openApiExtension is OpenApiPrimaryErrorMessageExtension primaryErrorMessageExten
}
private const string RequestBodyPlainTextContentType = "text/plain";
private const string RequestBodyOctetStreamContentType = "application/octet-stream";
private static readonly HashSet<string> noContentStatusCodes = new(StringComparer.OrdinalIgnoreCase) { "201", "202", "204", "205", "301", "302", "303", "304", "307" };
private static readonly HashSet<string> redirectStatusCodes = new(StringComparer.OrdinalIgnoreCase) { "301", "302", "303", "307" };
private static readonly HashSet<string> noContentStatusCodes = new(redirectStatusCodes, StringComparer.OrdinalIgnoreCase) { "201", "202", "204", "205", "304" };
private static readonly HashSet<string> errorStatusCodes = new(Enumerable.Range(400, 599).Select(static x => x.ToString(CultureInfo.InvariantCulture))
.Concat([CodeMethod.ErrorMappingClientRange, CodeMethod.ErrorMappingServerRange]), StringComparer.OrdinalIgnoreCase);
private void AddErrorMappingsForExecutorMethod(OpenApiUrlTreeNode currentNode, OpenApiOperation operation, CodeMethod executorMethod)
Expand Down Expand Up @@ -1249,31 +1250,28 @@ private void AddErrorMappingToExecutorMethod(OpenApiUrlTreeNode currentNode, Ope
}
else if (modelType is null)
{
string returnType;
if (operation.Responses.Any(static x => x.Value.Content.ContainsKey(RequestBodyOctetStreamContentType)))
returnType = "binary";
else if (operation.Responses.Any(static x => noContentStatusCodes.Contains(x.Key)))
returnType = VoidType;
else if (operation.Responses.Any(static x => x.Value.Content.ContainsKey(RequestBodyPlainTextContentType)))
returnType = "string";
else
returnType = "binary";
return (new CodeType { Name = returnType, IsExternal = true, }, null);
return (GetExecutorMethodDefaultReturnType(operation), null);
}
return (modelType, null);
}
else
{
string returnType;
if (operation.Responses.Any(static x => noContentStatusCodes.Contains(x.Key)))
returnType = VoidType;
else if (operation.Responses.Any(static x => x.Value.Content.ContainsKey(RequestBodyPlainTextContentType)))
returnType = "string";
else
returnType = "binary";
return (new CodeType { Name = returnType, IsExternal = true, }, null);
return (GetExecutorMethodDefaultReturnType(operation), null);
}
}
private static CodeType GetExecutorMethodDefaultReturnType(OpenApiOperation operation)
{
string returnType;
if (operation.Responses.Any(static x => x.Value.Content.ContainsKey(RequestBodyOctetStreamContentType) && redirectStatusCodes.Contains(x.Key)))
returnType = "binary";
else if (operation.Responses.Any(static x => noContentStatusCodes.Contains(x.Key)))
returnType = VoidType;
else if (operation.Responses.Any(static x => x.Value.Content.ContainsKey(RequestBodyPlainTextContentType)))
returnType = "string";
else
returnType = "binary";
return new CodeType { Name = returnType, IsExternal = true, };
}
private void CreateOperationMethods(OpenApiUrlTreeNode currentNode, OperationType operationType, OpenApiOperation operation, CodeClass parentClass)
{
try
Expand Down
1 change: 1 addition & 0 deletions tests/Kiota.Builder.Tests/ContentTypeMappingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public void Dispose()
[InlineData("application/octet-stream", "204", false, "default", "void")]
[InlineData("application/octet-stream", "200", true, "default", "binary")]
[InlineData("application/octet-stream", "200", false, "default", "binary")]
[InlineData("application/octet-stream", "302", false, "default", "binary")] // on a redirect to a binary content we generate a binary return type for download
[InlineData("text/html", "204", true, "default", "void")]
[InlineData("text/html", "204", false, "default", "void")]
[InlineData("text/html", "200", true, "default", "binary")]
Expand Down

0 comments on commit 435d902

Please sign in to comment.