Skip to content

Commit 7097c0c

Browse files
author
James (ODSC)
authored
Merge pull request #29 from OpenDataServices/28-anyof-allof
28 anyof allof
2 parents 232af28 + 1898abe commit 7097c0c

9 files changed

+98
-4
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ jobs:
66
runs-on: ubuntu-latest
77
strategy:
88
matrix:
9-
python-version: [ '3.5', '3.6', '3.7', '3.8']
9+
python-version: [ '3.5', '3.6', '3.7', '3.8', '3.9', '3.10']
1010
steps:
1111
- uses: actions/checkout@v2
1212
- name: Setup python

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
66

77
## [Unreleased]
88

9+
### Added
10+
11+
- Process anyOf and allOf and well as oneOf https://github.com/OpenDataServices/compile-to-json-schema/issues/28
12+
913
## [0.4.0] - 2021-07-30
1014

1115
### Added

compiletojsonschema/compiletojsonschema.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ def __process(self, source):
7272
for idx, data in enumerate(list(source["oneOf"])):
7373
out["oneOf"][idx] = self.__process(source["oneOf"][idx])
7474

75+
if "anyOf" in source:
76+
for idx, data in enumerate(list(source["anyOf"])):
77+
out["anyOf"][idx] = self.__process(source["anyOf"][idx])
78+
79+
if "allOf" in source:
80+
for idx, data in enumerate(list(source["allOf"])):
81+
out["allOf"][idx] = self.__process(source["allOf"][idx])
82+
7583
if "codelist" in source and (
7684
"openCodelist" not in source or not source["openCodelist"]
7785
):
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"id": "file-list-allof.json",
3+
"$schema": "http://json-schema.org/draft-04/schema#",
4+
"version": "0.1",
5+
"type": "array",
6+
"items": {
7+
"allOf": [
8+
{
9+
"$ref": "file-list-component-part1.json"
10+
},
11+
{
12+
"type": "object",
13+
"properties": {
14+
"pets": {
15+
"type": "string"
16+
}
17+
}
18+
}
19+
]
20+
}
21+
}

tests/fixtures/simple/file-list-anyof.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
"version": "0.1",
55
"type": "array",
66
"items": {
7-
"oneOf": [
7+
"anyOf": [
88
{
9-
"$ref": "file-list-anyof-part1.json"
9+
"$ref": "file-list-component-part1.json"
1010
},
1111
{
12-
"$ref": "file-list-anyof-part2.json"
12+
"$ref": "file-list-component-part2.json"
1313
}
1414
]
1515
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"id": "file-list-oneof.json",
3+
"$schema": "http://json-schema.org/draft-04/schema#",
4+
"version": "0.1",
5+
"type": "array",
6+
"items": {
7+
"oneOf": [
8+
{
9+
"$ref": "file-list-component-part1.json"
10+
},
11+
{
12+
"$ref": "file-list-component-part2.json"
13+
}
14+
]
15+
}
16+
}

tests/test_simple.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,31 @@ def test_file_list_anyof():
7878
out_string = ctjs.get_as_string()
7979
out = json.loads(out_string)
8080

81+
assert out["items"]["anyOf"][0]["properties"]["address"]["title"] == "Home Address"
82+
assert (
83+
out["items"]["anyOf"][0]["properties"]["address"]["description"]
84+
== "Where the person lives"
85+
)
86+
assert out["items"]["anyOf"][1]["properties"]["address"]["title"] == "Work Address"
87+
assert (
88+
out["items"]["anyOf"][1]["properties"]["address"]["description"]
89+
== "Where the person works"
90+
)
91+
92+
93+
def test_file_list_oneof():
94+
95+
input_filename = os.path.join(
96+
os.path.dirname(os.path.realpath(__file__)),
97+
"fixtures",
98+
"simple",
99+
"file-list-oneof.json",
100+
)
101+
102+
ctjs = CompileToJsonSchema(input_filename=input_filename)
103+
out_string = ctjs.get_as_string()
104+
out = json.loads(out_string)
105+
81106
assert out["items"]["oneOf"][0]["properties"]["address"]["title"] == "Home Address"
82107
assert (
83108
out["items"]["oneOf"][0]["properties"]["address"]["description"]
@@ -90,6 +115,26 @@ def test_file_list_anyof():
90115
)
91116

92117

118+
def test_file_list_allof():
119+
120+
input_filename = os.path.join(
121+
os.path.dirname(os.path.realpath(__file__)),
122+
"fixtures",
123+
"simple",
124+
"file-list-allof.json",
125+
)
126+
127+
ctjs = CompileToJsonSchema(input_filename=input_filename)
128+
out_string = ctjs.get_as_string()
129+
out = json.loads(out_string)
130+
131+
assert out["items"]["allOf"][0]["properties"]["address"]["title"] == "Home Address"
132+
assert (
133+
out["items"]["allOf"][0]["properties"]["address"]["description"]
134+
== "Where the person lives"
135+
)
136+
137+
93138
def test_passing_empty_schema_is_ok():
94139
ctjs = CompileToJsonSchema(input_schema={})
95140
assert "{}" == ctjs.get_as_string()

0 commit comments

Comments
 (0)