Fix rendering of additional and pattern Properties at the root of an object

Fixes rendering of m.receipt and m.direct, and the body or response of a few endpoints

Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
This commit is contained in:
Kévin Commaille 2023-09-13 16:35:27 +02:00
parent 7838d32596
commit e719219a6c
No known key found for this signature in database
GPG key ID: 29A48C1F03620416
7 changed files with 47 additions and 14 deletions

View file

@ -56,7 +56,11 @@
<h2>Content</h2> <h2>Content</h2>
{{ $anchor_base := anchorize $event_name }} {{ $anchor_base := anchorize $event_name }}
{{ $additional_types := partial "json-schema/resolve-additional-types" (dict "schema" $event_data.properties.content "anchor_base" $anchor_base) }} {{ $additional_types := partial "json-schema/resolve-additional-types" (dict
"schema" $event_data.properties.content
"anchor_base" $anchor_base
"is_root" true
) }}
{{ $additional_types = uniq $additional_types }} {{ $additional_types = uniq $additional_types }}
{{ range $additional_types }} {{ range $additional_types }}

View file

@ -2,6 +2,7 @@
Finds and returns all nested objects, given a dict containing: Finds and returns all nested objects, given a dict containing:
* `schema`: a JSON schema object * `schema`: a JSON schema object
* `is_root`: Whether this is the root of the schema, meaning we should not clean the object so we can show additional and pattern properties.
* `anchor_base`: a prefix to add to the HTML anchors generated for each object. If nil, no anchors are generated. * `anchor_base`: a prefix to add to the HTML anchors generated for each object. If nil, no anchors are generated.
* `name`: optionally, a name to use for this object in error/warning messages. If left unset, * `name`: optionally, a name to use for this object in error/warning messages. If left unset,
the object's `title` property is used (if present). the object's `title` property is used (if present).
@ -11,7 +12,7 @@
Assumes that "resolve-refs" and "resolve-allof" has already been called on the Assumes that "resolve-refs" and "resolve-allof" has already been called on the
input schema. input schema.
Returns an array of all the objects found. For each object, the following properties are returned: Returns an array of all the objects found. If `is_root` is true, the first object keeps all its properties. For all other objects, the following properties are returned:
* title * title
* properties * properties
* required * required
@ -23,6 +24,7 @@
*/}} */}}
{{ $this_object := .schema }} {{ $this_object := .schema }}
{{ $is_root := .is_root }}
{{ $anchor_base := .anchor_base }} {{ $anchor_base := .anchor_base }}
{{ $additional_objects := slice }} {{ $additional_objects := slice }}
{{ $name := .name | default $this_object.title | default "<untitled object>" }} {{ $name := .name | default $this_object.title | default "<untitled object>" }}
@ -36,7 +38,11 @@
{{/* {{/*
Add the object we were passed into the $additional_objects array Add the object we were passed into the $additional_objects array
*/}} */}}
{{ $additional_objects = $additional_objects | append (partial "clean-object" $this_object) }} {{ $this_additional_object := $this_object }}
{{ if not $is_root }}
{{ $this_additional_object = partial "clean-object" $this_object }}
{{ end }}
{{ $additional_objects = $additional_objects | append $this_additional_object }}
{{/* {{/*
Add any nested objects referenced in this object's `additionalProperties` Add any nested objects referenced in this object's `additionalProperties`

View file

@ -44,19 +44,31 @@
<tr> <tr>
<td><code>{{ $property_name }}</code></td> <td><code>{{ $property_name }}</code></td>
<td><code>{{ partial "partials/property-type" $property }}</code></td> <td><code>{{ partial "partials/property-type" $property }}</code></td>
<td> <td>{{ partial "partials/property-description" (dict "property" $property "required" $required) }}</td>
{{ if $required }}<strong>Required: </strong>{{end -}}
{{ $property.description | markdownify -}}
{{ if $property.enum }}<p>One of: <code>[{{ delimit $property.enum ", " }}]</code>.</p>{{ end -}}
{{ if (index $property "x-addedInMatrixVersion") }}{{ partial "added-in" (dict "v" (index $property "x-addedInMatrixVersion")) }}{{ end -}}
{{ if (index $property "x-changedInMatrixVersion") }}{{ partial "changed-in" (dict "changes_dict" (index $property "x-changedInMatrixVersion")) }}{{ end -}}
</td>
</tr> </tr>
{{ end }} {{ end }}
</table> </table>
{{ else if (or .additionalProperties .patternProperties) }}
<table{{ if .anchor }} id="{{ .anchor }}"{{ end }} class="object-table">
{{ with $title }}
<caption>{{ . }}</caption>
{{ end }}
<thead>
<th class="col-type">Type</th>
<th class="col-description">Description</th>
</thead>
{{ $property := partial "json-schema/resolve-allof" . }}
<tr>
<td><code>{{ partial "partials/property-type" $property }}</code></td>
<td>{{ partial "partials/property-description" (dict "property" $property) }}</td>
</tr>
</table>
{{ end }} {{ end }}
{{/* {{/*
@ -148,3 +160,14 @@
{{ end }} {{ end }}
{{ return $type }} {{ return $type }}
{{ end }} {{ end }}
{{/*
Computes the description to display for a property.
*/}}
{{ define "partials/property-description" }}
{{ if .required }}<strong>Required: </strong>{{end -}}
{{ .property.description | markdownify -}}
{{ if .property.enum }}<p>One of: <code>[{{ delimit .property.enum ", " }}]</code>.</p>{{ end -}}
{{ if (index .property "x-addedInMatrixVersion") }}{{ partial "added-in" (dict "v" (index .property "x-addedInMatrixVersion")) }}{{ end -}}
{{ if (index .property "x-changedInMatrixVersion") }}{{ partial "changed-in" (dict "changes_dict" (index .property "x-changedInMatrixVersion")) }}{{ end -}}
{{ end }}

View file

@ -45,7 +45,7 @@
{{ $schema := partial "json-schema/resolve-refs" (dict "schema" $json_body.schema "path" $path) }} {{ $schema := partial "json-schema/resolve-refs" (dict "schema" $json_body.schema "path" $path) }}
{{ $schema := partial "json-schema/resolve-allof" $schema }} {{ $schema := partial "json-schema/resolve-allof" $schema }}
{{ $additional_types := partial "json-schema/resolve-additional-types" (dict "schema" $schema "anchor_base" $anchor_base) }} {{ $additional_types := partial "json-schema/resolve-additional-types" (dict "schema" $schema "anchor_base" $anchor_base "is_root" true) }}
{{ $additional_types = uniq $additional_types }} {{ $additional_types = uniq $additional_types }}
{{ range $additional_types }} {{ range $additional_types }}
{{ partial "openapi/render-object-table" . }} {{ partial "openapi/render-object-table" . }}

View file

@ -80,7 +80,7 @@
response. (This will be a no-op for response types which aren't response. (This will be a no-op for response types which aren't
objects or arrays.) objects or arrays.)
*/}} */}}
{{ $additional_types := partial "json-schema/resolve-additional-types" (dict "schema" $schema "anchor_base" $anchor_base) }} {{ $additional_types := partial "json-schema/resolve-additional-types" (dict "schema" $schema "anchor_base" $anchor_base "is_root" true) }}
{{ $additional_types = uniq $additional_types }} {{ $additional_types = uniq $additional_types }}
{{ range $additional_types }} {{ range $additional_types }}
{{ partial "openapi/render-object-table" . }} {{ partial "openapi/render-object-table" . }}

View file

@ -50,7 +50,7 @@
{{ $definition.description | markdownify }} {{ $definition.description | markdownify }}
{{ $additional_types := partial "json-schema/resolve-additional-types" (dict "schema" $definition "name" (printf "\"%s\"" $path)) }} {{ $additional_types := partial "json-schema/resolve-additional-types" (dict "schema" $definition "name" (printf "\"%s\"" $path)) "is_root" true }}
{{ $additional_types = uniq $additional_types }} {{ $additional_types = uniq $additional_types }}
{{ range $additional_types }} {{ range $additional_types }}

View file

@ -35,7 +35,7 @@
{{ $event = merge $event (dict "title" "") }} {{ $event = merge $event (dict "title" "") }}
{{ $additional_types := partial "json-schema/resolve-additional-types" (dict "schema" $event) }} {{ $additional_types := partial "json-schema/resolve-additional-types" (dict "schema" $event) "is_root" true }}
{{ range $additional_types }} {{ range $additional_types }}
{{ partial "openapi/render-object-table" . }} {{ partial "openapi/render-object-table" . }}
{{end}} {{end}}