resolve-additional-types: generate an id for each returned type

This commit is contained in:
Richard van der Hoff 2022-07-19 18:21:10 +01:00
parent 2ef9eb9a1c
commit b2b169ed1e

View file

@ -2,25 +2,33 @@
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
* `anchor_base`: a prefix to add to the HTML anchors generated for each object. If nil, no anchors are generated.
This template finds all nested objects inside `schema`. This template finds all nested objects inside `schema`.
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.
It "cleans" each object by copying only the parts of the objects that Returns an array of all the objects found. For each object, the following properties are returned:
the renderer needs, and adds the result to an array, `additional_objects`. * title
* properties
Finally it returns the array of all the objects it found. * required
* enum
* anchor: a string suitable for using as an html anchor for this object (if `anchor_base` was set, and the object has a title)
Note that the returned array may contain duplicate objects. Note that the returned array may contain duplicate objects.
*/}} */}}
{{ $this_object := .schema }} {{ $this_object := .schema }}
{{ $anchor_base := .anchor_base }}
{{ $additional_objects := slice }} {{ $additional_objects := slice }}
{{ if eq $this_object.type "object" }} {{ if eq $this_object.type "object" }}
{{/* give this object an anchor, if it has a name */}}
{{ if (and $anchor_base $this_object.title) }}
{{ $this_object = merge $this_object (dict "anchor" (printf "%s_%s" $anchor_base (anchorize $this_object.title))) }}
{{ end }}
{{/* {{/*
Add the object we were passed into the $additional_objects array Add the object we were passed into the $additional_objects array
@ -35,7 +43,7 @@
{{ $additional_objects = $additional_objects | append (partial "clean-object" $this_object.additionalProperties) }} {{ $additional_objects = $additional_objects | append (partial "clean-object" $this_object.additionalProperties) }}
{{ range $key, $property := $this_object.additionalProperties.properties }} {{ range $key, $property := $this_object.additionalProperties.properties }}
{{ $additional_objects = partial "get-additional-objects" (dict "this_object" $property "additional_objects" $additional_objects) }} {{ $additional_objects = partial "get-additional-objects" (dict "this_object" $property "additional_objects" $additional_objects "anchor_base" $anchor_base) }}
{{ end }} {{ end }}
{{ end }} {{ end }}
@ -45,7 +53,7 @@
Add any nested objects referenced in this object's `properties` Add any nested objects referenced in this object's `properties`
*/}} */}}
{{ range $key, $property := $this_object.properties}} {{ range $key, $property := $this_object.properties}}
{{ $additional_objects = partial "get-additional-objects" (dict "this_object" $property "additional_objects" $additional_objects) }} {{ $additional_objects = partial "get-additional-objects" (dict "this_object" $property "additional_objects" $additional_objects "anchor_base" $anchor_base) }}
{{ end }} {{ end }}
{{ end }} {{ end }}
@ -56,10 +64,10 @@
*/}} */}}
{{ if reflect.IsSlice $this_object.items}} {{ if reflect.IsSlice $this_object.items}}
{{ range $this_object.items }} {{ range $this_object.items }}
{{ $additional_objects = partial "get-additional-objects" (dict "this_object" . "additional_objects" $additional_objects) }} {{ $additional_objects = partial "get-additional-objects" (dict "this_object" . "additional_objects" $additional_objects "anchor_base" $anchor_base) }}
{{ end }} {{ end }}
{{ else }} {{ else }}
{{ $additional_objects = partial "get-additional-objects" (dict "this_object" $this_object.items "additional_objects" $additional_objects) }} {{ $additional_objects = partial "get-additional-objects" (dict "this_object" $this_object.items "additional_objects" $additional_objects "anchor_base" $anchor_base) }}
{{ end }} {{ end }}
{{ end }} {{ end }}
@ -77,7 +85,7 @@
*/ */
{{ $this_object := partial "json-schema/resolve-allof" .this_object }} {{ $this_object := partial "json-schema/resolve-allof" .this_object }}
{{ $more_objects := partial "json-schema/resolve-additional-types" (dict "schema" $this_object) }} {{ $more_objects := partial "json-schema/resolve-additional-types" (dict "schema" $this_object "anchor_base" .anchor_base) }}
{{/* {{/*
As far as I know we don't have something like Array.concat(), so add them one at a time As far as I know we don't have something like Array.concat(), so add them one at a time
*/}} */}}
@ -93,5 +101,5 @@
but with (for example) different examples will be considered different. but with (for example) different examples will be considered different.
*/}} */}}
{{ define "partials/clean-object" }} {{ define "partials/clean-object" }}
{{ return (dict "title" .title "properties" .properties "required" .required "enum" .enum) }} {{ return (dict "title" .title "properties" .properties "required" .required "enum" .enum "anchor" .anchor) }}
{{ end }} {{ end }}