Skip to content

Commit

Permalink
Simplify uses of resolve-refs partial (#1773)
Browse files Browse the repository at this point in the history
* Use the resolve-refs partial as soon as possible

Call it right after accessing the site.Data,
since it is recursing it will solve all references in the tree.
That way we don't need to wonder where to call it,
we trust the validators that the refs will be used in the right place.

* Enable strict $ref rule in OpenAPI validator

* Document use of $ref to compose examples

* Fix schema path in event-fields shortcode

Signed-off-by: Kévin Commaille <[email protected]>
  • Loading branch information
zecakeh authored Apr 9, 2024
1 parent 2ea8e0f commit 2678370
Show file tree
Hide file tree
Showing 10 changed files with 22 additions and 31 deletions.
1 change: 1 addition & 0 deletions changelogs/internal/newsfragments/1773.clarification
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Simplify uses of `resolve-refs` partial.
6 changes: 1 addition & 5 deletions layouts/partials/openapi/render-api.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,19 @@
* `api_data`: the OpenAPI data
* `base_url`: the base URL: that is, the part we glue onto the front
of each value in `paths` to get a complete URL.
* `path`: the directory under /data where we found this API definition.
We use this to resolve "$ref" values, since they are relative to the schema's
location.

*/}}

{{ $api_data := index .api_data }}
{{ $base_url := .base_url }}
{{ $path := .path }}

{{ range $path_name, $path_data := $api_data.paths }}

{{ $endpoint := delimit (slice $base_url $path_name ) "" }}

{{/* note that a `paths` entry can be a $ref */}}

{{ $params := dict "endpoint" $endpoint "path" $path }}
{{ $params := dict "endpoint" $endpoint }}

{{ with $path_data.get }}

Expand Down
6 changes: 2 additions & 4 deletions layouts/partials/openapi/render-operation.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* `method`: the method, e.g. GET, PUT
* `endpoint`: the endpoint
* `operation_data`: the OpenAPI data for the operation
* `path`: the path where this definition was found, to enable us to resolve "$ref"

This template renders the operation as a `<section>` containing:

Expand All @@ -21,7 +20,6 @@
{{ $method := .method }}
{{ $endpoint := .endpoint }}
{{ $operation_data := .operation_data }}
{{ $path := .path }}
{{ $anchor := anchorize $endpoint }}

<section class="rendered-data http-api {{ $method }}">
Expand Down Expand Up @@ -80,9 +78,9 @@ <h1 id="{{ lower $method }}{{ $anchor }}">
</table>

<hr/>
{{ partial "openapi/render-request" (dict "parameters" $operation_data.parameters "request_body" $operation_data.requestBody "path" $path "anchor_base" $anchor ) }}
{{ partial "openapi/render-request" (dict "parameters" $operation_data.parameters "request_body" $operation_data.requestBody "anchor_base" $anchor ) }}
<hr/>
{{ partial "openapi/render-responses" (dict "responses" $operation_data.responses "path" $path "anchor_base" $anchor ) }}
{{ partial "openapi/render-responses" (dict "responses" $operation_data.responses "anchor_base" $anchor ) }}

</details>

Expand Down
3 changes: 0 additions & 3 deletions layouts/partials/openapi/render-parameters.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* `parameters`: OpenAPI data specifying the parameters
* `type`: the type of parameters to render: "header, ""path", "query"
* `caption`: caption to use for the table
* `path`: the path where this definition was found, to enable us to resolve "$ref"

This template renders a single table containing parameters of the given type.

Expand All @@ -14,9 +13,7 @@
{{ $parameters := .parameters }}
{{ $type := .type }}
{{ $caption := .caption }}
{{ $path := .path }}

{{ $parameters = partial "json-schema/resolve-refs" (dict "schema" $parameters "path" $path) }}
{{ $parameters_of_type := where $parameters "in" $type }}

{{ with $parameters_of_type }}
Expand Down
14 changes: 5 additions & 9 deletions layouts/partials/openapi/render-request.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

* `parameters`: OpenAPI data specifying the parameters
* `request_body`: OpenAPI data specifying the request body
* `path`: the path where this definition was found, to enable us to resolve "$ref"
* `anchor_base`: a prefix to add to the HTML anchors generated for each object

This template renders:
Expand All @@ -16,7 +15,6 @@

{{ $parameters := .parameters }}
{{ $request_body := .request_body }}
{{ $path := .path }}
{{ $anchor_base := .anchor_base }}

<h2>Request</h2>
Expand All @@ -26,9 +24,9 @@ <h2>Request</h2>
{{ if $parameters }}
<h3>Request parameters</h3>

{{ partial "openapi/render-parameters" (dict "parameters" $parameters "type" "header" "caption" "header parameters" "path" .path) }}
{{ partial "openapi/render-parameters" (dict "parameters" $parameters "type" "path" "caption" "path parameters" "path" .path) }}
{{ partial "openapi/render-parameters" (dict "parameters" $parameters "type" "query" "caption" "query parameters" "path" .path) }}
{{ partial "openapi/render-parameters" (dict "parameters" $parameters "type" "header" "caption" "header parameters") }}
{{ partial "openapi/render-parameters" (dict "parameters" $parameters "type" "path" "caption" "path parameters") }}
{{ partial "openapi/render-parameters" (dict "parameters" $parameters "type" "query" "caption" "query parameters") }}

{{ end }}

Expand All @@ -42,8 +40,7 @@ <h3>Request body</h3>
{{/*
Display the JSON schemas
*/}}
{{ $schema := partial "json-schema/resolve-refs" (dict "schema" $json_body.schema "path" $path) }}
{{ $schema := partial "json-schema/resolve-allof" $schema }}
{{ $schema := partial "json-schema/resolve-allof" $json_body.schema }}

{{ $additional_types := partial "json-schema/resolve-additional-types" (dict "schema" $schema "anchor_base" $anchor_base) }}
{{ range $additional_types }}
Expand All @@ -70,8 +67,7 @@ <h3>Request body example</h3>
{{ $example := dict }}

{{ if $body.schema }}
{{ $schema := partial "json-schema/resolve-refs" (dict "schema" $body.schema "path" $path) }}
{{ $schema := partial "json-schema/resolve-allof" $schema }}
{{ $schema := partial "json-schema/resolve-allof" $body.schema }}

{{ $example = partial "json-schema/resolve-example" $schema }}
{{ end }}
Expand Down
7 changes: 1 addition & 6 deletions layouts/partials/openapi/render-responses.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
Render the response part of a single HTTP API operation, given:

* `responses`: OpenAPI data specifying the responses
* `path`: the path where this definition was found, to enable us to resolve "$ref"
* `anchor_base`: a prefix to add to the HTML anchors generated for each object

This template renders:
Expand All @@ -15,7 +14,6 @@
*/}}

{{ $responses := .responses }}
{{ $path := .path }}
{{ $anchor_base := .anchor_base }}

<h2>Responses</h2>
Expand All @@ -26,8 +24,6 @@ <h2>Responses</h2>
<th class="col-status-description">Description</th>
</thead>

{{ $responses = partial "json-schema/resolve-refs" (dict "schema" $responses "path" $path) }}

{{ range $code, $response := $responses }}

<tr>
Expand Down Expand Up @@ -92,8 +88,7 @@ <h3>{{$code}} response</h3>
{{ if or (eq $schema.type "object") (eq $schema.type "array") }}
{{ $example := partial "json-schema/resolve-example" $schema }}
{{ if $json_body.examples }}
{{ $example = partial "json-schema/resolve-refs" (dict "schema" $json_body.examples "path" $path) }}
{{ $example = $example.response.value }}
{{ $example = $json_body.examples.response.value }}
{{ end }}

{{ $example_json := jsonify (dict "indent" " ") $example }}
Expand Down
2 changes: 1 addition & 1 deletion layouts/shortcodes/event-fields.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*/}}

{{ $event := index .Site.Data "event-schemas" "schema" "core-event-schema" .Params.event_type }}
{{ $path := "event-schemas/schema/core-event-schema" }}
{{ $path := delimit (slice "event-schemas/schema/core-event-schema" .Params.event_type) "/" }}

{{ $event = partial "json-schema/resolve-refs" (dict "schema" $event "path" $path) }}
{{ $event := partial "json-schema/resolve-allof" $event }}
Expand Down
4 changes: 3 additions & 1 deletion layouts/shortcodes/http-api.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@
{{ $base_url := (index $api_data.servers 0).variables.basePath.default }}
{{ $path := delimit (slice "api" $spec $api) "/" }}

{{ partial "openapi/render-api" (dict "api_data" $api_data "base_url" $base_url "path" $path) }}
{{ $api_data = partial "json-schema/resolve-refs" (dict "schema" $api_data "path" $path) }}

{{ partial "openapi/render-api" (dict "api_data" $api_data "base_url" $base_url) }}
5 changes: 5 additions & 0 deletions openapi_extensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,8 @@ property, etc).

A variation of the above: indicates changes to the associated parameter in
particular Matrix specification versions.

## Use of `$ref` inside examples

Although the OpenAPI/JSON Schema specs only allow to use `$ref` to reference a
whole example, we use it to compose examples from other examples.
5 changes: 3 additions & 2 deletions redocly.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# See https://redocly.com/docs/cli/configuration/ for more information.
extends:
- minimal
- recommended-strict
rules:
info-license: off
security-defined: off
operation-4xx-response: off
no-invalid-media-type-examples: off
no-path-trailing-slash: off
operation-2xx-response: off
operation-2xx-response: off
spec-strict-refs: error

0 comments on commit 2678370

Please sign in to comment.