Skip to content

Commit

Permalink
Merge pull request #368 from lobocv/jsonpath-key-names-support
Browse files Browse the repository at this point in the history
Added back support for the ~ operator which finds key names
  • Loading branch information
daveshanley authored Feb 1, 2025
2 parents df75a04 + 5e306f5 commit 26ed052
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 7 deletions.
3 changes: 2 additions & 1 deletion datamodel/low/extraction_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"context"
"crypto/sha256"
"fmt"
jsonpathconfig "github.com/speakeasy-api/jsonpath/pkg/jsonpath/config"
"net/url"
"path/filepath"
"reflect"
Expand Down Expand Up @@ -221,7 +222,7 @@ func LocateRefNodeWithContext(ctx context.Context, root *yaml.Node, idx *index.S
// cant be found? last resort is to try a path lookup
_, friendly := utils.ConvertComponentIdIntoFriendlyPathSearch(rv)
if friendly != "" {
path, err := jsonpath.NewPath(friendly)
path, err := jsonpath.NewPath(friendly, jsonpathconfig.WithPropertyNameExtension())
if err == nil {
nodes := path.Query(idx.GetRootNode())
if len(nodes) > 0 {
Expand Down
3 changes: 2 additions & 1 deletion index/find_component.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package index

import (
"fmt"
jsonpathconfig "github.com/speakeasy-api/jsonpath/pkg/jsonpath/config"
"net/url"
"path/filepath"
"strings"
Expand Down Expand Up @@ -57,7 +58,7 @@ func FindComponent(root *yaml.Node, componentId, absoluteFilePath string, index
if friendlySearch == "$." {
friendlySearch = "$"
}
path, err := jsonpath.NewPath(friendlySearch)
path, err := jsonpath.NewPath(friendlySearch, jsonpathconfig.WithPropertyNameExtension())
if path == nil || err != nil || root == nil {
return nil // no component found
}
Expand Down
5 changes: 3 additions & 2 deletions index/spec_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package index
import (
"fmt"
"github.com/speakeasy-api/jsonpath/pkg/jsonpath"
jsonpathconfig "github.com/speakeasy-api/jsonpath/pkg/jsonpath/config"
"log/slog"
"os"
"path/filepath"
Expand Down Expand Up @@ -700,7 +701,7 @@ func (index *SpecIndex) GetGlobalCallbacksCount() int {
for _, m := range p {

// look through method for callbacks
callbacks, _ := jsonpath.NewPath("$..callbacks")
callbacks, _ := jsonpath.NewPath("$..callbacks", jsonpathconfig.WithPropertyNameExtension())
var res []*yaml.Node
res = callbacks.Query(m.Node)
if len(res) > 0 {
Expand Down Expand Up @@ -746,7 +747,7 @@ func (index *SpecIndex) GetGlobalLinksCount() int {
for _, m := range p {

// look through method for links
links, _ := jsonpath.NewPath("$..links")
links, _ := jsonpath.NewPath("$..links", jsonpathconfig.WithPropertyNameExtension())
var res []*yaml.Node

res = links.Query(m.Node)
Expand Down
7 changes: 4 additions & 3 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"github.com/speakeasy-api/jsonpath/pkg/jsonpath"
jsonpathconfig "github.com/speakeasy-api/jsonpath/pkg/jsonpath/config"
"net/http"
"net/url"
"regexp"
Expand Down Expand Up @@ -45,7 +46,7 @@ func FindNodes(yamlData []byte, jsonPath string) ([]*yaml.Node, error) {
var node yaml.Node
yaml.Unmarshal(yamlData, &node)

path, err := jsonpath.NewPath(jsonPath)
path, err := jsonpath.NewPath(jsonPath, jsonpathconfig.WithPropertyNameExtension())
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -109,12 +110,12 @@ func FindNodesWithoutDeserializing(node *yaml.Node, jsonPath string) ([]*yaml.No
return FindNodesWithoutDeserializingWithTimeout(node, jsonPath, 500*time.Millisecond)
}

// FindNodesWithoutDeserializing will find a node based on JSONPath, without deserializing from yaml/json
// FindNodesWithoutDeserializingWithTimeout will find a node based on JSONPath, without deserializing from yaml/json
// This function can be customized with a timeout.
func FindNodesWithoutDeserializingWithTimeout(node *yaml.Node, jsonPath string, timeout time.Duration) ([]*yaml.Node, error) {
jsonPath = FixContext(jsonPath)

path, err := jsonpath.NewPath(jsonPath)
path, err := jsonpath.NewPath(jsonPath, jsonpathconfig.WithPropertyNameExtension())
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 26ed052

Please sign in to comment.