Skip to content

Commit

Permalink
Added initial support for meta:enum (#228)
Browse files Browse the repository at this point in the history
* Added initial support for meta:enum

Signed-off-by: Steve Springett <[email protected]>

* Added optional Markdown support for descriptions from meta:enum

Signed-off-by: Steve Springett <[email protected]>

---------

Signed-off-by: Steve Springett <[email protected]>
  • Loading branch information
stevespringett authored May 1, 2024
1 parent f9c15fd commit 1e17ab3
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 13 deletions.
6 changes: 6 additions & 0 deletions json_schema_for_humans/jinja_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions json_schema_for_humans/schema/schema_keyword.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class SchemaKeyword(Enum):
PATTERN = "pattern"
CONST = "const"
ENUM = "enum"
META_ENUM = "meta:enum"
ELSE = "else"
THEN = "then"
IF = "if"
Expand Down
13 changes: 13 additions & 0 deletions json_schema_for_humans/schema/schema_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions json_schema_for_humans/template_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 12 additions & 6 deletions json_schema_for_humans/templates/flat/content.html
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,18 @@
{# Enum and const #}
{%- if schema.kw_enum -%}
<div class="enum-value" id="{{ schema.kw_enum.html_id }}">
<h4>Must be one of:</h4>
<ul class="list-group">
{%- for enum_choice in schema.kw_enum.array_items -%}
<li class="list-group-item enum-item">{{ enum_choice.literal | python_to_json }}</li>
{%- endfor -%}
</ul>
<h4>Must be one of:</h4>
<ul class="list-group">
{%- for enum_choice in schema.kw_enum.array_items -%}
<li class="list-group-item enum-item">
{%- if schema.kw_meta_enum -%}<strong>{%- endif -%}
{{ enum_choice.literal | python_to_json }}
{%- if schema.kw_meta_enum -%}</strong>
: {{ (schema.enum_description(enum_choice.literal) | get_description_literal) }}
{%- endif -%}
</li>
{%- endfor -%}
</ul>
</div>
{%- endif -%}
{%- if schema.kw_const -%}
Expand Down
8 changes: 7 additions & 1 deletion json_schema_for_humans/templates/js/content.html
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,13 @@
<h4>Must be one of:</h4>
<ul class="list-group">
{%- for enum_choice in schema.kw_enum.array_items -%}
<li class="list-group-item enum-item">{{ enum_choice.literal | python_to_json }}</li>
<li class="list-group-item enum-item">
{%- if schema.kw_meta_enum -%}<strong>{%- endif -%}
{{ enum_choice.literal | python_to_json }}
{%- if schema.kw_meta_enum -%}</strong>
: {{ (schema.enum_description(enum_choice.literal) | get_description_literal) }}
{%- endif -%}
</li>
{%- endfor -%}
</ul>
</div>
Expand Down
18 changes: 12 additions & 6 deletions json_schema_for_humans/templates/js_offline/content.html
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,18 @@
{# Enum and const #}
{%- if schema.kw_enum -%}
<div class="enum-value" id="{{ schema.kw_enum.html_id }}">
<h4>Must be one of:</h4>
<ul class="list-group">
{%- for enum_choice in schema.kw_enum.array_items -%}
<li class="list-group-item enum-item">{{ enum_choice.literal | python_to_json }}</li>
{%- endfor -%}
</ul>
<h4>Must be one of:</h4>
<ul class="list-group">
{%- for enum_choice in schema.kw_enum.array_items -%}
<li class="list-group-item enum-item">
{%- if schema.kw_meta_enum -%}<strong>{%- endif -%}
{{ enum_choice.literal | python_to_json }}
{%- if schema.kw_meta_enum -%}</strong>
: {{ (schema.enum_description(enum_choice.literal) | get_description_literal) }}
{%- endif -%}
</li>
{%- endfor -%}
</ul>
</div>
{%- endif -%}
{%- if schema.kw_const -%}
Expand Down

0 comments on commit 1e17ab3

Please sign in to comment.