Skip to content

Commit 9ca3a7d

Browse files
dev-jonghoonparkmarkpollack
authored andcommitted
Refactor OpenAiImageOptions and enhance test coverage
This commit makes several improvements to the OpenAiImageOptions class: 1. Remove all deprecated methods from OpenAiImageOptions.Builder: - Removed withN(), withModel(), withQuality(), withResponseFormat(), withWidth(), withHeight(), withStyle(), and withUser() methods - These were marked as @deprecated(forRemoval = true, since = "1.0.0-M8") 2. Align OpenAiImageOptions structure with OpenAiChatOptions: - Added fromOptions() static method for creating copies - Added copy() instance method - Updated Builder class to match pattern in OpenAiChatOptions - Changed Builder field from private final to protected - Added Builder constructor that takes an existing options object 3. Enhance setSize() method to maintain consistency: - Updated setSize() to parse the size string and update width/height properties - Added proper error handling for invalid formats - Ensures consistent state between size, width, and height properties 4. Comprehensive test coverage improvements: - Added tests for builder pattern with all fields - Added tests for copy functionality - Added tests for all setter methods - Added tests for default values - Added tests for equals(), hashCode(), and toString() methods - Added specific tests for the updated setSize() behavior - Fixed test expectations to match actual implementation behavior These changes improve code consistency, maintainability, and test coverage while removing deprecated methods that were scheduled for removal. Signed-off-by: jonghoonpark <[email protected]>
1 parent a2975a6 commit 9ca3a7d

File tree

4 files changed

+322
-42
lines changed

4 files changed

+322
-42
lines changed

.claude/settings.local.json

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"permissions": {
3+
"allow": [
4+
"Bash(gh pr list:*)",
5+
"Bash(gh pr view:*)",
6+
"Bash(gh issue list:*)",
7+
"Bash(gh issue view:*)",
8+
"WebFetch(domain:github.com)"
9+
],
10+
"deny": []
11+
}
12+
}

models/spring-ai-openai/src/main/java/org/springframework/ai/openai/OpenAiImageOptions.java

+47-42
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,25 @@ public static Builder builder() {
112112
return new Builder();
113113
}
114114

115+
/**
116+
* Create a new OpenAiImageOptions instance from an existing one.
117+
* @param fromOptions The options to copy from
118+
* @return A new OpenAiImageOptions instance
119+
*/
120+
public static OpenAiImageOptions fromOptions(OpenAiImageOptions fromOptions) {
121+
OpenAiImageOptions options = new OpenAiImageOptions();
122+
options.n = fromOptions.n;
123+
options.model = fromOptions.model;
124+
options.width = fromOptions.width;
125+
options.height = fromOptions.height;
126+
options.quality = fromOptions.quality;
127+
options.responseFormat = fromOptions.responseFormat;
128+
options.size = fromOptions.size;
129+
options.style = fromOptions.style;
130+
options.user = fromOptions.user;
131+
return options;
132+
}
133+
115134
@Override
116135
public Integer getN() {
117136
return this.n;
@@ -227,6 +246,20 @@ public String getSize() {
227246

228247
public void setSize(String size) {
229248
this.size = size;
249+
250+
// Parse the size string to update width and height
251+
if (size != null) {
252+
try {
253+
String[] dimensions = size.split("x");
254+
if (dimensions.length == 2) {
255+
this.width = Integer.parseInt(dimensions[0]);
256+
this.height = Integer.parseInt(dimensions[1]);
257+
}
258+
}
259+
catch (Exception ex) {
260+
// If parsing fails, leave width and height unchanged
261+
}
262+
}
230263
}
231264

232265
@Override
@@ -258,14 +291,26 @@ public String toString() {
258291
+ ", user='" + this.user + '\'' + '}';
259292
}
260293

294+
/**
295+
* Create a copy of this options instance.
296+
* @return A new instance with the same options
297+
*/
298+
public OpenAiImageOptions copy() {
299+
return fromOptions(this);
300+
}
301+
261302
public static final class Builder {
262303

263-
private final OpenAiImageOptions options;
304+
protected OpenAiImageOptions options;
264305

265-
private Builder() {
306+
public Builder() {
266307
this.options = new OpenAiImageOptions();
267308
}
268309

310+
public Builder(OpenAiImageOptions options) {
311+
this.options = options;
312+
}
313+
269314
public Builder N(Integer n) {
270315
this.options.setN(n);
271316
return this;
@@ -306,46 +351,6 @@ public Builder user(String user) {
306351
return this;
307352
}
308353

309-
public Builder withN(Integer n) {
310-
this.options.setN(n);
311-
return this;
312-
}
313-
314-
public Builder withModel(String model) {
315-
this.options.setModel(model);
316-
return this;
317-
}
318-
319-
public Builder withQuality(String quality) {
320-
this.options.setQuality(quality);
321-
return this;
322-
}
323-
324-
public Builder withResponseFormat(String responseFormat) {
325-
this.options.setResponseFormat(responseFormat);
326-
return this;
327-
}
328-
329-
public Builder withWidth(Integer width) {
330-
this.options.setWidth(width);
331-
return this;
332-
}
333-
334-
public Builder withHeight(Integer height) {
335-
this.options.setHeight(height);
336-
return this;
337-
}
338-
339-
public Builder withStyle(String style) {
340-
this.options.setStyle(style);
341-
return this;
342-
}
343-
344-
public Builder withUser(String user) {
345-
this.options.setUser(user);
346-
return this;
347-
}
348-
349354
public OpenAiImageOptions build() {
350355
return this.options;
351356
}

0 commit comments

Comments
 (0)