Skip to content

Commit

Permalink
Merge branch 'rav/ref_objects_in_params' into dbkr/relations_common_p…
Browse files Browse the repository at this point in the history
…arams
  • Loading branch information
richvdh committed Mar 13, 2024
2 parents f16d804 + 63638fb commit f470763
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 34 deletions.
2 changes: 1 addition & 1 deletion layouts/partials/openapi/render-api.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

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

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

{{ with $path_data.get }}

Expand Down
4 changes: 1 addition & 3 deletions layouts/partials/openapi/render-operation.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
* `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"
* `root_schema`: the root schema object where this definition was found, to enable us to resolve local "$ref" references

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

Expand All @@ -23,7 +22,6 @@
{{ $endpoint := .endpoint }}
{{ $operation_data := .operation_data }}
{{ $path := .path }}
{{ $root_schema := .root_schema }}
{{ $anchor := anchorize $endpoint }}

<section class="rendered-data http-api {{ $method }}">
Expand Down Expand Up @@ -82,7 +80,7 @@ <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 "root_schema" $root_schema ) }}
{{ partial "openapi/render-request" (dict "parameters" $operation_data.parameters "request_body" $operation_data.requestBody "path" $path "anchor_base" $anchor ) }}
<hr/>
{{ partial "openapi/render-responses" (dict "responses" $operation_data.responses "path" $path "anchor_base" $anchor ) }}

Expand Down
8 changes: 1 addition & 7 deletions layouts/partials/openapi/render-parameters.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
* `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"
* `root_schema`: the root schema object where this definition was found, to enable us to resolve local "$ref" references

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

Expand All @@ -15,7 +14,6 @@
{{ $parameters := .parameters }}
{{ $type := .type }}
{{ $caption := .caption }}
{{ $root_schema := .root_schema }}
{{ $path := .path }}

{{/* build a dict mapping from name->parameter, which render-object-table expects */}}
Expand All @@ -26,11 +24,7 @@
Per https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#operation-object:
the parameters can be reference objects; resolve them now.
*/}}
{{ $parameter = partial "openapi/resolve-ref-object" (dict
"schema" $parameter
"root_schema" $root_schema
"path" $path
) }}
{{ $parameter = partial "openapi/resolve-ref-object" (dict "schema" $parameter "path" $path) }}
{{ if (eq $parameter.in $type) }}
{{/*
merge the schema at the same level as the rest of the other fields because that is
Expand Down
6 changes: 0 additions & 6 deletions layouts/partials/openapi/render-request.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,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"
* `root_schema`: the root schema object where this definition was found, to enable us to resolve local "$ref" references
* `anchor_base`: a prefix to add to the HTML anchors generated for each object

This template renders:
Expand All @@ -18,7 +17,6 @@
{{ $parameters := .parameters }}
{{ $request_body := .request_body }}
{{ $path := .path }}
{{ $root_schema := .root_schema }}
{{ $anchor_base := .anchor_base }}

<h2>Request</h2>
Expand All @@ -33,22 +31,18 @@ <h3>Request parameters</h3>
"type" "header"
"caption" "header parameters"
"path" $path
"root_schema" $root_schema
) }}
{{ partial "openapi/render-parameters" (dict
"parameters" $parameters
"type" "path"
"caption" "path parameters"
"path" $path
"root_schema" $root_schema
) }}
{{ partial "openapi/render-parameters" (dict
"parameters" $parameters
"type" "query"
"caption" "query parameters"
"root_schema" $root_schema
"path" $path
"root_schema" $root_schema
) }}
{{ end }}

Expand Down
29 changes: 12 additions & 17 deletions layouts/partials/openapi/resolve-ref-object.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,29 @@
* `path`: The path of the schema file containing the (potential) ref; used for resolving
relative references.

* `root_schema`: The top-level schema which contains the potential ref; use for resolving `#/`
references.

Ref: https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#reference-object.

*/}}

{{ $schema := .schema }}
{{ $path := .path }}
{{ $root_schema := .root_schema }}

{{ $ret := $schema }}

{{ $ref_value := index $schema "$ref"}}
{{ if $ref_value }}
{{ $ref_url := urls.Parse $ref_value }}

{{ if ne $ref_url.Path "" }}
{{/* Reference to a different file: load it */}}
{{ $full_path := path.Join $path $ref_url.Path }}
{{ $without_ext := replaceRE "\\.[^\\.]*$" "" $full_path }}
{{ $pieces := split $without_ext "/" }}
{{ $ret = index site.Data $pieces }}
{{ else }}
{{ $ret = $root_schema }}
{{ end }}
{{/* Resolve the ref URI relative to the path of the schema file */}}
{{ $base_uri := urls.Parse $path }}
{{ $ref_uri := urls.Parse $ref_value }}
{{ $full_uri := $base_uri.ResolveReference $ref_uri }}

{{/* strip the extension, and the leading `/`, from the path */}}
{{ $full_path := strings.TrimPrefix "/" (replaceRE "\\.[^\\.]*$" "" $full_uri.Path) }}

{{ $pieces := split $full_path "/" }}
{{ $ret = index site.Data $pieces }}

{{ if ne $ref_url.Fragment "" }}
{{ if $ref_uri.Fragment }}
{{/*
Per https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#relative-references-in-uris:

Expand All @@ -55,7 +50,7 @@
subsequent keys as indexes.
*/}}

{{ $keys := split (strings.TrimPrefix "/" $ref_url.Fragment ) "/" }}
{{ $keys := split (strings.TrimPrefix "/" $ref_uri.Fragment ) "/" }}
{{ $ret = index $ret $keys }}
{{ end }}

Expand Down

0 comments on commit f470763

Please sign in to comment.