Skip to content

Commit

Permalink
[Connector API] Support numeric for configuration select option value…
Browse files Browse the repository at this point in the history
… type (#107059) (#107098)
  • Loading branch information
jedrazb committed Apr 4, 2024
1 parent 9da2b3d commit a28a31a
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 8 deletions.
5 changes: 5 additions & 0 deletions 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: []
Expand Up @@ -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:
Expand Down
Expand Up @@ -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;
Expand All @@ -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");
Expand All @@ -43,12 +45,19 @@ public ConfigurationSelectOption(StreamInput in) throws IOException {
private static final ConstructingObjectParser<ConfigurationSelectOption, Void> 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
Expand Down Expand Up @@ -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
Expand All @@ -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;
}
Expand Down
Expand Up @@ -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("""
{
Expand Down

0 comments on commit a28a31a

Please sign in to comment.