fix: generate binary response when errors include plain text#7284
Merged
gavinbarron merged 3 commits intomainfrom Jan 13, 2026
Merged
fix: generate binary response when errors include plain text#7284gavinbarron merged 3 commits intomainfrom
gavinbarron merged 3 commits intomainfrom
Conversation
baywet
requested changes
Jan 13, 2026
Member
baywet
left a comment
There was a problem hiding this comment.
Can you add an entry in the changelog please?
Contributor
Author
|
Thanks for the catch, we should automate that at some point. |
baywet
approved these changes
Jan 13, 2026
Member
baywet
left a comment
There was a problem hiding this comment.
Thank you for making the changes!
Member
|
The reason why it wasn't automated to begin with is because of the preview releases that go out each week. We'd either need some complex branches management and sync process. Or something that commits dummy trigger commits. Since the project is much less active, maybe weekly previews is not as much important anymore. And deferring to another branch for previews + sync on demand would be enough. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug Fix for GitHub Issue #3855: Binary Format Support
The Actual Bug
When an API endpoint returns binary data (e.g.,
application/octet-streamwithformat: binary) in the success response (200) AND has error responses (4xx, 5xx) withtext/plaincontent type, Kiota incorrectly generates astringreturn type instead ofbinary(which becomesStreamin C#).Reproduction Test Case
Expected: Return type should be
binary(→Streamin C#)Actual (before fix): Return type was
stringRoot Cause
The bug is in
GetExecutorMethodDefaultReturnTypemethod in KiotaBuilder.cs:1365-1377.The Problem Flow
GetResponseSchemais called to get the schema for success responses (200, 201, etc.)StructuredMimeTypes(default:application/json,text/plain, etc.)application/octet-streamis NOT in the default structured MIME types list, it gets filtered outGetResponseSchemareturnsnullbecause no schema matched the structured MIME typesGetExecutorMethodDefaultReturnTypeGetExecutorMethodDefaultReturnTypewas checking if ANY response (including error responses like 500) hadtext/plaincontenttext/plain, it returned"string"instead of"binary"The Buggy Code (Before Fix)
The Fix
The fix ensures that
text/plainis only checked in success responses (200, 201, 202, 203, 206, 2XX), not error responses (4xx, 5xx).Fixed Code
Key Changes
&& OpenApiOperationExtensions.SuccessCodes.Contains(x.Key)(x.Value.Content?.ContainsKey(RequestBodyPlainTextContentType) ?? false)This ensures that only success responses (200, 201, 202, 203, 206, 2XX) are checked for
text/plaincontent type, and error responses (4xx, 5xx) are ignored.Test Coverage
Added comprehensive test case:
GeneratesBinaryReturnTypeWhenSuccessResponseIsBinaryAndErrorResponseIsPlainTextOrJsonAsyncThis test verifies that when:
application/octet-streamwithtype: string, format: binaryapplication/jsonandtext/plaincontent typesThe generated return type is correctly
binary(notstring).Test Results
Impact
This fix ensures that:
Streamin C# (or equivalent in other languages)text/plaindon't interfere with binary success responsesFiles Changed
GetExecutorMethodDefaultReturnTypeto only check success responses fortext/plainGeneratesBinaryReturnTypeWhenSuccessResponseIsBinaryAndErrorResponseIsPlainTextOrJsonAsyncRelated Constants
SuccessCodes:{ "200", "201", "202", "203", "206", "2XX" }(defined in OpenApiOperationExtensions.cs:11)errorStatusCodes:400-599(defined in KiotaBuilder.cs:1266)StructuredMimeTypes:application/json,text/plain,application/x-www-form-urlencoded,multipart/form-data(defined in GenerationConfiguration.cs:119-124)Example Usage
After this fix, the following OpenAPI spec correctly generates a binary return type:
Generated C# method signature:
This resolves the issue reported in GitHub #3855.