Skip to content

Commit 30fbec7

Browse files
committed
fix: json parse bugs
1 parent 36498f6 commit 30fbec7

File tree

7 files changed

+125
-56
lines changed

7 files changed

+125
-56
lines changed

api/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>top.bella</groupId>
88
<artifactId>openai-java</artifactId>
9-
<version>0.23.95</version>
9+
<version>0.23.96</version>
1010
</parent>
1111
<packaging>jar</packaging>
1212
<artifactId>openai-api</artifactId>

api/src/main/java/com/theokanning/openai/assistants/run/ToolChoice.java

Lines changed: 69 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -109,64 +109,91 @@ public ToolChoice deserialize(JsonParser jsonParser, DeserializationContext dese
109109
}
110110

111111
private Function parseFunction(JsonParser jsonParser) throws IOException {
112-
if (jsonParser.getCurrentToken() == JsonToken.START_OBJECT) {
113-
// 处理对象的情况
114-
Function function = new Function();
115-
while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
116-
// 判断对象内元素类型并进行相应的反序列化
117-
if (jsonParser.getCurrentName().equals("name")) {
118-
function.setName(jsonParser.nextTextValue());
119-
}
112+
JsonToken currentToken = jsonParser.getCurrentToken();
113+
114+
if (currentToken == JsonToken.VALUE_NULL) {
115+
return null;
116+
}
117+
118+
if (currentToken != JsonToken.START_OBJECT) {
119+
throw new IOException("Expected START_OBJECT for Function, but got: " + currentToken);
120+
}
121+
122+
// 处理对象的情况
123+
Function function = new Function();
124+
while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
125+
// 判断对象内元素类型并进行相应的反序列化
126+
if (jsonParser.getCurrentName().equals("name")) {
127+
function.setName(jsonParser.nextTextValue());
120128
}
121-
return function;
122129
}
123-
//抛出异常
124-
throw new IllegalArgumentException("Invalid Function");
130+
return function;
125131
}
126132

127133
private AllowedTools parseAllowedTools(JsonParser jsonParser) throws IOException {
128-
if (jsonParser.getCurrentToken() == JsonToken.START_OBJECT) {
129-
AllowedTools allowedTools = new AllowedTools();
130-
while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
131-
String fieldName = jsonParser.getCurrentName();
132-
if ("mode".equals(fieldName)) {
133-
allowedTools.setMode(jsonParser.nextTextValue());
134-
} else if ("tools".equals(fieldName)) {
135-
jsonParser.nextToken();
136-
allowedTools.setTools(parseToolsList(jsonParser));
137-
}
134+
JsonToken currentToken = jsonParser.getCurrentToken();
135+
136+
if (currentToken == JsonToken.VALUE_NULL) {
137+
return null;
138+
}
139+
140+
if (currentToken != JsonToken.START_OBJECT) {
141+
throw new IOException("Expected START_OBJECT for AllowedTools, but got: " + currentToken);
142+
}
143+
144+
AllowedTools allowedTools = new AllowedTools();
145+
while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
146+
String fieldName = jsonParser.getCurrentName();
147+
if ("mode".equals(fieldName)) {
148+
allowedTools.setMode(jsonParser.nextTextValue());
149+
} else if ("tools".equals(fieldName)) {
150+
jsonParser.nextToken();
151+
allowedTools.setTools(parseToolsList(jsonParser));
138152
}
139-
return allowedTools;
140153
}
141-
throw new IllegalArgumentException("Invalid AllowedTools");
154+
return allowedTools;
142155
}
143156

144157
private List<Tool> parseToolsList(JsonParser jsonParser) throws IOException {
145-
if (jsonParser.getCurrentToken() == JsonToken.START_ARRAY) {
146-
List<Tool> tools = new java.util.ArrayList<>();
147-
while (jsonParser.nextToken() != JsonToken.END_ARRAY) {
148-
tools.add(parseTool(jsonParser));
149-
}
150-
return tools;
158+
JsonToken currentToken = jsonParser.getCurrentToken();
159+
160+
if (currentToken == JsonToken.VALUE_NULL) {
161+
return null;
151162
}
152-
throw new IllegalArgumentException("Invalid Tools Array");
163+
164+
if (currentToken != JsonToken.START_ARRAY) {
165+
throw new IOException("Expected START_ARRAY for Tools, but got: " + currentToken);
166+
}
167+
168+
List<Tool> tools = new java.util.ArrayList<>();
169+
while (jsonParser.nextToken() != JsonToken.END_ARRAY) {
170+
tools.add(parseTool(jsonParser));
171+
}
172+
return tools;
153173
}
154174

155175
private Tool parseTool(JsonParser jsonParser) throws IOException {
156-
if (jsonParser.getCurrentToken() == JsonToken.START_OBJECT) {
157-
Tool tool = new Tool();
158-
while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
159-
String fieldName = jsonParser.getCurrentName();
160-
if ("type".equals(fieldName)) {
161-
tool.setType(jsonParser.nextTextValue());
162-
} else if ("function".equals(fieldName)) {
163-
jsonParser.nextToken();
164-
tool.setFunction(parseFunction(jsonParser));
165-
}
176+
JsonToken currentToken = jsonParser.getCurrentToken();
177+
178+
if (currentToken == JsonToken.VALUE_NULL) {
179+
return null;
180+
}
181+
182+
if (currentToken != JsonToken.START_OBJECT) {
183+
throw new IOException("Expected START_OBJECT for Tool, but got: " + currentToken);
184+
}
185+
186+
Tool tool = new Tool();
187+
while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
188+
String fieldName = jsonParser.getCurrentName();
189+
if ("type".equals(fieldName)) {
190+
tool.setType(jsonParser.nextTextValue());
191+
} else if ("function".equals(fieldName)) {
192+
jsonParser.nextToken();
193+
tool.setFunction(parseFunction(jsonParser));
166194
}
167-
return tool;
168195
}
169-
throw new IllegalArgumentException("Invalid Tool");
196+
return tool;
170197
}
171198
}
172199

api/src/main/java/com/theokanning/openai/completion/chat/ChatResponseFormat.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ public void serialize(ChatResponseFormat value, JsonGenerator gen, SerializerPro
7878

7979
@NoArgsConstructor
8080
public static class ChatResponseFormatDeserializer extends JsonDeserializer<ChatResponseFormat> {
81+
8182
@Override
8283
public ChatResponseFormat deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
8384
if (jsonParser.getCurrentToken() == JsonToken.VALUE_STRING) {

api/src/main/java/com/theokanning/openai/completion/chat/ContentDeserializer.java

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* @date 2024年04月10 11:17
1717
**/
1818
public class ContentDeserializer extends JsonDeserializer<Object> {
19+
1920
@Override
2021
public Object deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
2122
if (jsonParser.getCurrentToken() == JsonToken.VALUE_STRING) {
@@ -39,9 +40,11 @@ public Object deserialize(JsonParser jsonParser, DeserializationContext deserial
3940

4041
MultiMediaContent parseContent(JsonParser jsonParser) throws IOException {
4142
MultiMediaContent content = new MultiMediaContent();
43+
4244
while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
4345
String fieldName = jsonParser.getCurrentName();
4446
jsonParser.nextToken();
47+
4548
if ("type".equals(fieldName)) {
4649
content.setType(jsonParser.getText());
4750
} else if ("text".equals(fieldName)) {
@@ -60,14 +63,25 @@ MultiMediaContent parseContent(JsonParser jsonParser) throws IOException {
6063
}
6164

6265
private ImageFile parseImageFile(JsonParser jsonParser) throws IOException {
63-
if(jsonParser.getCurrentToken() == JsonToken.VALUE_NULL) {
66+
JsonToken currentToken = jsonParser.getCurrentToken();
67+
68+
// 关键修复1: 检查null值
69+
if (currentToken == JsonToken.VALUE_NULL) {
6470
return null;
6571
}
72+
73+
// 关键修复2: 验证必须是对象开始
74+
if (currentToken != JsonToken.START_OBJECT) {
75+
throw new IOException("Expected START_OBJECT for image_file, but got: " + currentToken);
76+
}
77+
6678
String fileId = null;
6779
String detail = null;
80+
6881
while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
6982
String fieldName = jsonParser.getCurrentName();
7083
jsonParser.nextToken();
84+
7185
if ("file_id".equals(fieldName)) {
7286
fileId = jsonParser.getValueAsString();
7387
} else if ("detail".equals(fieldName)) {
@@ -78,14 +92,23 @@ private ImageFile parseImageFile(JsonParser jsonParser) throws IOException {
7892
}
7993

8094
private ImageUrl parseImageUrl(JsonParser jsonParser) throws IOException {
81-
String url = null;
82-
String detail = null;
83-
if(jsonParser.getCurrentToken() == JsonToken.VALUE_NULL) {
95+
JsonToken currentToken = jsonParser.getCurrentToken();
96+
97+
if (currentToken == JsonToken.VALUE_NULL) {
8498
return null;
8599
}
100+
101+
if (currentToken != JsonToken.START_OBJECT) {
102+
throw new IOException("Expected START_OBJECT for image_url, but got: " + currentToken);
103+
}
104+
105+
String url = null;
106+
String detail = null;
107+
86108
while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
87109
String fieldName = jsonParser.getCurrentName();
88110
jsonParser.nextToken();
111+
89112
if ("url".equals(fieldName)) {
90113
url = jsonParser.getValueAsString();
91114
} else if ("detail".equals(fieldName)) {
@@ -96,14 +119,23 @@ private ImageUrl parseImageUrl(JsonParser jsonParser) throws IOException {
96119
}
97120

98121
private InputAudio parseInputAudio(JsonParser jsonParser) throws IOException {
99-
String data = null;
100-
String format = null;
101-
if(jsonParser.getCurrentToken() == JsonToken.VALUE_NULL) {
122+
JsonToken currentToken = jsonParser.getCurrentToken();
123+
124+
if (currentToken == JsonToken.VALUE_NULL) {
102125
return null;
103126
}
127+
128+
if (currentToken != JsonToken.START_OBJECT) {
129+
throw new IOException("Expected START_OBJECT for input_audio, but got: " + currentToken);
130+
}
131+
132+
String data = null;
133+
String format = null;
134+
104135
while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
105136
String fieldName = jsonParser.getCurrentName();
106137
jsonParser.nextToken();
138+
107139
if ("data".equals(fieldName)) {
108140
data = jsonParser.getValueAsString();
109141
} else if ("format".equals(fieldName)) {
@@ -114,14 +146,23 @@ private InputAudio parseInputAudio(JsonParser jsonParser) throws IOException {
114146
}
115147

116148
private AudioURL parseAudioUrl(JsonParser jsonParser) throws IOException {
117-
String url = null;
118-
String audioTranscript = null;
119-
if(jsonParser.getCurrentToken() == JsonToken.VALUE_NULL) {
149+
JsonToken currentToken = jsonParser.getCurrentToken();
150+
151+
if (currentToken == JsonToken.VALUE_NULL) {
120152
return null;
121153
}
154+
155+
if (currentToken != JsonToken.START_OBJECT) {
156+
throw new IOException("Expected START_OBJECT for audio_url, but got: " + currentToken);
157+
}
158+
159+
String url = null;
160+
String audioTranscript = null;
161+
122162
while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
123163
String fieldName = jsonParser.getCurrentName();
124164
jsonParser.nextToken();
165+
125166
if ("url".equals(fieldName)) {
126167
url = jsonParser.getValueAsString();
127168
} else if ("audio_transcript".equals(fieldName)) {

client/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>top.bella</groupId>
88
<artifactId>openai-java</artifactId>
9-
<version>0.23.95</version>
9+
<version>0.23.96</version>
1010
</parent>
1111
<packaging>jar</packaging>
1212

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<groupId>top.bella</groupId>
77
<artifactId>openai-java</artifactId>
8-
<version>0.23.95</version>
8+
<version>0.23.96</version>
99
<packaging>pom</packaging>
1010
<description>openai java 版本</description>
1111
<name>openai-java</name>

service/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>top.bella</groupId>
88
<artifactId>openai-java</artifactId>
9-
<version>0.23.95</version>
9+
<version>0.23.96</version>
1010
</parent>
1111
<packaging>jar</packaging>
1212

0 commit comments

Comments
 (0)