-
Notifications
You must be signed in to change notification settings - Fork 25k
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
Enable strict duplicate checks for JSON content #22073
Changes from 6 commits
dfd402f
80f1d07
5ba6b26
41504c4
d4a39ee
26e827c
3de1d07
6398450
53e06f7
2116831
9c9e9f6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -67,7 +67,7 @@ public void testFieldsParsing() throws Exception { | |
assertThat(request.getIndexConstraints()[3].getComparison(), equalTo(LTE)); | ||
assertThat(request.getIndexConstraints()[4].getField(), equalTo("field5")); | ||
assertThat(request.getIndexConstraints()[4].getValue(), equalTo("2")); | ||
assertThat(request.getIndexConstraints()[4].getProperty(), equalTo(MAX)); | ||
assertThat(request.getIndexConstraints()[4].getProperty(), equalTo(MIN)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For those following along at home, this is required because the example data had a duplicate key and @danielmitterdorfer switched it to |
||
assertThat(request.getIndexConstraints()[4].getComparison(), equalTo(GT)); | ||
assertThat(request.getIndexConstraints()[5].getField(), equalTo("field5")); | ||
assertThat(request.getIndexConstraints()[5].getValue(), equalTo("9")); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ | |
import org.elasticsearch.ElasticsearchParseException; | ||
import org.elasticsearch.common.settings.Settings; | ||
import org.elasticsearch.common.settings.SettingsException; | ||
import org.elasticsearch.common.xcontent.json.JsonXContent; | ||
import org.elasticsearch.test.ESTestCase; | ||
|
||
import static org.hamcrest.CoreMatchers.containsString; | ||
|
@@ -48,6 +49,10 @@ public void testSimpleJsonSettings() throws Exception { | |
} | ||
|
||
public void testDuplicateKeysThrowsException() { | ||
if (JsonXContent.isStrictDuplicateDetectionEnabled()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, I didn't know about There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cool. I think I did have it backwards. |
||
logger.info("Skipping test as it uses a custom duplicate check that is obsolete when strict duplicate checks are enabled."); | ||
return; | ||
} | ||
final String json = "{\"foo\":\"bar\",\"foo\":\"baz\"}"; | ||
final SettingsException e = expectThrows(SettingsException.class, () -> Settings.builder().loadFromSource(json).build()); | ||
assertEquals(e.getCause().getClass(), ElasticsearchParseException.class); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ | |
import com.fasterxml.jackson.core.JsonFactory; | ||
import com.fasterxml.jackson.core.JsonGenerator; | ||
|
||
import com.fasterxml.jackson.core.JsonParseException; | ||
import org.elasticsearch.common.xcontent.BaseXContentTestCase; | ||
import org.elasticsearch.common.xcontent.XContentType; | ||
|
||
|
@@ -39,4 +40,15 @@ public void testBigInteger() throws Exception { | |
JsonGenerator generator = new JsonFactory().createGenerator(os); | ||
doTestBigInteger(generator, os); | ||
} | ||
|
||
public void testChecksForDuplicates() throws Exception { | ||
if (JsonXContent.isStrictDuplicateDetectionEnabled() == false) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I'll change all affected tests accordingly. |
||
logger.info("Skipping test as it is only effective is strict duplicate detection is enabled"); | ||
return; | ||
} | ||
|
||
JsonParseException pex = expectThrows(JsonParseException.class, | ||
() -> XContentType.JSON.xContent().createParser("{ \"key\": 1, \"key\": 2 }").map()); | ||
assertEquals("Duplicate field 'key'", pex.getMessage()); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this exception still a problem?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes it is. A user could still pass
-Des.json.strict_duplicate_detection=complete_bogus
and provoke an exception when we try to convert the system property to aboolean
. I don't want to be lenient and terminate early then.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If they specify a non-boolean I think they'll get
false
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree that we should only support
true
andfalse
though.