utils: normalize schema before validation (#86422)
gitea/passerelle/pipeline/head This commit looks good Details

To allow using lazy strings in JSON schemas we cast lazy strings to str
before validating the schema itself. The resulting validator is cached.
This commit is contained in:
Benjamin Dauvergne 2024-02-01 17:07:11 +01:00
parent f2b64b6ebf
commit a1d4c44ac4
1 changed files with 15 additions and 3 deletions

View File

@ -209,11 +209,23 @@ def datasource_schema():
VALIDATOR_CACHE = {}
def normalize_schema(schema):
'''Remove lazy strings'''
if isinstance(schema, dict):
return {k: normalize_schema(v) for k, v in schema.items()}
if isinstance(schema, list):
return [normalize_schema(v) for v in schema]
if hasattr(schema, '_delegate_text'):
return str(schema)
return schema
def get_validator_for_schema(schema):
if id(schema) not in VALIDATOR_CACHE:
validator_cls = validators.validator_for(schema)
validator_cls.check_schema(schema)
validator = validator_cls(schema)
normalized_schema = normalize_schema(schema)
validator_cls = validators.validator_for(normalized_schema)
validator_cls.check_schema(normalized_schema)
validator = validator_cls(normalized_schema)
VALIDATOR_CACHE[id(schema)] = validator
return VALIDATOR_CACHE[id(schema)]