diff --git a/json_schema_for_humans/jinja_filters.py b/json_schema_for_humans/jinja_filters.py index 3d1a603f..c8dd31a5 100644 --- a/json_schema_for_humans/jinja_filters.py +++ b/json_schema_for_humans/jinja_filters.py @@ -89,6 +89,12 @@ def python_to_json(value: Any) -> Any: def get_description(env: Environment, schema_node: SchemaNode) -> str: """Filter. Get the description of a property or an empty string""" description = schema_node.description + return get_description_literal(env, description) + + +@pass_environment +def get_description_literal(env: Environment, description: str) -> str: + """Filter. Get the description of a property or an empty string""" config: GenerationConfiguration = env.globals["jsfh_config"] if config.default_from_description: diff --git a/json_schema_for_humans/schema/schema_keyword.py b/json_schema_for_humans/schema/schema_keyword.py index 1bd977d4..3f07e8b8 100644 --- a/json_schema_for_humans/schema/schema_keyword.py +++ b/json_schema_for_humans/schema/schema_keyword.py @@ -17,6 +17,7 @@ class SchemaKeyword(Enum): PATTERN = "pattern" CONST = "const" ENUM = "enum" + META_ENUM = "meta:enum" ELSE = "else" THEN = "then" IF = "if" diff --git a/json_schema_for_humans/schema/schema_node.py b/json_schema_for_humans/schema/schema_node.py index eddcf59b..4fdb39c0 100644 --- a/json_schema_for_humans/schema/schema_node.py +++ b/json_schema_for_humans/schema/schema_node.py @@ -345,6 +345,10 @@ def kw_else(self) -> Optional["SchemaNode"]: def kw_enum(self) -> Optional["SchemaNode"]: return self.get_keyword(SchemaKeyword.ENUM) + @property + def kw_meta_enum(self) -> Optional["SchemaNode"]: + return self.get_keyword(SchemaKeyword.META_ENUM) + @property def kw_const(self) -> Optional["SchemaNode"]: return self.get_keyword(SchemaKeyword.CONST) @@ -465,6 +469,15 @@ def is_object(self) -> bool: return any(node.literal == const.TYPE_OBJECT for node in self.kw_type.array_items) return False + def enum_description(self, value: str) -> Optional["SchemaNode"]: + meta_enum_node = self.get_keyword(SchemaKeyword.META_ENUM) + if not meta_enum_node: + return None + description = meta_enum_node.raw.get(value) + if not description: + return None + return description + @property def raw(self) -> Optional[Union[int, bool, str, List, Dict]]: """Get the value of the node as it would exist in the original schema. diff --git a/json_schema_for_humans/template_renderer.py b/json_schema_for_humans/template_renderer.py index 4b3b4325..6245916d 100644 --- a/json_schema_for_humans/template_renderer.py +++ b/json_schema_for_humans/template_renderer.py @@ -47,6 +47,7 @@ def _get_jinja_template(self) -> Template: ) env.filters["get_type_name"] = templating_utils.get_type_name env.filters["get_description"] = jinja_filters.get_description + env.filters["get_description_literal"] = jinja_filters.get_description_literal env.filters["get_numeric_restrictions_text"] = jinja_filters.get_numeric_restrictions_text env.filters["get_required_properties"] = jinja_filters.get_required_properties diff --git a/json_schema_for_humans/templates/flat/content.html b/json_schema_for_humans/templates/flat/content.html index 0349f92a..5c47142b 100644 --- a/json_schema_for_humans/templates/flat/content.html +++ b/json_schema_for_humans/templates/flat/content.html @@ -58,12 +58,18 @@ {# Enum and const #} {%- if schema.kw_enum -%}
-

Must be one of:

- +

Must be one of:

+
{%- endif -%} {%- if schema.kw_const -%} diff --git a/json_schema_for_humans/templates/js/content.html b/json_schema_for_humans/templates/js/content.html index 35d83975..2845855d 100644 --- a/json_schema_for_humans/templates/js/content.html +++ b/json_schema_for_humans/templates/js/content.html @@ -69,7 +69,13 @@

Must be one of:

diff --git a/json_schema_for_humans/templates/js_offline/content.html b/json_schema_for_humans/templates/js_offline/content.html index 35d83975..e08c4e22 100644 --- a/json_schema_for_humans/templates/js_offline/content.html +++ b/json_schema_for_humans/templates/js_offline/content.html @@ -66,12 +66,18 @@ {# Enum and const #} {%- if schema.kw_enum -%}
-

Must be one of:

- +

Must be one of:

+
{%- endif -%} {%- if schema.kw_const -%}