Skip to content

Commit

Permalink
Calculate additional codelist values for schema using anyOf, like OCD…
Browse files Browse the repository at this point in the history
  • Loading branch information
jpmckinney authored and odscjames committed Nov 8, 2023
1 parent b2c6851 commit c6366fa
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Restore jsonschema's type validator, as its performance has improved in recent Python versions https://github.com/OpenDataServices/lib-cove/pull/127
- Allow `SchemaJsonMixin` classes to define a `validator` method, that accepts lib-cove's JSON Schema draft 4 validator class and its format checker, and returns a validator instance. https://github.com/OpenDataServices/lib-cove/pull/128

### Fixed

- Calculate additional codelist values for schema using `anyOf`, like OCDS record packages https://github.com/open-contracting/lib-cove-ocds/issues/106

## [0.31.0] - 2023-07-06

### Changed
Expand Down
29 changes: 19 additions & 10 deletions libcove/lib/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,17 +456,26 @@ def get_schema_codelist_paths(
if "codelist" in value and path not in codelist_paths:
codelist_paths[path] = (value["codelist"], value.get("openCodelist", False))

if value.get("type") == "object":
get_schema_codelist_paths(None, value, path, codelist_paths)
elif value.get("type") == "array" and isinstance(value.get("items"), dict):
if value.get("items").get("type") == "string":
if "codelist" in value["items"] and path not in codelist_paths:
codelist_paths[path] = (
value["items"]["codelist"],
value["items"].get("openCodelist", False),
descendants = []
if "oneOf" in value and isinstance(value["oneOf"], list):
descendants = value["oneOf"]
else:
descendants = [value]

for value in descendants:
if value.get("type") == "object":
get_schema_codelist_paths(None, value, path, codelist_paths)
elif value.get("type") == "array" and isinstance(value.get("items"), dict):
if value.get("items").get("type") == "string":
if "codelist" in value["items"] and path not in codelist_paths:
codelist_paths[path] = (
value["items"]["codelist"],
value["items"].get("openCodelist", False),
)
elif value.get("items").get("properties"):
get_schema_codelist_paths(
None, value["items"], path, codelist_paths
)
elif value.get("items").get("properties"):
get_schema_codelist_paths(None, value["items"], path, codelist_paths)

return codelist_paths

Expand Down
46 changes: 46 additions & 0 deletions tests/lib/fixtures/common/schema_with_oneof_codelists.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"id": "bods-package.json",
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"data": {
"oneOf": [
{
"type": "object",
"properties": {
"entityType": {
"enum": [
"registeredEntity",
"legalEntity",
"arrangement",
"anonymousEntity",
"unknownEntity"
],
"openCodelist": false,
"title": "Type",
"type": "string",
"codelist": "currency.csv"
}
}
},
{
"type": "object",
"properties": {
"entityType": {
"enum": [
"registeredEntity",
"legalEntity",
"arrangement",
"anonymousEntity",
"unknownEntity"
],
"type": "string"
}
}
}
]
}
},
"type": "array",
"version": "0.1"
}
29 changes: 29 additions & 0 deletions tests/lib/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -1347,3 +1347,32 @@ def test_get_additional_codelist_values():
"extension_codelist": False,
},
}


def test_get_additional_codelist_values_anyOf():
json_data = {"data": {"entityType": "additional"} }

schema_obj = SchemaJsonMixin()
schema_obj.schema_host = os.path.join(
os.path.dirname(os.path.realpath(__file__)), "fixtures", "common/"
)
schema_obj.release_pkg_schema_name = f"schema_with_oneof_codelists.json"
schema_obj.pkg_schema_url = os.path.join(
schema_obj.schema_host, schema_obj.release_pkg_schema_name
)
schema_obj.codelists = "https://raw.githubusercontent.com/open-contracting/standard/1.1/schema/codelists/"

additional_codelist_values = get_additional_codelist_values(schema_obj, json_data)

assert additional_codelist_values == {
'data/entityType': {
'path': 'data',
'field': 'entityType',
'codelist': 'currency.csv',
'codelist_url': 'https://raw.githubusercontent.com/open-contracting/standard/1.1/schema/codelists/currency.csv',
'codelist_amend_urls': [],
'isopen': False,
'values': ['additional'],
'extension_codelist': False
}
}

0 comments on commit c6366fa

Please sign in to comment.