Skip to content

Commit

Permalink
Clean up URI resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
richvdh committed Mar 13, 2024
1 parent 44db164 commit 62393f8
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions layouts/partials/openapi/resolve-ref-object.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,18 @@

{{ $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 +54,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 62393f8

Please sign in to comment.