Skip to content
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

Make boolean conversion strict #22200

Merged
merged 26 commits into from
Jan 19, 2017
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
b5d642b
Make boolean conversion strict
danielmitterdorfer Dec 15, 2016
e705d96
Use Booleans#parseBoolean in JSonXContent
danielmitterdorfer Dec 16, 2016
9fb7ea1
Merge remote-tracking branch 'origin/master' into strict-booleans
danielmitterdorfer Dec 19, 2016
db3fd5e
Address initial review comments
danielmitterdorfer Dec 19, 2016
c1b9df1
Merge remote-tracking branch 'origin/master' into strict-booleans
danielmitterdorfer Dec 21, 2016
cbb3822
Improve BWC
danielmitterdorfer Dec 21, 2016
c09b301
Recognize numbers as booleans in lenient mode
danielmitterdorfer Dec 21, 2016
665554d
Remove boolean leniency for snapshot requests
danielmitterdorfer Dec 23, 2016
1ee2db4
Remove boolean leniency for indices options
danielmitterdorfer Dec 23, 2016
f7337ba
Remove boolean leniency for multi search API
danielmitterdorfer Dec 23, 2016
206adfa
Remove boolean leniency for mappers
danielmitterdorfer Dec 23, 2016
fcfce6c
Merge remote-tracking branch 'origin/master' into strict-booleans
danielmitterdorfer Dec 23, 2016
8de2eb4
Merge remote-tracking branch 'origin/master' into strict-booleans
danielmitterdorfer Jan 2, 2017
6144f36
Minor simplifications and more tests
danielmitterdorfer Jan 3, 2017
22aabf2
Correct minor oddities
danielmitterdorfer Jan 3, 2017
978f047
Merge remote-tracking branch 'origin/master' into strict-booleans
danielmitterdorfer Jan 3, 2017
33af9f1
Remove IOException in SettingsTests
danielmitterdorfer Jan 3, 2017
55b7e2b
Avoid to create mixed bool representations in translog
danielmitterdorfer Jan 3, 2017
0577acc
Merge remote-tracking branch 'origin/master' into strict-booleans
danielmitterdorfer Jan 10, 2017
4c90110
Address review comments
danielmitterdorfer Jan 13, 2017
f4a2f64
Merge remote-tracking branch 'origin/master' into strict-booleans
danielmitterdorfer Jan 13, 2017
0f91b13
Include more context on parse errors in nodeBooleanValue
danielmitterdorfer Jan 13, 2017
61f3912
Document breaking REST API changes
danielmitterdorfer Jan 13, 2017
bdc029e
Correct mapping migration docs
danielmitterdorfer Jan 13, 2017
fc8f39a
Merge remote-tracking branch 'origin/master' into strict-booleans
danielmitterdorfer Jan 18, 2017
4523e55
Address further review comments
danielmitterdorfer Jan 18, 2017
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
4 changes: 0 additions & 4 deletions buildSrc/src/main/resources/checkstyle_suppressions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,6 @@
<suppress files="core[/\\]src[/\\]main[/\\]java[/\\]org[/\\]elasticsearch[/\\]cluster[/\\]routing[/\\]allocation[/\\]decider[/\\]AllocationDeciders.java" checks="LineLength" />
<suppress files="core[/\\]src[/\\]main[/\\]java[/\\]org[/\\]elasticsearch[/\\]cluster[/\\]service[/\\]InternalClusterService.java" checks="LineLength" />
<suppress files="core[/\\]src[/\\]main[/\\]java[/\\]org[/\\]elasticsearch[/\\]common[/\\]Base64.java" checks="LineLength" />
<suppress files="core[/\\]src[/\\]main[/\\]java[/\\]org[/\\]elasticsearch[/\\]common[/\\]Booleans.java" checks="LineLength" />
<suppress files="core[/\\]src[/\\]main[/\\]java[/\\]org[/\\]elasticsearch[/\\]common[/\\]Numbers.java" checks="LineLength" />
<suppress files="core[/\\]src[/\\]main[/\\]java[/\\]org[/\\]elasticsearch[/\\]common[/\\]blobstore[/\\]fs[/\\]FsBlobStore.java" checks="LineLength" />
<suppress files="core[/\\]src[/\\]main[/\\]java[/\\]org[/\\]elasticsearch[/\\]common[/\\]blobstore[/\\]url[/\\]URLBlobStore.java" checks="LineLength" />
Expand Down Expand Up @@ -566,8 +565,6 @@
<suppress files="core[/\\]src[/\\]test[/\\]java[/\\]org[/\\]elasticsearch[/\\]action[/\\]ingest[/\\]SimulatePipelineResponseTests.java" checks="LineLength" />
<suppress files="core[/\\]src[/\\]test[/\\]java[/\\]org[/\\]elasticsearch[/\\]action[/\\]ingest[/\\]WriteableIngestDocumentTests.java" checks="LineLength" />
<suppress files="core[/\\]src[/\\]test[/\\]java[/\\]org[/\\]elasticsearch[/\\]action[/\\]search[/\\]SearchRequestBuilderTests.java" checks="LineLength" />
<suppress files="core[/\\]src[/\\]test[/\\]java[/\\]org[/\\]elasticsearch[/\\]action[/\\]support[/\\]IndicesOptionsTests.java" checks="LineLength" />
<suppress files="core[/\\]src[/\\]test[/\\]java[/\\]org[/\\]elasticsearch[/\\]action[/\\]support[/\\]IndicesOptionsTests.java" checks="LineLength" />
<suppress files="core[/\\]src[/\\]test[/\\]java[/\\]org[/\\]elasticsearch[/\\]action[/\\]support[/\\]TransportActionFilterChainTests.java" checks="LineLength" />
<suppress files="core[/\\]src[/\\]test[/\\]java[/\\]org[/\\]elasticsearch[/\\]action[/\\]support[/\\]WaitActiveShardCountIT.java" checks="LineLength" />
<suppress files="core[/\\]src[/\\]test[/\\]java[/\\]org[/\\]elasticsearch[/\\]action[/\\]support[/\\]broadcast[/\\]node[/\\]TransportBroadcastByNodeActionTests.java" checks="LineLength" />
Expand Down Expand Up @@ -656,7 +653,6 @@
<suppress files="core[/\\]src[/\\]test[/\\]java[/\\]org[/\\]elasticsearch[/\\]cluster[/\\]settings[/\\]ClusterSettingsIT.java" checks="LineLength" />
<suppress files="core[/\\]src[/\\]test[/\\]java[/\\]org[/\\]elasticsearch[/\\]cluster[/\\]shards[/\\]ClusterSearchShardsIT.java" checks="LineLength" />
<suppress files="core[/\\]src[/\\]test[/\\]java[/\\]org[/\\]elasticsearch[/\\]cluster[/\\]structure[/\\]RoutingIteratorTests.java" checks="LineLength" />
<suppress files="core[/\\]src[/\\]test[/\\]java[/\\]org[/\\]elasticsearch[/\\]common[/\\]BooleansTests.java" checks="LineLength" />
<suppress files="core[/\\]src[/\\]test[/\\]java[/\\]org[/\\]elasticsearch[/\\]common[/\\]blobstore[/\\]FsBlobStoreContainerTests.java" checks="LineLength" />
<suppress files="core[/\\]src[/\\]test[/\\]java[/\\]org[/\\]elasticsearch[/\\]common[/\\]blobstore[/\\]FsBlobStoreTests.java" checks="LineLength" />
<suppress files="core[/\\]src[/\\]test[/\\]java[/\\]org[/\\]elasticsearch[/\\]common[/\\]breaker[/\\]MemoryCircuitBreakerTests.java" checks="LineLength" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
import static org.elasticsearch.common.settings.Settings.readSettingsFromStream;
import static org.elasticsearch.common.settings.Settings.writeSettingsToStream;
import static org.elasticsearch.common.settings.Settings.Builder.EMPTY_SETTINGS;
import static org.elasticsearch.common.xcontent.support.XContentMapValues.lenientNodeBooleanValue;
import static org.elasticsearch.common.xcontent.support.XContentMapValues.nodeBooleanValue;

/**
* Create snapshot request
Expand Down Expand Up @@ -366,14 +366,14 @@ public CreateSnapshotRequest source(Map<String, Object> source) {
throw new IllegalArgumentException("malformed indices section, should be an array of strings");
}
} else if (name.equals("partial")) {
partial(lenientNodeBooleanValue(entry.getValue()));
partial(nodeBooleanValue(entry.getValue(), "partial"));
} else if (name.equals("settings")) {
if (!(entry.getValue() instanceof Map)) {
throw new IllegalArgumentException("malformed settings section, should indices an inner object");
}
settings((Map<String, Object>) entry.getValue());
} else if (name.equals("include_global_state")) {
includeGlobalState = lenientNodeBooleanValue(entry.getValue());
includeGlobalState = nodeBooleanValue(entry.getValue(), "include_global_state");
}
}
indicesOptions(IndicesOptions.fromMap((Map<String, Object>) source, IndicesOptions.lenientExpandOpen()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
import static org.elasticsearch.common.settings.Settings.readSettingsFromStream;
import static org.elasticsearch.common.settings.Settings.writeSettingsToStream;
import static org.elasticsearch.common.settings.Settings.Builder.EMPTY_SETTINGS;
import static org.elasticsearch.common.xcontent.support.XContentMapValues.lenientNodeBooleanValue;
import static org.elasticsearch.common.xcontent.support.XContentMapValues.nodeBooleanValue;

/**
* Restore snapshot request
Expand Down Expand Up @@ -481,16 +481,16 @@ public RestoreSnapshotRequest source(Map<String, Object> source) {
throw new IllegalArgumentException("malformed indices section, should be an array of strings");
}
} else if (name.equals("partial")) {
partial(lenientNodeBooleanValue(entry.getValue()));
partial(nodeBooleanValue(entry.getValue(), "partial"));
} else if (name.equals("settings")) {
if (!(entry.getValue() instanceof Map)) {
throw new IllegalArgumentException("malformed settings section");
}
settings((Map<String, Object>) entry.getValue());
} else if (name.equals("include_global_state")) {
includeGlobalState = lenientNodeBooleanValue(entry.getValue());
includeGlobalState = nodeBooleanValue(entry.getValue(), "include_global_state");
} else if (name.equals("include_aliases")) {
includeAliases = lenientNodeBooleanValue(entry.getValue());
includeAliases = nodeBooleanValue(entry.getValue(), "include_aliases");
} else if (name.equals("rename_pattern")) {
if (entry.getValue() instanceof String) {
renamePattern((String) entry.getValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,8 @@ public static void parseDocuments(XContentParser parser, List<Item> items, @Null
} else if ("_version_type".equals(currentFieldName) || "_versionType".equals(currentFieldName) || "version_type".equals(currentFieldName) || "versionType".equals(currentFieldName)) {
versionType = VersionType.fromString(parser.text());
} else if ("_source".equals(currentFieldName)) {
if (parser.isBooleanValue()) {
// check lenient to avoid interpreting the value as string but parse strict in order to provoke an error early on.
if (parser.isBooleanValueLenient()) {
fetchSourceContext = new FetchSourceContext(parser.booleanValue(), fetchSourceContext.includes(),
fetchSourceContext.excludes());
} else if (token == XContentParser.Token.VALUE_STRING) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ private AutoCreate(String value) {
boolean autoCreateIndex;
List<Tuple<String, Boolean>> expressions = new ArrayList<>();
try {
autoCreateIndex = Booleans.parseBooleanExact(value);
autoCreateIndex = Booleans.parseBoolean(value);
} catch (IllegalArgumentException ex) {
try {
String[] patterns = Strings.commaDelimitedListToStringArray(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import java.io.IOException;
import java.util.Map;

import static org.elasticsearch.common.xcontent.support.XContentMapValues.lenientNodeBooleanValue;
import static org.elasticsearch.common.xcontent.support.XContentMapValues.nodeBooleanValue;
import static org.elasticsearch.common.xcontent.support.XContentMapValues.nodeStringArrayValue;

/**
Expand Down Expand Up @@ -195,8 +195,8 @@ public static IndicesOptions fromParameters(Object wildcardsString, Object ignor

//note that allowAliasesToMultipleIndices is not exposed, always true (only for internal use)
return fromOptions(
lenientNodeBooleanValue(ignoreUnavailableString, defaultSettings.ignoreUnavailable()),
lenientNodeBooleanValue(allowNoIndicesString, defaultSettings.allowNoIndices()),
nodeBooleanValue(ignoreUnavailableString, "ignore_unavailable", defaultSettings.ignoreUnavailable()),
nodeBooleanValue(allowNoIndicesString, "allow_no_indices", defaultSettings.allowNoIndices()),
expandWildcardsOpen,
expandWildcardsClosed,
defaultSettings.allowAliasesToMultipleIndices(),
Expand Down Expand Up @@ -279,7 +279,7 @@ public String toString() {
", allow_no_indices=" + allowNoIndices() +
", expand_wildcards_open=" + expandWildcardsOpen() +
", expand_wildcards_closed=" + expandWildcardsClosed() +
", allow_alisases_to_multiple_indices=" + allowAliasesToMultipleIndices() +
", allow_aliases_to_multiple_indices=" + allowAliasesToMultipleIndices() +
", forbid_closed_indices=" + forbidClosedIndices() +
']';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ final class AutoExpandReplicas {
public static final Setting<AutoExpandReplicas> SETTING = new Setting<>(IndexMetaData.SETTING_AUTO_EXPAND_REPLICAS, "false", (value) -> {
final int min;
final int max;
if (Booleans.parseBoolean(value, true) == false) {
if (Booleans.isFalse(value)) {
return new AutoExpandReplicas(0, 0, false);
}
final int dash = value.indexOf('-');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1217,7 +1217,8 @@ public static IndexMetaData fromXContent(XContentParser parser) throws IOExcepti
* {@link #isIndexUsingShadowReplicas(org.elasticsearch.common.settings.Settings)}.
*/
public static boolean isOnSharedFilesystem(Settings settings) {
return settings.getAsBoolean(SETTING_SHARED_FILESYSTEM, isIndexUsingShadowReplicas(settings));
Version version = settings.getAsVersion(SETTING_VERSION_CREATED, Version.CURRENT);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure it is a good idea to have a default here? This shouldn't be missing in production, right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I missed that one and will change it.

return settings.getAsBooleanLenientForPreEs6Indices(version, SETTING_SHARED_FILESYSTEM, isIndexUsingShadowReplicas(settings));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't the settings object here already have access to the Version by retrieving the version inside the getAsBooleanLenientForPreEs6Indices method, so it can be skipped retrieving it outside of the method?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe an additional arity version that didn't require the version and automatically looked it up from itself, that could save you some lookups (if you want)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I use the indexCreatedVersion of IndexMetaData now (just had to turn this into an instance method) and have also provided an overloaded version without parameters.

}

/**
Expand All @@ -1226,7 +1227,8 @@ public static boolean isOnSharedFilesystem(Settings settings) {
* setting for this is <code>false</code>.
*/
public static boolean isIndexUsingShadowReplicas(Settings settings) {
return settings.getAsBoolean(SETTING_SHADOW_REPLICAS, false);
Version version = settings.getAsVersion(SETTING_VERSION_CREATED, Version.CURRENT);
return settings.getAsBooleanLenientForPreEs6Indices(version, SETTING_SHADOW_REPLICAS, false);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import java.io.IOException;
import java.util.Map;

import static org.elasticsearch.common.xcontent.support.XContentMapValues.lenientNodeBooleanValue;
import static org.elasticsearch.common.xcontent.support.XContentMapValues.nodeBooleanValue;

/**
* Mapping configuration for a type.
Expand Down Expand Up @@ -95,10 +95,6 @@ public MappingMetaData(CompressedXContent mapping) throws IOException {
initMappers((Map<String, Object>) mappingMap.get(this.type));
}

public MappingMetaData(Map<String, Object> mapping) throws IOException {
this(mapping.keySet().iterator().next(), mapping);
}

public MappingMetaData(String type, Map<String, Object> mapping) throws IOException {
this.type = type;
XContentBuilder mappingBuilder = XContentFactory.jsonBuilder().map(mapping);
Expand Down Expand Up @@ -127,7 +123,12 @@ private void initMappers(Map<String, Object> withoutType) {
String fieldName = entry.getKey();
Object fieldNode = entry.getValue();
if (fieldName.equals("required")) {
required = lenientNodeBooleanValue(fieldNode);
try {
required = nodeBooleanValue(fieldNode);
} catch (IllegalArgumentException ex) {
throw new IllegalArgumentException("Failed to create mapping for type [" + this.type() + "]. " +
"Illegal value in field [_routing.required].", ex);
}
}
}
this.routing = new Routing(required);
Expand Down
Loading