Skip to content

Commit ab04e27

Browse files
YunKuiLusobychacko
authored andcommitted
Fix infinite recursion in getMimeType(Path) method
Fixes: #3576 Auto-cherry-pick to 1.0.x Signed-off-by: YunKui Lu <luyunkui95@gmail.com> Add missing copyrights Signed-off-by: Soby Chacko <soby.chacko@broadcom.com>
1 parent 12c725d commit ab04e27

File tree

2 files changed

+95
-2
lines changed

2 files changed

+95
-2
lines changed

models/spring-ai-vertex-ai-gemini/src/main/java/org/springframework/ai/vertexai/gemini/MimeTypeDetector.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ public abstract class MimeTypeDetector {
5555
/**
5656
* List of all MIME types supported by the Vertex Gemini API.
5757
*/
58-
private static final Map<String, MimeType> GEMINI_MIME_TYPES = new HashMap<>();
58+
// exposed for testing purposes
59+
static final Map<String, MimeType> GEMINI_MIME_TYPES = new HashMap<>();
5960

6061
public static MimeType getMimeType(URL url) {
6162
return getMimeType(url.getFile());
@@ -70,7 +71,7 @@ public static MimeType getMimeType(File file) {
7071
}
7172

7273
public static MimeType getMimeType(Path path) {
73-
return getMimeType(path.getFileName());
74+
return getMimeType(path.toUri());
7475
}
7576

7677
public static MimeType getMimeType(Resource resource) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Copyright 2025-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.ai.vertexai.gemini;
18+
19+
import java.io.File;
20+
import java.net.MalformedURLException;
21+
import java.net.URI;
22+
import java.nio.file.Path;
23+
import java.util.stream.Stream;
24+
25+
import org.junit.jupiter.params.ParameterizedTest;
26+
import org.junit.jupiter.params.provider.Arguments;
27+
import org.junit.jupiter.params.provider.MethodSource;
28+
29+
import org.springframework.core.io.PathResource;
30+
import org.springframework.util.MimeType;
31+
32+
import static org.assertj.core.api.Assertions.assertThat;
33+
import static org.springframework.ai.vertexai.gemini.MimeTypeDetector.GEMINI_MIME_TYPES;
34+
35+
/**
36+
* @author YunKui Lu
37+
*/
38+
class MimeTypeDetectorTests {
39+
40+
private static Stream<Arguments> provideMimeTypes() {
41+
return GEMINI_MIME_TYPES.entrySet().stream().map(entry -> Arguments.of(entry.getKey(), entry.getValue()));
42+
}
43+
44+
@ParameterizedTest
45+
@MethodSource("provideMimeTypes")
46+
void getMimeTypeByURLPath(String extension, MimeType expectedMimeType) throws MalformedURLException {
47+
String path = "https://testhost/test." + extension;
48+
MimeType mimeType = MimeTypeDetector.getMimeType(URI.create(path).toURL());
49+
assertThat(mimeType).isEqualTo(expectedMimeType);
50+
}
51+
52+
@ParameterizedTest
53+
@MethodSource("provideMimeTypes")
54+
void getMimeTypeByURI(String extension, MimeType expectedMimeType) {
55+
String path = "https://testhost/test." + extension;
56+
MimeType mimeType = MimeTypeDetector.getMimeType(URI.create(path));
57+
assertThat(mimeType).isEqualTo(expectedMimeType);
58+
}
59+
60+
@ParameterizedTest
61+
@MethodSource("provideMimeTypes")
62+
void getMimeTypeByFile(String extension, MimeType expectedMimeType) {
63+
String path = "test." + extension;
64+
MimeType mimeType = MimeTypeDetector.getMimeType(new File(path));
65+
assertThat(mimeType).isEqualTo(expectedMimeType);
66+
}
67+
68+
@ParameterizedTest
69+
@MethodSource("provideMimeTypes")
70+
void getMimeTypeByPath(String extension, MimeType expectedMimeType) {
71+
String path = "test." + extension;
72+
MimeType mimeType = MimeTypeDetector.getMimeType(Path.of(path));
73+
assertThat(mimeType).isEqualTo(expectedMimeType);
74+
}
75+
76+
@ParameterizedTest
77+
@MethodSource("provideMimeTypes")
78+
void getMimeTypeByResource(String extension, MimeType expectedMimeType) {
79+
String path = "test." + extension;
80+
MimeType mimeType = MimeTypeDetector.getMimeType(new PathResource(path));
81+
assertThat(mimeType).isEqualTo(expectedMimeType);
82+
}
83+
84+
@ParameterizedTest
85+
@MethodSource("provideMimeTypes")
86+
void getMimeTypeByString(String extension, MimeType expectedMimeType) {
87+
String path = "test." + extension;
88+
MimeType mimeType = MimeTypeDetector.getMimeType(path);
89+
assertThat(mimeType).isEqualTo(expectedMimeType);
90+
}
91+
92+
}

0 commit comments

Comments
 (0)