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

Conversation

kfcampbell
Copy link
Member

@kfcampbell kfcampbell commented Aug 26, 2024

Fixes #5208.

If a response contains application/octet-stream as a response type, the Kiota-generated response type should be a binary one.

An concrete example of when this may happen: an API is migrating from hosting their own assets to returning redirects to a blob storage service, and they may return either a 302 Found and redirect to a URL that contains the asset, or the binary asset directly. For a standard consumer with redirects enabled, the end result is the same. For Kiota, generation needs to know to return binary content types. Currently Kiota will not do so and instead return void types.

@kfcampbell
Copy link
Member Author

I'm struggling a bit to test this change. I initially thought that adding a test case to the existing function @baywet graciously linked me to in this thread would be sufficient:

public void GeneratesTheRightReturnTypeBasedOnContentAndStatus(string contentType, string statusCode, bool addModel, string acceptedContentType, string returnType)

I've experimented with the case of [InlineData("application/octet-stream", "302", false, "application/octet-stream", "binary")] with both variants of addModel (the boolean parameter), and continue getting void results:

[xUnit.net 00:00:00.93]     Kiota.Builder.Tests.ContentTypeMappingTests.GeneratesTheRightReturnTypeBasedOnContentAndStatusWithNullModelType(contentType: "application/octet-stream", statusCode: "302", addModel: False, acceptedContentType: "application/octet-stream", returnType: "binary") [FAIL]
  Failed Kiota.Builder.Tests.ContentTypeMappingTests.GeneratesTheRightReturnTypeBasedOnContentAndStatusWithNullModelType(contentType: "application/octet-stream", statusCode: "302", addModel: False, acceptedContentType: "application/octet-stream", returnType: "binary") [270 ms]
  Error Message:
   Assert.Equal() Failure: Strings differ
           ↓ (pos 0)
Expected: "binary"
Actual:   "void"
           ↑ (pos 0)
  Stack Trace:
     at Kiota.Builder.Tests.ContentTypeMappingTests.GeneratesTheRightReturnTypeBasedOnContentAndStatusWithNullModelType(String contentType, String statusCode, Boolean addModel, String acceptedContentType, String returnType) in /home/kfcampbell/github/dev/kiota/tests/Kiota.Builder.Tests/ContentTypeMappingTests.cs:line 230
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
   at System.Reflection.MethodBaseInvoker.InvokeWithManyArgs(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)

Upon debugging, I never even hit the else if (modelType is null) statement that triggers the relevant logic.

@baywet baywet marked this pull request as ready for review August 26, 2024 18:32
@baywet baywet requested a review from a team as a code owner August 26, 2024 18:32
Copy link
Member

@baywet baywet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution!
You were almost there, there were in fact two blocks to update. I took the opportunity to refactor the duplication.

@baywet baywet enabled auto-merge August 26, 2024 18:32
@baywet baywet merged commit 76d34e3 into microsoft:main Aug 26, 2024
206 of 207 checks passed
@kfcampbell kfcampbell deleted the explicit-octet-stream-generates-binary branch September 3, 2024 16:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Requests that redirect to download resources are not performed
2 participants