Skip to content

Commit

Permalink
compile: Process anyOf and allOf and well as oneOf
Browse files Browse the repository at this point in the history
  • Loading branch information
jarofgreen committed Nov 2, 2021
1 parent 232af28 commit 8b8cd7b
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

### Added

- Process anyOf and allOf and well as oneOf https://github.com/OpenDataServices/compile-to-json-schema/issues/28

## [0.4.0] - 2021-07-30

### Added
Expand Down
8 changes: 8 additions & 0 deletions compiletojsonschema/compiletojsonschema.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ def __process(self, source):
for idx, data in enumerate(list(source["oneOf"])):
out["oneOf"][idx] = self.__process(source["oneOf"][idx])

if "anyOf" in source:
for idx, data in enumerate(list(source["anyOf"])):
out["anyOf"][idx] = self.__process(source["anyOf"][idx])

if "allOf" in source:
for idx, data in enumerate(list(source["allOf"])):
out["allOf"][idx] = self.__process(source["allOf"][idx])

if "codelist" in source and (
"openCodelist" not in source or not source["openCodelist"]
):
Expand Down
21 changes: 21 additions & 0 deletions tests/fixtures/simple/file-list-allof.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"id": "file-list-allof.json",
"$schema": "http://json-schema.org/draft-04/schema#",
"version": "0.1",
"type": "array",
"items": {
"allOf": [
{
"$ref": "file-list-component-part1.json"
},
{
"type": "object",
"properties": {
"pets": {
"type": "string"
}
}
}
]
}
}
6 changes: 3 additions & 3 deletions tests/fixtures/simple/file-list-anyof.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
"version": "0.1",
"type": "array",
"items": {
"oneOf": [
"anyOf": [
{
"$ref": "file-list-anyof-part1.json"
"$ref": "file-list-component-part1.json"
},
{
"$ref": "file-list-anyof-part2.json"
"$ref": "file-list-component-part2.json"
}
]
}
Expand Down
16 changes: 16 additions & 0 deletions tests/fixtures/simple/file-list-oneof.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"id": "file-list-oneof.json",
"$schema": "http://json-schema.org/draft-04/schema#",
"version": "0.1",
"type": "array",
"items": {
"oneOf": [
{
"$ref": "file-list-component-part1.json"
},
{
"$ref": "file-list-component-part2.json"
}
]
}
}
45 changes: 45 additions & 0 deletions tests/test_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,31 @@ def test_file_list_anyof():
out_string = ctjs.get_as_string()
out = json.loads(out_string)

assert out["items"]["anyOf"][0]["properties"]["address"]["title"] == "Home Address"
assert (
out["items"]["anyOf"][0]["properties"]["address"]["description"]
== "Where the person lives"
)
assert out["items"]["anyOf"][1]["properties"]["address"]["title"] == "Work Address"
assert (
out["items"]["anyOf"][1]["properties"]["address"]["description"]
== "Where the person works"
)


def test_file_list_oneof():

input_filename = os.path.join(
os.path.dirname(os.path.realpath(__file__)),
"fixtures",
"simple",
"file-list-oneof.json",
)

ctjs = CompileToJsonSchema(input_filename=input_filename)
out_string = ctjs.get_as_string()
out = json.loads(out_string)

assert out["items"]["oneOf"][0]["properties"]["address"]["title"] == "Home Address"
assert (
out["items"]["oneOf"][0]["properties"]["address"]["description"]
Expand All @@ -90,6 +115,26 @@ def test_file_list_anyof():
)


def test_file_list_allof():

input_filename = os.path.join(
os.path.dirname(os.path.realpath(__file__)),
"fixtures",
"simple",
"file-list-allof.json",
)

ctjs = CompileToJsonSchema(input_filename=input_filename)
out_string = ctjs.get_as_string()
out = json.loads(out_string)

assert out["items"]["allOf"][0]["properties"]["address"]["title"] == "Home Address"
assert (
out["items"]["allOf"][0]["properties"]["address"]["description"]
== "Where the person lives"
)


def test_passing_empty_schema_is_ok():
ctjs = CompileToJsonSchema(input_schema={})
assert "{}" == ctjs.get_as_string()

0 comments on commit 8b8cd7b

Please sign in to comment.