-
-
Notifications
You must be signed in to change notification settings - Fork 103
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
Support reference objects in operation parameters #1749
Changes from all commits
477023b
cc978c5
44db164
62393f8
63638fb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Templates: add support for reference objects in parameters. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
{{/* | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We also need to update |
||
|
||
Handles OpenAPI "Reference Objects". | ||
|
||
Reference objects are JSON objects with a single property, `$ref` (and | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's actually a bit more complicated than that… Depending on the property it is used in, the behavior of conflicts could be undefined. This table summarizes things. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right. I guess we are specifically targetting "reference objects" here, so it's correct that I guess we'd need to make this logic more generic if we wanted to use it in other places, but that could be a future change. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, what made me react here is more the comment in the resolve-refs partial that says it should be replaced by this one, but in its current state it's not possible. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah ok, maybe we can clarify that comment. |
||
optionally a `summary` and `description`). This partial resolves the reference | ||
and returns the expanded object. | ||
|
||
The input parameter is a dict with the following keys: | ||
|
||
* `schema`: A schema object to check for $ref properties. | ||
|
||
* `path`: The path of the schema file containing the (potential) ref; used for resolving | ||
relative references. | ||
|
||
Ref: https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#reference-object. | ||
|
||
*/}} | ||
|
||
{{ $schema := .schema }} | ||
{{ $path := .path }} | ||
|
||
{{ $ret := $schema }} | ||
|
||
{{ $ref_value := index $schema "$ref"}} | ||
{{ if $ref_value }} | ||
{{/* 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 $ref_uri.Fragment }} | ||
{{/* | ||
Per https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#relative-references-in-uris: | ||
|
||
> If a URI contains a fragment identifier, then the fragment should | ||
> be resolved per the fragment resolution mechanism of the | ||
> referenced document. If the representation of the referenced | ||
> document is JSON or YAML, then the fragment identifier SHOULD be | ||
> interpreted as a JSON-Pointer as per RFC6901. | ||
|
||
RFC6901, in a nutshell, says the pointer is a series of keys | ||
separated by `/`. We strip off the leading `/` and use the | ||
subsequent keys as indexes. | ||
*/}} | ||
|
||
{{ $keys := split (strings.TrimPrefix "/" $ref_uri.Fragment ) "/" }} | ||
{{ $ret = index $ret $keys }} | ||
{{ end }} | ||
|
||
{{/* | ||
OpenAPI spec says that "summary" and "description" from the reference object override | ||
the values from the referenced component. | ||
*/}} | ||
{{ if isset $schema "summary" }} | ||
{{ $ret = merge $ret (dict "summary" $schema.summary) }} | ||
{{ end }} | ||
{{ if isset $schema "description" }} | ||
{{ $ret = merge $ret (dict "description" $schema.summary) }} | ||
{{ end }} | ||
Comment on lines
+57
to
+66
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we want to be more strict with how we resolve refs, should we throw an error or log a warning if |
||
{{ end }} | ||
|
||
{{ return $ret }} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
path
is expected to be the path to the file, but the one received by this partial is the path to the folder containing the file. So we need to have the filename in the path here somehow, without interfering with other partials that useresolve-refs
and still expect the folder.