-
Notifications
You must be signed in to change notification settings - Fork 2.2k
fix: add booleanSchemaValue to Schema's hashCode and equals #4958
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
Merged
+185
−3
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
181 changes: 181 additions & 0 deletions
181
modules/swagger-models/src/test/java/io/swagger/v3/oas/models/media/SchemaTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,181 @@ | ||
| package io.swagger.v3.oas.models.media; | ||
|
|
||
| import org.testng.annotations.Test; | ||
| import static org.testng.Assert.*; | ||
|
|
||
| import java.math.BigDecimal; | ||
| import java.util.Arrays; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| public class SchemaTest { | ||
|
|
||
| @Test | ||
| public void testEqualsWithDifferentBooleanSchemaValue() { | ||
| Schema<Object> schema1 = new Schema<>(); | ||
| Schema<Object> schema2 = new Schema<>(); | ||
|
|
||
| schema1.setBooleanSchemaValue(true); | ||
| schema2.setBooleanSchemaValue(false); | ||
|
|
||
| assertFalse(schema1.equals(schema2)); | ||
| assertFalse(schema2.equals(schema1)); | ||
| assertNotEquals(schema1.hashCode(), schema2.hashCode()); | ||
| } | ||
|
|
||
|
|
||
| @Test | ||
| public void testEqualsWithOneNullBooleanSchemaValue() { | ||
| Schema<Object> schema1 = new Schema<>(); | ||
| Schema<Object> schema2 = new Schema<>(); | ||
|
|
||
| schema1.setBooleanSchemaValue(true); | ||
| schema2.setBooleanSchemaValue(null); | ||
|
|
||
| assertFalse(schema1.equals(schema2)); | ||
| assertFalse(schema2.equals(schema1)); | ||
| assertNotEquals(schema1.hashCode(), schema2.hashCode()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testBooleanSchemaValueFalseVsTrue() { | ||
| // This test specifically addresses the issue where false gets mapped to true | ||
| Schema<Object> schemaFalse = new Schema<>(); | ||
| Schema<Object> schemaTrue = new Schema<>(); | ||
|
|
||
| schemaFalse.setBooleanSchemaValue(false); | ||
| schemaTrue.setBooleanSchemaValue(true); | ||
|
|
||
| // These should be different | ||
| assertFalse(schemaFalse.equals(schemaTrue)); | ||
| assertFalse(schemaTrue.equals(schemaFalse)); | ||
|
|
||
| // Hash codes should be different to prevent collisions in maps | ||
| assertNotEquals(schemaFalse.hashCode(), schemaTrue.hashCode()); | ||
|
|
||
| // Verify the actual values | ||
| assertEquals(Boolean.FALSE, schemaFalse.getBooleanSchemaValue()); | ||
| assertEquals(Boolean.TRUE, schemaTrue.getBooleanSchemaValue()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testComplexSchemaEqualsWithBooleanSchemaValue() { | ||
| Schema<Object> schema1 = createComplexSchema(); | ||
| Schema<Object> schema2 = createComplexSchema(); | ||
|
|
||
| // Initially they should be equal | ||
| assertTrue(schema1.equals(schema2)); | ||
| assertEquals(schema1.hashCode(), schema2.hashCode()); | ||
|
|
||
| // Change only booleanSchemaValue | ||
| schema2.setBooleanSchemaValue(false); | ||
|
|
||
| // Now they should be different | ||
| assertFalse(schema1.equals(schema2)); | ||
| assertNotEquals(schema1.hashCode(), schema2.hashCode()); | ||
| } | ||
|
|
||
|
|
||
| @Test | ||
| public void testBooleanSchemaValueInMapLookup() { | ||
| Map<Schema<Object>, String> visitedMap = new HashMap<>(); | ||
|
|
||
| Schema<Object> schemaTrue = new Schema<>(); | ||
| Schema<Object> schemaFalse = new Schema<>(); | ||
|
|
||
| schemaTrue.setBooleanSchemaValue(true); | ||
| schemaFalse.setBooleanSchemaValue(false); | ||
|
|
||
| visitedMap.put(schemaTrue, "true_schema"); | ||
| visitedMap.put(schemaFalse, "false_schema"); | ||
|
|
||
| assertEquals(visitedMap.size(), 2); | ||
| assertEquals(visitedMap.get(schemaTrue), "true_schema"); | ||
| assertEquals(visitedMap.get(schemaFalse), "false_schema"); | ||
|
|
||
| Schema<Object> anotherTrue = new Schema<>(); | ||
| anotherTrue.setBooleanSchemaValue(true); | ||
| assertEquals(visitedMap.get(anotherTrue), "true_schema"); | ||
|
|
||
| Schema<Object> anotherFalse = new Schema<>(); | ||
| anotherFalse.setBooleanSchemaValue(false); | ||
| assertEquals(visitedMap.get(anotherFalse), "false_schema"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSchemaResolutionWithBooleanSchemaValue() { | ||
| Map<Schema<Object>, Schema<Object>> visitedMap = new HashMap<>(); | ||
|
|
||
| // Create a schema with booleanSchemaValue = false | ||
| Schema<Object> originalSchema = new Schema<>(); | ||
| originalSchema.setBooleanSchemaValue(false); | ||
| originalSchema.setTitle("Original Schema"); | ||
| originalSchema.setType("boolean"); | ||
|
|
||
| // Create a resolved version of the same schema | ||
| Schema<Object> resolvedSchema = new Schema<>(); | ||
| resolvedSchema.setBooleanSchemaValue(false); | ||
| resolvedSchema.setTitle("Resolved Schema"); | ||
| resolvedSchema.setType("boolean"); | ||
|
|
||
| // Put the original schema in the visited map | ||
| visitedMap.put(originalSchema, resolvedSchema); | ||
|
|
||
| // Create another schema with identical structure but booleanSchemaValue = true | ||
| Schema<Object> differentSchema = new Schema<>(); | ||
| differentSchema.setBooleanSchemaValue(true); | ||
| differentSchema.setTitle("Original Schema"); | ||
| differentSchema.setType("boolean"); | ||
|
|
||
| // The lookup should NOT return the resolved schema for the different schema | ||
| assertNull(visitedMap.get(differentSchema)); | ||
|
|
||
| // But it should return the resolved schema for an equivalent schema with false value | ||
| Schema<Object> equivalentSchema = new Schema<>(); | ||
| equivalentSchema.setBooleanSchemaValue(false); | ||
| equivalentSchema.setTitle("Original Schema"); | ||
| equivalentSchema.setType("boolean"); | ||
| assertEquals(resolvedSchema, visitedMap.get(equivalentSchema)); | ||
|
|
||
| // Verify that the schemas with different booleanSchemaValue are indeed different | ||
| assertNotEquals(originalSchema, differentSchema); | ||
| assertNotEquals(originalSchema.hashCode(), differentSchema.hashCode()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testBooleanSchemaValueInSetOperations() { | ||
| java.util.Set<Schema<Object>> schemaSet = new java.util.HashSet<>(); | ||
|
|
||
| Schema<Object> schemaTrue = new Schema<>(); | ||
| schemaTrue.setBooleanSchemaValue(true); | ||
|
|
||
| Schema<Object> schemaFalse = new Schema<>(); | ||
| schemaFalse.setBooleanSchemaValue(false); | ||
|
|
||
| schemaSet.add(schemaTrue); | ||
| schemaSet.add(schemaFalse); | ||
|
|
||
| // Should have two distinct schemas in the set | ||
| assertEquals(schemaSet.size(), 2); | ||
| assertTrue(schemaSet.contains(schemaTrue)); | ||
| assertTrue(schemaSet.contains(schemaFalse)); | ||
|
|
||
| // Adding equivalent schemas should not increase the set size | ||
| Schema<Object> anotherTrue = new Schema<>(); | ||
| anotherTrue.setBooleanSchemaValue(true); | ||
| schemaSet.add(anotherTrue); | ||
| assertEquals(schemaSet.size(), 2); | ||
| } | ||
|
|
||
| private Schema<Object> createComplexSchema() { | ||
| Schema<Object> schema = new Schema<>(); | ||
| schema.setTitle("Complex Schema"); | ||
| schema.setType("object"); | ||
| schema.setDescription("A complex schema for testing"); | ||
| schema.setMaximum(new BigDecimal("100")); | ||
| schema.setMinimum(new BigDecimal("0")); | ||
| schema.setRequired(Arrays.asList("id", "name")); | ||
| schema.setBooleanSchemaValue(true); | ||
| return schema; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.