Skip to content

Commit

Permalink
Add SAT test for no duplicates in enum properties (#19243)
Browse files Browse the repository at this point in the history
* Make pytest aware of backward compatibility mark

* Unit tests for duplicate values in enum properties

* Add docs about airbyte enforcements to enum usage

* Add docs info to test
  • Loading branch information
erohmensing authored and akashkulk committed Nov 17, 2022
1 parent f6bd354 commit 90697fb
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ testpaths =
markers =
default_timeout
slow: marks tests as slow (deselect with '-m "not slow"')
backward_compatibility
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,18 @@ def test_docker_env(self, actual_connector_spec: ConnectorSpecification, docker_
docker_runner.entry_point
), "env should be equal to space-joined entrypoint"

def test_enum_usage(self, actual_connector_spec: ConnectorSpecification):
"""Check that enum lists in specs contain distinct values."""
docs_url = "https://docs.airbyte.io/connector-development/connector-specification-reference"
docs_msg = f"See specification reference at {docs_url}."

schema_helper = JsonSchemaHelper(actual_connector_spec.connectionSpecification)
enum_paths = schema_helper.find_nodes(keys=["enum"])

for path in enum_paths:
enum_list = schema_helper.get_node(path)
assert len(set(enum_list)) == len(enum_list), f"Enum lists should not contain duplicate values. Misconfigured enum array: {enum_list}. {docs_msg}"

def test_oneof_usage(self, actual_connector_spec: ConnectorSpecification):
"""Check that if spec contains oneOf it follows the rules according to reference
https://docs.airbyte.io/connector-development/connector-specification-reference
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,49 @@ def test_oneof_usage(connector_spec, should_fail):
else:
t.test_oneof_usage(actual_connector_spec=ConnectorSpecification(connectionSpecification=connector_spec))

@parametrize_test_case(
{
"test_id": "successful",
"connector_spec": {
"type": "object",
"properties": {
"property_with_options": {
"title": "Property with options",
"description": "A property in the form of an enumerated list",
"type": "string",
"default": "Option 1",
"enum": ["Option 1", "Option 2", "Option 3"],
}
},
},
"should_fail": False,
},
{
"test_id": "duplicate_values",
"connector_spec": {
"type": "object",
"properties": {
"property_with_options": {
"title": "Property with options",
"description": "A property in the form of an enumerated list",
"type": "string",
"default": "Option 1",
"enum": ["Option 1", "Option 2", "Option 3", "Option 2"],
}
},
},
"should_fail": True,
},
)
def test_enum_usage(connector_spec, should_fail):
t = _TestSpec()
if should_fail is True:
with pytest.raises(AssertionError):
t.test_enum_usage(actual_connector_spec=ConnectorSpecification(connectionSpecification=connector_spec))
else:
t.test_enum_usage(actual_connector_spec=ConnectorSpecification(connectionSpecification=connector_spec))



@pytest.mark.parametrize(
"connector_spec, expected_error",
Expand Down
31 changes: 29 additions & 2 deletions docs/connector-development/connector-specification-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,14 @@ In this case, use the `"airbyte_hidden": true` keyword to hide that field from t
}
```

Results in the following form:

![hidden fields](../.gitbook/assets/spec_reference_hidden_field_screenshot.png)

Results in the following form:

## Airbyte Modifications to `jsonschema`

### Using `oneOf`s
### Using `oneOf`

In some cases, a connector needs to accept one out of many options. For example, a connector might need to know the compression codec of the file it will read, which will render in the Airbyte UI as a list of the available codecs. In JSONSchema, this can be expressed using the [oneOf](https://json-schema.org/understanding-json-schema/reference/combining.html#oneof) keyword.

Expand Down Expand Up @@ -183,3 +185,28 @@ In each item in the `oneOf` array, the `option_title` string field exists with t
}
```
### Using `enum`
In regular `jsonschema`, some drafts enforce that `enum` lists must contain distinct values, while others do not. For consistency, Airbyte enforces this restriction.
For example, this spec is invalid, since `a_format` is listed twice under the enumerated property `format`:
```javascript
{
"connection_specification": {
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "File Source Spec",
"type": "object",
"required": ["format"],
"properties": {
"dataset_name": {
...
},
"format": {
type: "string",
enum: ["a_format", "another_format", "a_format"]
},
}
}
}
```

0 comments on commit 90697fb

Please sign in to comment.