Skip to content

Commit

Permalink
feat(export/jsonschema): supports array type (datacontract#200)
Browse files Browse the repository at this point in the history
  • Loading branch information
nyeongna authored May 15, 2024
1 parent caed695 commit 63ddf0f
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 2 deletions.
2 changes: 2 additions & 0 deletions datacontract/export/jsonschema_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ def to_property(field: Field) -> dict:
else:
property["properties"] = to_properties(field.fields)
property["required"] = to_required(field.fields)
if json_type == "array":
property["items"] = to_property(field.items)

if field.pattern:
property["pattern"] = field.pattern
Expand Down
14 changes: 12 additions & 2 deletions tests/fixtures/local-json-complex/data/sts_data.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
{
"array_test_string": ["test1", "test2"],
"array_test_object": [
{
"key": "key1",
"value": "value1"
},
{
"key": "key2",
"value": "value2"
}
],
"id": "11111111",
"sts_data": {
"connection_test": "SUCCESS",
Expand All @@ -11,5 +22,4 @@
}
}
}

}
}
30 changes: 30 additions & 0 deletions tests/fixtures/local-json-complex/datacontract.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,25 @@ models:
sts_data:
type: table
fields:
array_test_string:
type: array
required: true
items:
type: string
required: true
array_test_object:
type: array
required: true
items:
type: object
required: true
fields:
key:
type: string
required: true
value:
type: string
required: true
id:
type: string
minLength: 1
Expand Down Expand Up @@ -38,6 +57,17 @@ examples:
model: sts_data
data: | # expressed as string or inline yaml or via "$ref: data.csv"
{
"array_test_string": ["test1", "test2"],
"array_test_object": [
{
"key": "key1",
"value": "value1"
},
{
"key": "key2",
"value": "value2"
}
],
"id": "11111111",
"sts_data": {
"connection_test": "SUCCESS",
Expand Down
26 changes: 26 additions & 0 deletions tests/test_export_jsonschema.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,30 @@ def test_to_jsonschemas_complex_2():
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"array_test_string": {
"type": "array",
"items": {
"type": "string"
}
},
"array_test_object": {
"type": "array",
"items": {
"type": "object",
"properties": {
"key": {
"type": "string"
},
"value": {
"type": "string"
}
},
"required": [
"key",
"value"
]
}
},
"id": {
"type": "string",
"pattern": "^[0-9]{8}$",
Expand Down Expand Up @@ -221,6 +245,8 @@ def test_to_jsonschemas_complex_2():
}
},
"required": [
"array_test_string",
"array_test_object",
"id",
"sts_data"
]
Expand Down

0 comments on commit 63ddf0f

Please sign in to comment.