From a28a31a5a0e7c101aefdb7c0154c3b98b71e809a Mon Sep 17 00:00:00 2001 From: Jedr Blaszyk Date: Thu, 4 Apr 2024 14:49:25 +0200 Subject: [PATCH] [Connector API] Support numeric for configuration select option value type (#107059) (#107098) --- docs/changelog/107059.yaml | 5 ++ .../335_connector_update_configuration.yml | 40 ++++++++++++++++ .../ConfigurationSelectOption.java | 25 ++++++---- .../ConnectorConfigurationTests.java | 48 +++++++++++++++++++ 4 files changed, 110 insertions(+), 8 deletions(-) create mode 100644 docs/changelog/107059.yaml diff --git a/docs/changelog/107059.yaml b/docs/changelog/107059.yaml new file mode 100644 index 0000000000000..6c7ee48f9b53b --- /dev/null +++ b/docs/changelog/107059.yaml @@ -0,0 +1,5 @@ +pr: 107059 +summary: "[Connector API] Support numeric for configuration select option value type" +area: Application +type: bug +issues: [] diff --git a/x-pack/plugin/ent-search/qa/rest/src/yamlRestTest/resources/rest-api-spec/test/entsearch/335_connector_update_configuration.yml b/x-pack/plugin/ent-search/qa/rest/src/yamlRestTest/resources/rest-api-spec/test/entsearch/335_connector_update_configuration.yml index 0bde4bafaffd4..418a3cf6de94a 100644 --- a/x-pack/plugin/ent-search/qa/rest/src/yamlRestTest/resources/rest-api-spec/test/entsearch/335_connector_update_configuration.yml +++ b/x-pack/plugin/ent-search/qa/rest/src/yamlRestTest/resources/rest-api-spec/test/entsearch/335_connector_update_configuration.yml @@ -161,6 +161,46 @@ setup: - match: { configuration.some_field.tooltip: null } +--- +"Update Connector Configuration with numeric select options": + - do: + connector.update_configuration: + connector_id: test-connector + body: + configuration: + some_field: + default_value: null + depends_on: + - field: some_field + value: 31 + display: numeric + label: Very important field + options: + - label: ten + value: 10 + - label: five + value: 5 + order: 4 + required: true + sensitive: false + tooltip: null + type: str + ui_restrictions: [ ] + validations: + - constraint: 0 + type: greater_than + value: 123 + + + - match: { result: updated } + + - do: + connector.get: + connector_id: test-connector + + - match: { configuration.some_field.options.0.value: 10 } + - match: { configuration.some_field.options.1.value: 5 } + --- "Update Connector Configuration - Connector doesn't exist": - do: diff --git a/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/connector/configuration/ConfigurationSelectOption.java b/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/connector/configuration/ConfigurationSelectOption.java index 3c17f97ead51d..9728faaac3dd4 100644 --- a/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/connector/configuration/ConfigurationSelectOption.java +++ b/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/connector/configuration/ConfigurationSelectOption.java @@ -11,9 +11,11 @@ import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ObjectParser; import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentParseException; import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; @@ -25,16 +27,16 @@ public class ConfigurationSelectOption implements Writeable, ToXContentObject { private final String label; - private final String value; + private final Object value; - private ConfigurationSelectOption(String label, String value) { + private ConfigurationSelectOption(String label, Object value) { this.label = label; this.value = value; } public ConfigurationSelectOption(StreamInput in) throws IOException { this.label = in.readString(); - this.value = in.readString(); + this.value = in.readGenericValue(); } private static final ParseField LABEL_FIELD = new ParseField("label"); @@ -43,12 +45,19 @@ public ConfigurationSelectOption(StreamInput in) throws IOException { private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( "connector_configuration_select_option", true, - args -> new ConfigurationSelectOption.Builder().setLabel((String) args[0]).setValue((String) args[1]).build() + args -> new ConfigurationSelectOption.Builder().setLabel((String) args[0]).setValue(args[1]).build() ); static { PARSER.declareString(constructorArg(), LABEL_FIELD); - PARSER.declareString(constructorArg(), VALUE_FIELD); + PARSER.declareField(constructorArg(), (p, c) -> { + if (p.currentToken() == XContentParser.Token.VALUE_STRING) { + return p.text(); + } else if (p.currentToken() == XContentParser.Token.VALUE_NUMBER) { + return p.numberValue(); + } + throw new XContentParseException("Unsupported token [" + p.currentToken() + "]"); + }, VALUE_FIELD, ObjectParser.ValueType.VALUE); } @Override @@ -76,7 +85,7 @@ public static ConfigurationSelectOption fromXContent(XContentParser parser) thro @Override public void writeTo(StreamOutput out) throws IOException { out.writeString(label); - out.writeString(value); + out.writeGenericValue(value); } @Override @@ -95,14 +104,14 @@ public int hashCode() { public static class Builder { private String label; - private String value; + private Object value; public Builder setLabel(String label) { this.label = label; return this; } - public Builder setValue(String value) { + public Builder setValue(Object value) { this.value = value; return this; } diff --git a/x-pack/plugin/ent-search/src/test/java/org/elasticsearch/xpack/application/connector/ConnectorConfigurationTests.java b/x-pack/plugin/ent-search/src/test/java/org/elasticsearch/xpack/application/connector/ConnectorConfigurationTests.java index 3a7ff819ecbf5..caedb526b0b7e 100644 --- a/x-pack/plugin/ent-search/src/test/java/org/elasticsearch/xpack/application/connector/ConnectorConfigurationTests.java +++ b/x-pack/plugin/ent-search/src/test/java/org/elasticsearch/xpack/application/connector/ConnectorConfigurationTests.java @@ -89,6 +89,54 @@ public void testToXContent() throws IOException { assertToXContentEquivalent(originalBytes, toXContent(parsed, XContentType.JSON, humanReadable), XContentType.JSON); } + public void testToXContent_WithNumericSelectOptions() throws IOException { + String content = XContentHelper.stripWhitespace(""" + { + "default_value": null, + "depends_on": [ + { + "field": "some_field", + "value": true + } + ], + "display": "textbox", + "label": "Very important field", + "options": [ + { + "label": "five", + "value": 5 + }, + { + "label": "ten", + "value": 10 + } + ], + "order": 4, + "required": true, + "sensitive": false, + "tooltip": "Wow, this tooltip is useful.", + "type": "str", + "ui_restrictions": [], + "validations": [ + { + "constraint": 0, + "type": "greater_than" + } + ], + "value": "" + } + """); + + ConnectorConfiguration configuration = ConnectorConfiguration.fromXContentBytes(new BytesArray(content), XContentType.JSON); + boolean humanReadable = true; + BytesReference originalBytes = toShuffledXContent(configuration, XContentType.JSON, ToXContent.EMPTY_PARAMS, humanReadable); + ConnectorConfiguration parsed; + try (XContentParser parser = createParser(XContentType.JSON.xContent(), originalBytes)) { + parsed = ConnectorConfiguration.fromXContent(parser); + } + assertToXContentEquivalent(originalBytes, toXContent(parsed, XContentType.JSON, humanReadable), XContentType.JSON); + } + public void testToXContentCrawlerConfig_WithNullValue() throws IOException { String content = XContentHelper.stripWhitespace(""" {