-
Notifications
You must be signed in to change notification settings - Fork 7
feat: Add support for all relevant keywords to get_schema_fields. Add name, deprecated_self, pattern, merge_by_id to Field. #194
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
… name, deprecated_self, pattern, merge_by_id to Field. - Rename definition_path to definition - Remove support for null schema, which are invalid JSON Schema and prohibited by OCDS - Remove definition_pointer, definition_pointer_components, definition_path_components from Field - Remove support for adding fields to Field.__dict__ test: Add benchmark and regression tests. Add manage.py script to generate regression scenarios.
Here's the version of multilingual = set()
nonmultilingual = []
if definitions := schema.get('definitions'):
for name, subschema in definitions.items():
# Not all definitions set ``properties``, e.g. ``FiscalBreakdownFieldMapping`` in budget_and_spend.
yield from get_schema_fields(subschema, pointer=f'/definitions/{name}', definition=name)
if pattern_properties := schema.get('patternProperties'):
for pattern, subschema in pattern_properties.items():
# The pattern might have an extra set of parentheses (OCDS 1.1). Assumes the final character is $.
for offset in (2, 1):
end = -LANGUAGE_CODE_SUFFIX_LEN - offset
# The pattern must be anchored and the suffix must occur at the end.
if (
pattern[end:-offset] == LANGUAGE_CODE_SUFFIX
and pattern[:offset] == '^('[:offset]
and pattern[-offset:] == ')$'[-offset:]
):
multilingual.add(pattern[offset:end])
break
# Set ``multilingual`` on corresponding ``properties``, instead of yielding these ``patternProperties``.
else:
# ``patternProperties`` never sets ``patternProperties``, ``properties``, ``items`` or ``oneOf``.
deprecated_self = _deprecated(subschema)
nonmultilingual.append(
Field(
name=pattern,
schema=subschema,
pointer=f'{pointer}/patternProperties/{pattern}',
path_components=(*path_components, pattern),
definition=definition,
deprecated_self=deprecated_self,
deprecated=deprecated or deprecated_self,
pattern=True,
)
)
if properties := schema.get('properties'):
for name, subschema in properties.items():
prop_pointer = f'{pointer}/properties/{name}'
prop_path_components = (*path_components, name)
prop_deprecated_self = _deprecated(subschema)
prop_deprecated = deprecated or prop_deprecated_self
prop_whole_list_merge = whole_list_merge or subschema.get('wholeListMerge', False)
yield Field(
name=name,
schema=subschema,
pointer=prop_pointer,
path_components=prop_path_components,
definition=definition,
deprecated_self=prop_deprecated_self,
deprecated=prop_deprecated,
multilingual=name in multilingual,
required=name in schema.get('required', []),
merge_by_id=name == 'id' and array and not prop_whole_list_merge,
)
# If an extension removes a property.
if subschema is None:
continue
# This guard isn't necessary, but it improves performance.
if 'properties' in subschema or 'patternProperties' in subschema:
yield from get_schema_fields(
subschema, prop_pointer, prop_path_components, definition, prop_deprecated, prop_whole_list_merge
)
# To date, ``definitions``, ``patternProperties`` and ``items`` never set ``items``.
if (items := subschema.get('items')) and ('properties' in items or 'patternProperties' in items):
yield from get_schema_fields(
items,
f'{prop_pointer}/items',
prop_path_components,
definition,
prop_deprecated,
prop_whole_list_merge,
array=True,
)
# To date, ``definitions``, ``patternProperties`` and ``items`` never set ``oneOf``.
if one_ofs := subschema.get('oneOf'):
for i, one_of in enumerate(one_ofs):
# To date, ``oneOf`` never sets ``patternProperties`` or ``properties``.
if (items := one_of.get('items')) and ('properties' in items or 'patternProperties' in items):
yield from get_schema_fields(
items,
f'{prop_pointer}/anyOf/{i}/items',
prop_path_components,
definition,
prop_deprecated,
prop_whole_list_merge, # To date, ``oneOf`` never sets ``wholeListMerge``
array=True,
)
# Yield ``patternProperties`` last, to be interpreted in the context of ``properties``.
for field in nonmultilingual:
yield field |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Notes:
Expected warnings when creating the versioned release schema in manage.py:
["integer", "string"]
["integer", "string", "null"]
["number"]
["number", "string", "null"]
["boolean", "number", "string", "null"]
["boolean"]
["integer"]
[]
items
type["string", "null"]