ARTICLE AD BOX
My goal is to apply an Overlay to an Openapi Document.
The Overlay Specification exemplifies the usage of "traits".
E.g.: given the following document:
openapi: 3.1.0 info: title: API with a paged collection version: 1.0.0 paths: /items: get: x-oai-traits: ['paged'] responses: 200: description: OKone can target the get method with the following jsonpath:
$.paths[?(@.get['x-oai-traits'][?(@ == 'paged')])].getAccording to the Openapi Spec this JSONPath must be RFC 9535 Compliant.
My concrete use case is to target the root of the openapi document if a given trait is present. For example given the following open api document:
openapi: 3.1.0 info: title: API with a paged collection version: 1.0.0 x-oai-traits: ['something'] paths: /items: get: responses: 200: description: OKI would like to target the root node, so that my overlay can add some things to the document.
I am using the go libopenapi library which promises
libopenapi supports all RFC9535 and JSON Path Plus annotations and query expressions
I tried an expression like:
$[?(@['x-oai-traits'][?(@ == 'something')])]But the library says:
Warning (overlay 1): target matched zero nodesIs it possible to target the root node with a JSONPath that matches only given a condition of a child node using the libopenapi?
Here is an example script that tries to apply an overlay to an openapi document using the libopenapi:
package main import ( "fmt" "github.com/pb33f/libopenapi" ) func main() { openApiDocumentText := `openapi: 3.1.0 info: title: API with a paged collection version: 1.0.0 x-oai-traits: ['something'] paths: /items: get: responses: 200: description: OK ` overlayDocumentText := `overlay: 1.1.0 info: title: example overlay version: 1.0.0 actions: - target: "$[?(@['x-oai-traits'][?(@ == 'something')])]" update: info: license: name: Apache 2.0 identifier: Apache-2.0 ` overlayResult, err := libopenapi.ApplyOverlayFromBytesToSpecBytes([]byte(openApiDocumentText), []byte(overlayDocumentText)) if err != nil { panic(err) } for _, warning := range overlayResult.Warnings { fmt.Printf("Warning: %s (target: %s)\n", warning.Message, warning.Target) } fmt.Printf(string(overlayResult.Bytes)) }