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

If any response contains octet-stream, make response binary #5246

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
35 changes: 18 additions & 17 deletions src/Kiota.Builder/KiotaBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1159,7 +1159,9 @@ openApiExtension is OpenApiPrimaryErrorMessageExtension primaryErrorMessageExten
};
}
private const string RequestBodyPlainTextContentType = "text/plain";
private static readonly HashSet<string> noContentStatusCodes = new(StringComparer.OrdinalIgnoreCase) { "201", "202", "204", "205", "301", "302", "303", "304", "307" };
private const string RequestBodyOctetStreamContentType = "application/octet-stream";
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 @@ -1248,29 +1250,28 @@ private void AddErrorMappingToExecutorMethod(OpenApiUrlTreeNode currentNode, Ope
}
else if (modelType is null)
{
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);
}
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
Loading