Skip to content

fix: incorrect equals and hashCode in Options class #4049

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.List;
import java.util.Map;

import nl.jqno.equalsverifier.EqualsVerifier;
import org.junit.jupiter.api.Test;

import org.springframework.ai.anthropic.api.AnthropicApi.ChatCompletionRequest.Metadata;
Expand Down Expand Up @@ -159,6 +160,8 @@ void testCopyMutationDoesNotAffectOriginal() {
assertThat(copy.getModel()).isEqualTo("modified-model");
assertThat(copy.getMaxTokens()).isEqualTo(200);
assertThat(copy.getTemperature()).isEqualTo(0.8);

EqualsVerifier.simple().forClass(AnthropicChatOptions.class).usingGetClass().verify();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.springframework.ai.azure.openai;

import java.util.List;
import java.util.Objects;

import com.azure.ai.openai.models.AudioTranscriptionFormat;
import com.azure.ai.openai.models.AudioTranscriptionTimestampGranularity;
Expand Down Expand Up @@ -66,6 +67,7 @@ public class AzureOpenAiAudioTranscriptionOptions implements AudioTranscriptionO
private @JsonProperty("temperature") Float temperature = 0F;

private @JsonProperty("timestamp_granularities") List<GranularityType> granularityType;
// @formatter:on

public static Builder builder() {
return new Builder();
Expand Down Expand Up @@ -129,58 +131,26 @@ public void setGranularityType(List<GranularityType> granularityType) {
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((this.model == null) ? 0 : this.model.hashCode());
result = prime * result + ((this.prompt == null) ? 0 : this.prompt.hashCode());
result = prime * result + ((this.language == null) ? 0 : this.language.hashCode());
result = prime * result + ((this.responseFormat == null) ? 0 : this.responseFormat.hashCode());
return result;
public final boolean equals(Object o) {
if (!(o instanceof AzureOpenAiAudioTranscriptionOptions that))
return false;

return Objects.equals(model, that.model) && Objects.equals(deploymentName, that.deploymentName)
&& responseFormat == that.responseFormat && Objects.equals(prompt, that.prompt)
&& Objects.equals(language, that.language) && Objects.equals(temperature, that.temperature)
&& Objects.equals(granularityType, that.granularityType);
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
AzureOpenAiAudioTranscriptionOptions other = (AzureOpenAiAudioTranscriptionOptions) obj;
if (this.model == null) {
if (other.model != null) {
return false;
}
}
else if (!this.model.equals(other.model)) {
return false;
}
if (this.prompt == null) {
if (other.prompt != null) {
return false;
}
}
else if (!this.prompt.equals(other.prompt)) {
return false;
}
if (this.language == null) {
if (other.language != null) {
return false;
}
}
else if (!this.language.equals(other.language)) {
return false;
}
if (this.responseFormat == null) {
return other.responseFormat == null;
}
else {
return this.responseFormat.equals(other.responseFormat);
}
public int hashCode() {
int result = Objects.hashCode(model);
result = 31 * result + Objects.hashCode(deploymentName);
result = 31 * result + Objects.hashCode(responseFormat);
result = 31 * result + Objects.hashCode(prompt);
result = 31 * result + Objects.hashCode(language);
result = 31 * result + Objects.hashCode(temperature);
result = 31 * result + Objects.hashCode(granularityType);
return result;
}

public enum WhisperModel {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,8 @@ public boolean equals(Object o) {
&& Objects.equals(this.toolCallbacks, that.toolCallbacks)
&& Objects.equals(this.toolNames, that.toolNames)
&& Objects.equals(this.internalToolExecutionEnabled, that.internalToolExecutionEnabled)
&& Objects.equals(this.logprobs, that.logprobs) && Objects.equals(this.topLogProbs, that.topLogProbs)
&& Objects.equals(this.seed, that.seed) && Objects.equals(this.logprobs, that.logprobs)
&& Objects.equals(this.topLogProbs, that.topLogProbs)
&& Objects.equals(this.enhancements, that.enhancements)
&& Objects.equals(this.streamOptions, that.streamOptions)
&& Objects.equals(this.enableStreamUsage, that.enableStreamUsage)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.springframework.ai.azure.openai;

import java.util.List;
import java.util.Objects;

import com.fasterxml.jackson.annotation.JsonIgnore;

Expand Down Expand Up @@ -105,6 +106,24 @@ public void setDimensions(Integer dimensions) {
this.dimensions = dimensions;
}

@Override
public final boolean equals(Object o) {
if (!(o instanceof AzureOpenAiEmbeddingOptions that))
return false;

return Objects.equals(user, that.user) && Objects.equals(deploymentName, that.deploymentName)
&& Objects.equals(inputType, that.inputType) && Objects.equals(dimensions, that.dimensions);
}

@Override
public int hashCode() {
int result = Objects.hashCode(user);
result = 31 * result + Objects.hashCode(deploymentName);
result = 31 * result + Objects.hashCode(inputType);
result = 31 * result + Objects.hashCode(dimensions);
return result;
}

public com.azure.ai.openai.models.EmbeddingsOptions toAzureOptions(List<String> instructions) {

var azureOptions = new com.azure.ai.openai.models.EmbeddingsOptions(instructions);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2025-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.ai.azure.openai;

import nl.jqno.equalsverifier.EqualsVerifier;
import org.junit.jupiter.api.Test;

class AzureOpenAiAudioTranscriptionOptionsTests {

@Test
void testEqualsAndHashCode() {
EqualsVerifier.simple().forClass(AzureOpenAiAudioTranscriptionOptions.class).usingGetClass().verify();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.azure.ai.openai.models.AzureChatGroundingEnhancementConfiguration;
import com.azure.ai.openai.models.AzureChatOCREnhancementConfiguration;
import com.azure.ai.openai.models.ChatCompletionStreamOptions;
import nl.jqno.equalsverifier.EqualsVerifier;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -34,6 +35,11 @@
*/
class AzureOpenAiChatOptionsTests {

@Test
void testEqualsAndHashCode() {
EqualsVerifier.simple().forClass(AzureOpenAiChatOptions.class).usingGetClass().verify();
}

@Test
void testBuilderWithAllFields() {
AzureOpenAiResponseFormat responseFormat = AzureOpenAiResponseFormat.builder()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2025-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.ai.azure.openai;

import nl.jqno.equalsverifier.EqualsVerifier;
import org.junit.jupiter.api.Test;

class AzureOpenAiEmbeddingOptionsTests {

@Test
void testEqualsAndHashCode() {
EqualsVerifier.simple().forClass(AzureOpenAiEmbeddingOptions.class).usingGetClass().verify();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2025-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.ai.azure.openai;

import nl.jqno.equalsverifier.EqualsVerifier;
import org.junit.jupiter.api.Test;

class AzureOpenAiImageOptionsTests {

@Test
void testEqualsAndHashCode() {
EqualsVerifier.simple().forClass(AzureOpenAiImageOptions.class).usingGetClass().verify();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@

package org.springframework.ai.bedrock.converse;

import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.Map;

import nl.jqno.equalsverifier.EqualsVerifier;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

/**
Expand All @@ -30,6 +31,11 @@
*/
class BedrockChatOptionsTests {

@Test
void testEqualsAndHashCode() {
EqualsVerifier.simple().forClass(BedrockChatOptions.class).usingGetClass().verify();
}

@Test
void testBuilderWithAllFields() {
BedrockChatOptions options = BedrockChatOptions.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package org.springframework.ai.bedrock.cohere;

import java.util.Objects;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
Expand Down Expand Up @@ -84,6 +86,21 @@ public Integer getDimensions() {
return null;
}

@Override
public final boolean equals(Object o) {
if (!(o instanceof BedrockCohereEmbeddingOptions that))
return false;

return inputType == that.inputType && truncate == that.truncate;
}

@Override
public int hashCode() {
int result = Objects.hashCode(inputType);
result = 31 * result + Objects.hashCode(truncate);
return result;
}

public static class Builder {

private BedrockCohereEmbeddingOptions options = new BedrockCohereEmbeddingOptions();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package org.springframework.ai.bedrock.titan;

import java.util.Objects;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
Expand Down Expand Up @@ -56,6 +58,19 @@ public String getModel() {
return null;
}

@Override
public final boolean equals(Object o) {
if (!(o instanceof BedrockTitanEmbeddingOptions that))
return false;

return inputType == that.inputType;
}

@Override
public int hashCode() {
return Objects.hashCode(inputType);
}

@Override
@JsonIgnore
public Integer getDimensions() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2025-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.ai.bedrock.cohere;

import nl.jqno.equalsverifier.EqualsVerifier;
import org.junit.jupiter.api.Test;

class BedrockCohereEmbeddingOptionsTests {

@Test
void testEqualsAndHashCode() {
EqualsVerifier.simple().forClass(BedrockCohereEmbeddingOptions.class).usingGetClass().verify();
}

}
Loading