Fix rendering of properties with a list of types (#1487)

pull/1507/head
Richard van der Hoff 1 year ago committed by GitHub
parent 1f729eef60
commit cdbf44eef0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1 @@
Fix rendering of properties with a list of types

@ -34,49 +34,32 @@
{{ range $property_name, $property := $properties }} {{ range $property_name, $property := $properties }}
{{ $property := partial "json-schema/resolve-allof" $property }} {{ $property := partial "json-schema/resolve-allof" $property }}
{{ $type := $property.type }}
{{/* {{ if eq $property.type "object" }}
If the property has a `title`, use that rather than `type`. {{ $type = partial "type-or-title" $property }}
This means we can write things like `EventFilter` rather than `object`.
*/}}
{{ $type := $property.type}}
{{ if $property.title }}
{{ $type = $property.title }}
{{ end }} {{ end }}
{{/* {{/*
If the property is an array, indicate this with square brackets, If the property is an array, indicate this with square brackets,
like `[type]`. like `[type]`.
*/}} */}}
{{ if eq $type "array"}} {{ if eq $property.type "array"}}
{{ $items := $property.items }} {{ $items := $property.items }}
{{ if $property.items }} {{ if $property.items }}
{{ $items = partial "json-schema/resolve-allof" $property.items }} {{ $items = partial "json-schema/resolve-allof" $property.items }}
{{ end }} {{ end }}
{{ $inner_type := $items.type }} {{ $inner_type := partial "type-or-title" $items }}
{{ if $items.title }}
{{ $inner_type = $items.title }}
{{ end }}
{{ $type = delimit (slice "[" $inner_type "]") "" }} {{ $type = delimit (slice "[" $inner_type "]") "" }}
{{ end }} {{ end }}
{{/* {{/*
If the property is an enum, indicate this. If the property is an enum, indicate this.
*/}} */}}
{{ if (and (eq $type "string") ($property.enum)) }} {{ if (and (eq $property.type "string") ($property.enum)) }}
{{ $type = "enum" }} {{ $type = "enum" }}
{{ end }} {{ end }}
{{/*
If the property uses `additionalProperties` to describe its
internal structure, handle this.
*/}}
{{ if reflect.IsMap $property.additionalProperties }}
{{ if $property.additionalProperties.title }}
{{ $type = delimit (slice "{string: " $property.additionalProperties.title "}" ) "" }}
{{ end }}
{{ end }}
{{/* {{/*
Handle two ways of indicating "required", one for simple parameters, Handle two ways of indicating "required", one for simple parameters,
the other for request and response body objects. the other for request and response body objects.
@ -100,3 +83,30 @@
</table> </table>
{{ end }} {{ 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.
*/}}
{{ define "partials/type-or-title" }}
{{ $type := "" }}
{{ if .title }}
{{/*
If the property has a `title`, use that rather than `type`.
This means we can write things like `EventFilter` rather than `object`.
*/}}
{{ $type = .title }}
{{ else if reflect.IsMap .additionalProperties }}
{{/*
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) "}" ) "" }}
{{ else if reflect.IsSlice .type }}
{{/* It's legal to specify an array of types. Join them together in that case */}}
{{ $type = delimit .type "|" }}
{{ else }}
{{ $type = .type }}
{{ end }}
{{ return $type }}
{{ end }}

Loading…
Cancel
Save