Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions changelogs/internal/newsfragments/1789.clarification
@@ -0,0 +1 @@
Fix property type resolution in `render-object-table` partial.
98 changes: 48 additions & 50 deletions layouts/partials/openapi/render-object-table.html
Expand Up @@ -78,20 +78,20 @@
{{ 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

* `oneOf`: optional array of dictionaries describing the different formats
that the property can have

* `additionalProperties`: optional dictionary for properties with undefined
names
* `additionalProperties`: if the type is an object, optional dictionary for
properties with undefined names

* `patternProperties`: optional dictionary for properties with names
adhering to a regex pattern
* `patternProperties`: if the type is an object, optional dictionary for
properties with names adhering to a regex pattern

* `items`: if the type is an array, array of dictionaries describing the
format of the array's items
Expand All @@ -100,37 +100,59 @@

*/}}
{{ define "partials/property-type" }}
{{ $type := .type }}

{{ if or (eq .type "object") (and .oneOf (reflect.IsSlice .oneOf)) }}
{{ $type = partial "type-or-title" . }}
{{ end }}
{{ $type := "" }}

{{/*
If the property is an array, indicate this with square brackets,
like `[type]`.
*/}}
{{ if eq .type "array"}}
{{ if eq .type "object" }}
{{/* Resolve the type or title of the object */}}
{{ $type = partial "object-type-or-title" . }}
{{ else if eq .type "array"}}
{{/*
If the property is an array, indicate this with square brackets,
like `[type]`.
*/}}
{{ $items := .items }}
{{ if .items }}
{{ $items = partial "json-schema/resolve-allof" .items }}
{{ end }}
{{ $inner_type := partial "type-or-title" $items }}
{{ $inner_type := partial "property-type" $items }}
{{ $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 }}

{{ return $type }}
{{ 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

* `oneOf`: optional array of dictionaries describing the different formats
that the property can have
* `title`: optional string for the title of the object property

* `additionalProperties`: optional dictionary for properties with undefined
names
Expand All @@ -142,8 +164,8 @@

The title has a higher priority than anything else.
*/}}
{{ define "partials/type-or-title" }}
{{ $type := "" }}
{{ define "partials/object-type-or-title" }}
{{ $type := "object" }}
{{ if .title }}
{{/*
If the property has a `title`, use that rather than `type`.
Expand Down Expand Up @@ -176,32 +198,8 @@
{{ end }}

{{ $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 }}

{{ return $type }}
{{ end }}

Expand Down