Skip to content

Commit

Permalink
fix: const with value null wrongly displayed (#288)
Browse files Browse the repository at this point in the history
  • Loading branch information
dblanchette authored Dec 6, 2024
1 parent 50aab5b commit e2642b2
Show file tree
Hide file tree
Showing 20 changed files with 201 additions and 18 deletions.
4 changes: 4 additions & 0 deletions docs/examples/cases/complex_const.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
{
"title": "0",
"const": 0
},
{
"title": "null",
"const": null
}
]
}
39 changes: 39 additions & 0 deletions docs/examples/examples_flat_default/complex_const.html
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,45 @@ <h3 class="ml-2 mt-2"><a id="oneOf_i2" href="#oneOf_i2">0</a></h3>



</div>
</div><div class="card">
<h3 class="ml-2 mt-2"><a id="oneOf_i3" href="#oneOf_i3">null</a></h3>
<div class="card-body">


<div class="breadcrumbs">root
<svg width="1em" height="1em" viewBox="0 0 16 16" class="bi bi-arrow-right-short" fill="currentColor" xmlns="http://www.w3.org/2000/svg">
<path
fill-rule="evenodd"
d="M4 8a.5.5 0 0 1 .5-.5h5.793L8.146 5.354a.5.5 0 1 1 .708-.708l3 3a.5.5 0 0 1 0 .708l-3 3a.5.5 0 0 1-.708-.708L10.293 8.5H4.5A.5.5 0 0 1 4 8z"
/>
</svg>
<a href="#oneOf">oneOf</a>
<svg width="1em" height="1em" viewBox="0 0 16 16" class="bi bi-arrow-right-short" fill="currentColor" xmlns="http://www.w3.org/2000/svg">
<path
fill-rule="evenodd"
d="M4 8a.5.5 0 0 1 .5-.5h5.793L8.146 5.354a.5.5 0 1 1 .708-.708l3 3a.5.5 0 0 1 0 .708l-3 3a.5.5 0 0 1-.708-.708L10.293 8.5H4.5A.5.5 0 0 1 4 8z"
/>
</svg>
<a href="#oneOf_i3">null</a></div><span class="badge badge-dark value-type">Type: const</span><br/>











<span class="const-value" id="oneOf_i3_const">Specific value: <code>null</code></span>






</div>
</div></div>

Expand Down
31 changes: 31 additions & 0 deletions docs/examples/examples_js_default/complex_const.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@
id="oneOf_i2" data-toggle="tab" href="#tab-pane_oneOf_i2" role="tab"
onclick="setAnchor('#oneOf_i2')"
>0</a>
</li><li class="nav-item">
<a class="nav-link oneOf-option"
id="oneOf_i3" data-toggle="tab" href="#tab-pane_oneOf_i3" role="tab"
onclick="setAnchor('#oneOf_i3')"
>null</a>
</li></ul>
<div class="tab-content card"><div class="tab-pane fade card-body active show"
id="tab-pane_oneOf_i0" role="tabpanel">
Expand Down Expand Up @@ -121,6 +126,32 @@



</div><div class="tab-pane fade card-body "
id="tab-pane_oneOf_i3" role="tabpanel">


<div class="breadcrumbs">root
<svg width="1em" height="1em" viewBox="0 0 16 16" class="bi bi-arrow-right-short" fill="currentColor" xmlns="http://www.w3.org/2000/svg">
<path
fill-rule="evenodd"
d="M4 8a.5.5 0 0 1 .5-.5h5.793L8.146 5.354a.5.5 0 1 1 .708-.708l3 3a.5.5 0 0 1 0 .708l-3 3a.5.5 0 0 1-.708-.708L10.293 8.5H4.5A.5.5 0 0 1 4 8z"
/>
</svg>
<a href="#oneOf" onclick="anchorLink('oneOf')">oneOf</a>
<svg width="1em" height="1em" viewBox="0 0 16 16" class="bi bi-arrow-right-short" fill="currentColor" xmlns="http://www.w3.org/2000/svg">
<path
fill-rule="evenodd"
d="M4 8a.5.5 0 0 1 .5-.5h5.793L8.146 5.354a.5.5 0 1 1 .708-.708l3 3a.5.5 0 0 1 0 .708l-3 3a.5.5 0 0 1-.708-.708L10.293 8.5H4.5A.5.5 0 0 1 4 8z"
/>
</svg>
<a href="#oneOf_i3" onclick="anchorLink('oneOf_i3')">null</a></div><h4>null</h4><span class="badge badge-dark value-type">Type: const</span><br/>
<span class="const-value" id="oneOf_i3_const">Specific value: <code>null</code></span>






</div></div></div>


Expand Down
31 changes: 31 additions & 0 deletions docs/examples/examples_js_offline_default/complex_const.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@
id="oneOf_i2" data-toggle="tab" href="#tab-pane_oneOf_i2" role="tab"
onclick="setAnchor('#oneOf_i2')"
>0</a>
</li><li class="nav-item">
<a class="nav-link oneOf-option"
id="oneOf_i3" data-toggle="tab" href="#tab-pane_oneOf_i3" role="tab"
onclick="setAnchor('#oneOf_i3')"
>null</a>
</li></ul>
<div class="tab-content card"><div class="tab-pane fade card-body active show"
id="tab-pane_oneOf_i0" role="tabpanel">
Expand Down Expand Up @@ -121,6 +126,32 @@



</div><div class="tab-pane fade card-body "
id="tab-pane_oneOf_i3" role="tabpanel">


<div class="breadcrumbs">root
<svg width="1em" height="1em" viewBox="0 0 16 16" class="bi bi-arrow-right-short" fill="currentColor" xmlns="http://www.w3.org/2000/svg">
<path
fill-rule="evenodd"
d="M4 8a.5.5 0 0 1 .5-.5h5.793L8.146 5.354a.5.5 0 1 1 .708-.708l3 3a.5.5 0 0 1 0 .708l-3 3a.5.5 0 0 1-.708-.708L10.293 8.5H4.5A.5.5 0 0 1 4 8z"
/>
</svg>
<a href="#oneOf" onclick="anchorLink('oneOf')">oneOf</a>
<svg width="1em" height="1em" viewBox="0 0 16 16" class="bi bi-arrow-right-short" fill="currentColor" xmlns="http://www.w3.org/2000/svg">
<path
fill-rule="evenodd"
d="M4 8a.5.5 0 0 1 .5-.5h5.793L8.146 5.354a.5.5 0 1 1 .708-.708l3 3a.5.5 0 0 1 0 .708l-3 3a.5.5 0 0 1-.708-.708L10.293 8.5H4.5A.5.5 0 0 1 4 8z"
/>
</svg>
<a href="#oneOf_i3" onclick="anchorLink('oneOf_i3')">null</a></div><h4>null</h4><span class="badge badge-dark value-type">Type: const</span><br/>
<span class="const-value" id="oneOf_i3_const">Specific value: <code>null</code></span>






</div></div></div>


Expand Down
13 changes: 13 additions & 0 deletions docs/examples/examples_md_default/complex_const.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
- [1. Property `root > oneOf > 1st constant`](#oneOf_i0)
- [2. Property `root > oneOf > 2nd constant`](#oneOf_i1)
- [3. Property `root > oneOf > 0`](#oneOf_i2)
- [4. Property `root > oneOf > null`](#oneOf_i3)

| | |
| ------------ | ----------- |
Expand All @@ -14,6 +15,7 @@
| [1st constant](#oneOf_i0) |
| [2nd constant](#oneOf_i1) |
| [0](#oneOf_i2) |
| [null](#oneOf_i3) |

## <a name="oneOf_i0"></a>1. Property `root > oneOf > 1st constant`

Expand Down Expand Up @@ -54,6 +56,17 @@ Specific value: `{

Specific value: `0`

## <a name="oneOf_i3"></a>4. Property `root > oneOf > null`

**Title:** null

| | |
| ------------ | ------- |
| **Type** | `const` |
| **Required** | No |

Specific value: `null`

| | Array restrictions |
| -------------------- | ------------------ |
| **Min items** | N/A |
Expand Down
16 changes: 16 additions & 0 deletions docs/examples/examples_md_nested_default/complex_const.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
- [1. Property `root > oneOf > 1st constant`](#oneOf_i0)
- [2. Property `root > oneOf > 2nd constant`](#oneOf_i1)
- [3. Property `root > oneOf > 0`](#oneOf_i2)
- [4. Property `root > oneOf > null`](#oneOf_i3)

| | |
| ------------ | ----------- |
Expand All @@ -16,6 +17,7 @@
| [1st constant](#oneOf_i0) |
| [2nd constant](#oneOf_i1) |
| [0](#oneOf_i2) |
| [null](#oneOf_i3) |

<blockquote>

Expand Down Expand Up @@ -64,6 +66,20 @@ Specific value: `{

Specific value: `0`

</blockquote>
<blockquote>

## <a name="oneOf_i3"></a>4. Property `root > oneOf > null`

**Title:** null

| | |
| ------------ | ------- |
| **Type** | `const` |
| **Required** | No |

Specific value: `null`

</blockquote>

</blockquote>
Expand Down
15 changes: 15 additions & 0 deletions docs/examples/examples_md_nested_with_badges/complex_const.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
- [1. Property `root > oneOf > 1st constant`](#oneOf_i0)
- [2. Property `root > oneOf > 2nd constant`](#oneOf_i1)
- [3. Property `root > oneOf > 0`](#oneOf_i2)
- [4. Property `root > oneOf > null`](#oneOf_i3)

| | |
| -------- | ----------- |
Expand All @@ -15,6 +16,7 @@
| [1st constant](#oneOf_i0) |
| [2nd constant](#oneOf_i1) |
| [0](#oneOf_i2) |
| [null](#oneOf_i3) |

<blockquote>

Expand Down Expand Up @@ -60,6 +62,19 @@ Specific value: `{

Specific value: `0`

</blockquote>
<blockquote>

## <a name="oneOf_i3"></a>4. Property `root > oneOf > null`

**Title:** null

| | |
| -------- | ------- |
| **Type** | `const` |

Specific value: `null`

</blockquote>

</blockquote>
Expand Down
12 changes: 12 additions & 0 deletions docs/examples/examples_md_with_badges/complex_const.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
- [1. Property `root > oneOf > 1st constant`](#oneOf_i0)
- [2. Property `root > oneOf > 2nd constant`](#oneOf_i1)
- [3. Property `root > oneOf > 0`](#oneOf_i2)
- [4. Property `root > oneOf > null`](#oneOf_i3)

| | |
| -------- | ----------- |
Expand All @@ -13,6 +14,7 @@
| [1st constant](#oneOf_i0) |
| [2nd constant](#oneOf_i1) |
| [0](#oneOf_i2) |
| [null](#oneOf_i3) |

## <a name="oneOf_i0"></a>1. Property `root > oneOf > 1st constant`

Expand Down Expand Up @@ -50,6 +52,16 @@ Specific value: `{

Specific value: `0`

## <a name="oneOf_i3"></a>4. Property `root > oneOf > null`

**Title:** null

| | |
| -------- | ------- |
| **Type** | `const` |

Specific value: `null`

| | Array restrictions |
| -------------------- | ------------------ |
| **Min items** | N/A |
Expand Down
4 changes: 3 additions & 1 deletion json_schema_for_humans/jinja_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import yaml
from jinja2 import Environment, pass_environment
from markdown2 import Markdown # type: ignore
from markupsafe import escape as markupsafe_escape
from pygments import highlight
from pygments.formatters.html import HtmlFormatter
from pygments.lexers.data import YamlLexer
Expand Down Expand Up @@ -83,6 +82,9 @@ def python_to_json(value: Any) -> Any:
Used to display a string literals more explicitly for default and const values.
"""
if value is None:
return "null"

return json.dumps(value, indent=4, separators=(",", ": "), ensure_ascii=False)


Expand Down
2 changes: 2 additions & 0 deletions json_schema_for_humans/schema/intermediate_representation.py
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,8 @@ def _build_node(
new_node.array_additional_items = schema_value
elif schema_key == SchemaKeyword.ADDITIONAL_ITEMS.value:
new_node.array_additional_items = schema_value
elif schema_key == SchemaKeyword.CONST.value:
new_node.const = schema_value
else:
# Add the property name (correctly escaped) to the ID
new_depth = depth
Expand Down
18 changes: 18 additions & 0 deletions json_schema_for_humans/schema/schema_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
ALLOWED_KEYWORDS_WITH_REF = [SchemaKeyword.DESCRIPTION.value]


class NotSet:
"""A class to represent a value that is not set"""


class SchemaNode:
"""
Represents a part of a JSON schema with additional metadata to help with documentation
Expand Down Expand Up @@ -115,6 +119,7 @@ def __init__(
self.array_additional_items = array_additional_items
self.tuple_validation_items: List["SchemaNode"] = tuple_validation_items or []
self.property_name = property_name
self.const = NotSet

@property
def explicit_no_additional_properties(self) -> bool:
Expand Down Expand Up @@ -534,6 +539,19 @@ def should_be_a_link(self, config: GenerationConfiguration) -> bool:

return self.has_circular_reference(config)

@property
def is_const(self) -> bool:
"""Check if the node is a const node"""
return self.const is not NotSet

@property
def const_value(self) -> Any:
"""Get the value of the const node"""
if self.const is NotSet:
return None

return self.const

def node_is_parent(self, node_to_check: "SchemaNode") -> bool:
"""Check if the provided node is a parent of the current node"""
if self.file != node_to_check.file:
Expand Down
4 changes: 2 additions & 2 deletions json_schema_for_humans/templates/flat/content.html
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ <h4>Must be one of:</h4>
</ul>
</div>
{%- endif -%}
{%- if schema.kw_const -%}
<span class="const-value" id="{{ schema.kw_const.html_id }}">Specific value: <code>{{ schema.kw_const.raw | python_to_json }}</code></span>
{%- if schema.is_const -%}
<span class="const-value" id="{{ schema.html_id }}_const">Specific value: <code>{{ schema.const_value | python_to_json }}</code></span>
{%- endif -%}

{# Pattern (Regular Expression) #}
Expand Down
4 changes: 2 additions & 2 deletions json_schema_for_humans/templates/js/content.html
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ <h4>Must be one of:</h4>
</ul>
</div>
{%- endif -%}
{%- if schema.kw_const -%}
<span class="const-value" id="{{ schema.kw_const.html_id }}">Specific value: <code>{{ schema.kw_const.raw | python_to_json }}</code></span>
{%- if schema.is_const -%}
<span class="const-value" id="{{ schema.html_id }}_const">Specific value: <code>{{ schema.const_value | python_to_json }}</code></span>
{%- endif -%}

{# Pattern (Regular Expression) #}
Expand Down
4 changes: 2 additions & 2 deletions json_schema_for_humans/templates/js_offline/content.html
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ <h4>Must be one of:</h4>
</ul>
</div>
{%- endif -%}
{%- if schema.kw_const -%}
<span class="const-value" id="{{ schema.kw_const.html_id }}">Specific value: <code>{{ schema.kw_const.raw | python_to_json }}</code></span>
{%- if schema.is_const -%}
<span class="const-value" id="{{ schema.html_id }}_const">Specific value: <code>{{ schema.const_value | python_to_json }}</code></span>
{%- endif -%}

{# Pattern (Regular Expression) #}
Expand Down
4 changes: 2 additions & 2 deletions json_schema_for_humans/templates/md/content.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@
{% if schema.kw_enum -%}
{% include "section_one_of.md" %}
{%- endif %}
{%- if schema.kw_const -%}
Specific value: `{{ schema.kw_const.raw | python_to_json }}`
{%- if schema.is_const -%}
Specific value: `{{ schema.const_value | python_to_json }}`
{%- endif -%}

{# Conditional subschema, or if-then-else section #}
Expand Down
Loading

0 comments on commit e2642b2

Please sign in to comment.