Fix property type resolution in render-object-table (#1789)

The split was not clear between property-type and type-or-title,
so it was not obvious which partial should be called for recursion.
That resulted in an error where type-or-title was only called for objects and array items, even if it also resolves
arrays of types.

This makes the split clearer. property-type must be called for any schema,
and object-type-or-title is only called for object schemas.

Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
pull/1791/head
Kévin Commaille 7 months ago committed by GitHub
parent 521e555cf6
commit f4e7b2aa97
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1 @@
Fix property type resolution in `render-object-table` partial.

@ -78,20 +78,20 @@
{{ end }} {{ end }}
{{/* {{/*
Computes the type to display for a property, given: Computes the type to display for a property's schema, given:
* `type`: string or array of strings for the type(s) of the property * `type`: optional string or array of strings for the type(s) of the property
* `title`: optional string for the title of the property * `title`: optional string for the title of the property
* `oneOf`: optional array of dictionaries describing the different formats * `oneOf`: optional array of dictionaries describing the different formats
that the property can have that the property can have
* `additionalProperties`: optional dictionary for properties with undefined * `additionalProperties`: if the type is an object, optional dictionary for
names properties with undefined names
* `patternProperties`: optional dictionary for properties with names * `patternProperties`: if the type is an object, optional dictionary for
adhering to a regex pattern properties with names adhering to a regex pattern
* `items`: if the type is an array, array of dictionaries describing the * `items`: if the type is an array, array of dictionaries describing the
format of the array's items format of the array's items
@ -100,37 +100,59 @@
*/}} */}}
{{ define "partials/property-type" }} {{ define "partials/property-type" }}
{{ $type := .type }} {{ $type := "" }}
{{ if or (eq .type "object") (and .oneOf (reflect.IsSlice .oneOf)) }}
{{ $type = partial "type-or-title" . }}
{{ end }}
{{/* {{ if eq .type "object" }}
If the property is an array, indicate this with square brackets, {{/* Resolve the type or title of the object */}}
like `[type]`. {{ $type = partial "object-type-or-title" . }}
*/}} {{ else if eq .type "array"}}
{{ if eq .type "array"}} {{/*
If the property is an array, indicate this with square brackets,
like `[type]`.
*/}}
{{ $items := .items }} {{ $items := .items }}
{{ if .items }} {{ if .items }}
{{ $items = partial "json-schema/resolve-allof" .items }} {{ $items = partial "json-schema/resolve-allof" .items }}
{{ end }} {{ end }}
{{ $inner_type := partial "type-or-title" $items }} {{ $inner_type := partial "property-type" $items }}
{{ $type = delimit (slice "[" $inner_type "]") "" }} {{ $type = delimit (slice "[" $inner_type "]") "" }}
{{ else if or (reflect.IsSlice .type) .oneOf }}
{{/*
It's legal to specify an array of types.
There are two ways to do that:
- Use an array of strings.
- Use oneOf, with items having a schema.
Join them together in that case, like `type|other_type`.
*/}}
{{ $types := slice }}
{{ if .oneOf }}
{{ range .oneOf }}
{{ $types = $types | append (partial "property-type" .) }}
{{ end }}
{{ else }}
{{ range .type }}
{{ $types = $types | append . }}
{{ end }}
{{ end }}
{{ $type = delimit $types "|" }}
{{ else }}
{{/* A simple type like string or boolean */}}
{{ $type = .type }}
{{ end }} {{ end }}
{{ return $type }} {{ return $type }}
{{ end }} {{ end }}
{{/* {{/*
Computes the type to display for a property's schema, given: Computes the type to display for an object property's schema, given:
* `type`: string or array of strings for the type(s) of the property * `type`: string equal to "object"
* `title`: optional string for the title of the property * `title`: optional string for the title of the object property
* `oneOf`: optional array of dictionaries describing the different formats
that the property can have
* `additionalProperties`: optional dictionary for properties with undefined * `additionalProperties`: optional dictionary for properties with undefined
names names
@ -142,8 +164,8 @@
The title has a higher priority than anything else. The title has a higher priority than anything else.
*/}} */}}
{{ define "partials/type-or-title" }} {{ define "partials/object-type-or-title" }}
{{ $type := "" }} {{ $type := "object" }}
{{ if .title }} {{ if .title }}
{{/* {{/*
If the property has a `title`, use that rather than `type`. If the property has a `title`, use that rather than `type`.
@ -176,32 +198,8 @@
{{ end }} {{ end }}
{{ $type = delimit (slice "{string: " (delimit $types "|") "}" ) "" }} {{ $type = delimit (slice "{string: " (delimit $types "|") "}" ) "" }}
{{ else if reflect.IsSlice .type }}
{{/* It's legal to specify an array of types. Join them together in that case */}}
{{ $types := slice }}
{{ range .type }}
{{ $types = $types | append . }}
{{ end }}
{{ $type = delimit $types "|" }}
{{ else if and .oneOf (reflect.IsSlice .oneOf) }}
{{/*
This is like an array of types except some of the types probably have a schema.
Join them together too.
*/}}
{{ $types := slice }}
{{ range .oneOf }}
{{ $types = $types | append (partial "type-or-title" .) }}
{{ end }}
{{ $type = delimit $types "|" }}
{{ else }}
{{ $type = .type }}
{{ end }} {{ end }}
{{ return $type }} {{ return $type }}
{{ end }} {{ end }}

Loading…
Cancel
Save