-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
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
Draft: probe content type of files using different strategy (i.e. inspect first bytes) #18949
base: master
Are you sure you want to change the base?
Draft: probe content type of files using different strategy (i.e. inspect first bytes) #18949
Conversation
@@ -1643,21 +1643,25 @@ public class ApiClient { | |||
* @return The guessed Content-Type | |||
*/ | |||
public String guessContentTypeFromFile(File file) { | |||
String contentType = URLConnection.guessContentTypeFromName(file.getName()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks for the PR
can you please add some tests in https://github.com/OpenAPITools/openapi-generator/blob/5a18e9897bd8b2b4a5013a41bccaf93710dd3883/samples/client/petstore/java/okhttp-gson/src/test/java/org/openapitools/client/ApiClientTest.java to test guessContentTypeFromFile
with different file types to confirm the enhancement is good?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added them, but it seems that the default implementation for FileTypeDetector relies on file extension (commented test).
I'm not very knowledgeable on java and the description on how to change the implementation, so I'd need some help from someone who is in order to change the implementation to accomodate the commented test, but I'd say that, as long as we use this API, users can at least do it.
current implementations are relying on file name/extension, which is prone to errors (.mp4, is it a video or a picture?) and spoofing. This change allows customers to use or opt into mechanisms which inspect the first bytes of a file's payload
5a18e98
to
5820375
Compare
https://github.com/OpenAPITools/openapi-generator/actions/runs/9585429177/job/26431446540?pr=18949 please update the samples when you've time |
String contentType = URLConnection.guessContentTypeFromName(file.getName()); | ||
if (contentType == null) { | ||
try { | ||
String contentType = Files.probeContentType(file.toPath()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shall we use URLConnection.guessContentTypeFromStream
instead as suggested in https://stackoverflow.com/a/19712111/677735?
current implementations are relying on file name/extension, which is prone to errors (.mp4, is it a video or a picture?) and spoofing. This change allows customers to use or opt into mechanisms which inspect the first bytes of a file's payload.
I'd like to do the same change for other http clients we use, but I'd like to feedback to validate the viability of this approach first.
PR checklist
Commit all changed files.
This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
These must match the expectations made by your contribution.
You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example
./bin/generate-samples.sh bin/configs/java*
.IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
master
(upcoming 7.6.0 minor release - breaking changes with fallbacks),8.0.x
(breaking changes without fallbacks)