feat(ingest): handling for const in json schema (#9694)

Co-authored-by: Harshal Sheth <hsheth2@gmail.com>
This commit is contained in:
Aseem Bansal 2024-01-24 12:14:14 +05:30 committed by GitHub
parent c158d2b9ec
commit c4dec931a3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 19 additions and 4 deletions

View File

@ -316,10 +316,12 @@ class JsonSchemaTranslator:
@staticmethod
def _get_description_from_any_schema(schema: Dict) -> str:
# we do a redundant `if description in schema` check to guard against the scenario that schema is not a dictionary
description = (
(schema.get("description") or "") if "description" in schema else ""
)
description = ""
if "description" in schema:
description = str(schema.get("description"))
elif "const" in schema:
schema_const = schema.get("const")
description = f"Const value: {schema_const}"
if JsonSchemaTranslator._INJECT_DEFAULTS_INTO_DESCRIPTION:
default = schema.get("default")
if default is not None:

View File

@ -725,6 +725,19 @@ def test_non_str_enums():
assert fields[0].description == 'One of: "baz", 1, null'
def test_const_description_pulled_correctly():
schema = {
"$id": "test",
"$schema": "http://json-schema.org/draft-07/schema#",
"properties": {"bar": {"type": "string", "const": "not_defined"}},
}
fields = list(JsonSchemaTranslator.get_fields_from_schema(schema))
expected_field_paths: List[str] = ["[version=2.0].[type=object].[type=string].bar"]
assert_field_paths_match(fields, expected_field_paths)
assert fields[0].description == "Const value: not_defined"
def test_anyof_with_properties():
# We expect the event / timestamp fields to be included in both branches of the anyOf.