From 4ca33aec902ddc16a02ba016d5a48c93913dbeb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Commaille?= Date: Wed, 13 Sep 2023 16:09:53 +0200 Subject: [PATCH] Do not assume additionalProperties is an object MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Also resolve allOf of additionalProperties. Fixes names of objects refs and rendering of arrays Signed-off-by: Kévin Commaille --- .../partials/openapi/render-object-table.html | 49 +++++++++++-------- 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/layouts/partials/openapi/render-object-table.html b/layouts/partials/openapi/render-object-table.html index 0de9d2d1..9690e978 100644 --- a/layouts/partials/openapi/render-object-table.html +++ b/layouts/partials/openapi/render-object-table.html @@ -34,24 +34,6 @@ {{ range $property_name, $property := $properties }} {{ $property := partial "json-schema/resolve-allof" $property }} - {{ $type := $property.type }} - - {{ if or (eq $property.type "object") (and $property.oneOf (reflect.IsSlice .oneOf)) }} - {{ $type = partial "type-or-title" $property }} - {{ end }} - - {{/* - If the property is an array, indicate this with square brackets, - like `[type]`. - */}} - {{ if eq $property.type "array"}} - {{ $items := $property.items }} - {{ if $property.items }} - {{ $items = partial "json-schema/resolve-allof" $property.items }} - {{ end }} - {{ $inner_type := partial "type-or-title" $items }} - {{ $type = delimit (slice "[" $inner_type "]") "" }} - {{ end }} {{/* Handle two ways of indicating "required", one for simple parameters, @@ -61,7 +43,7 @@ {{ $property_name }} - {{ $type }} + {{ partial "partials/property-type" $property }} {{ if $required }}Required: {{end -}} {{ $property.description | markdownify -}} @@ -77,6 +59,32 @@ {{ end }} +{{/* + Computes the type to display for a property. +*/}} +{{ define "partials/property-type" }} + {{ $type := .type }} + + {{ if or (eq .type "object") (and .oneOf (reflect.IsSlice .oneOf)) }} + {{ $type = partial "type-or-title" . }} + {{ end }} + + {{/* + If the property is an array, indicate this with square brackets, + like `[type]`. + */}} + {{ if eq .type "array"}} + {{ $items := .items }} + {{ if .items }} + {{ $items = partial "json-schema/resolve-allof" .items }} + {{ end }} + {{ $inner_type := partial "type-or-title" $items }} + {{ $type = delimit (slice "[" $inner_type "]") "" }} + {{ end }} + + {{ return $type }} +{{ end }} + {{/* Picks either the `title` of a property, or the `type`, to turn into the rendered type field. Also handles `additionalProperties`, if no `title` is present. @@ -94,7 +102,8 @@ If the property uses `additionalProperties` to describe its internal structure, handle this with a bit of recursion */}} - {{ $type = delimit (slice "{string: " (partial "type-or-title" .additionalProperties) "}" ) "" }} + {{ $additionalProperties := partial "json-schema/resolve-allof" .additionalProperties }} + {{ $type = delimit (slice "{string: " (partial "property-type" $additionalProperties) "}" ) "" }} {{ else if reflect.IsSlice .type }} {{/* It's legal to specify an array of types. Join them together in that case */}}