Skip to content

Commit

Permalink
Fixed borked tests after path upgrades
Browse files Browse the repository at this point in the history
  • Loading branch information
daveshanley committed Jun 13, 2024
1 parent ea98726 commit 90bc8e8
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 16 deletions.
16 changes: 8 additions & 8 deletions index/spec_index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1249,15 +1249,15 @@ components:
if assert.Contains(t, params, "/") {
if assert.Contains(t, params["/"], "top") {
if assert.Contains(t, params["/"]["top"], "#/components/parameters/param1") {
assert.Equal(t, "$.components.parameters.param1", params["/"]["top"]["#/components/parameters/param1"][0].Path)
assert.Equal(t, "$.components.parameters['param1']", params["/"]["top"]["#/components/parameters/param1"][0].Path)
}
if assert.Contains(t, params["/"]["top"], "paramour.yaml#/components/parameters/param3") {
assert.Equal(t, "$.components.parameters.param3", params["/"]["top"]["paramour.yaml#/components/parameters/param3"][0].Path)
assert.Equal(t, "$.components.parameters['param3']", params["/"]["top"]["paramour.yaml#/components/parameters/param3"][0].Path)
}
}
if assert.Contains(t, params["/"], "get") {
if assert.Contains(t, params["/"]["get"], "#/components/parameters/param2") {
assert.Equal(t, "$.components.parameters.param2", params["/"]["get"]["#/components/parameters/param2"][0].Path)
assert.Equal(t, "$.components.parameters['param2']", params["/"]["get"]["#/components/parameters/param2"][0].Path)
}
if assert.Contains(t, params["/"]["get"], "test") {
assert.Equal(t, "$.paths['/'].get.parameters[2]", params["/"]["get"]["test"][0].Path)
Expand Down Expand Up @@ -1620,9 +1620,9 @@ paths:
idx := NewSpecIndexWithConfig(&rootNode, CreateOpenAPIIndexConfig())

schemas := idx.GetAllInlineSchemas()
assert.Equal(t, "$.paths['/test'].get.parameters.schema", schemas[0].Path)
assert.Equal(t, "$.paths['/test'].get.parameters.schema.properties.code", schemas[1].Path)
assert.Equal(t, "$.paths['/test'].get.parameters.schema.properties.message", schemas[2].Path)
assert.Equal(t, "$.paths['/test'].get.parameters['schema']", schemas[0].Path)
assert.Equal(t, "$.paths['/test'].get.parameters['schema'].properties['code']", schemas[1].Path)
assert.Equal(t, "$.paths['/test'].get.parameters['schema'].properties['message']", schemas[2].Path)
}

func TestSpecIndex_TestPathsAsRef(t *testing.T) {
Expand Down Expand Up @@ -1655,8 +1655,8 @@ components:

index := NewSpecIndexWithConfig(&rootNode, CreateOpenAPIIndexConfig())
params := index.GetOperationParameterReferences()
assert.Equal(t, "$.components.parameters.test-2", params["/test"]["top"]["#/components/parameters/test-2"][0].Path)
assert.Equal(t, "$.components.parameters.test-3", params["/test-2"]["get"]["#/components/parameters/test-3"][0].Path)
assert.Equal(t, "$.components.parameters['test-2']", params["/test"]["top"]["#/components/parameters/test-2"][0].Path)
assert.Equal(t, "$.components.parameters['test-3']", params["/test-2"]["get"]["#/components/parameters/test-3"][0].Path)
assert.Equal(t, "bing bong", params["/test"]["top"]["#/components/parameters/test-2"][0].Node.Content[5].Value)
assert.Equal(t, "ding a ling", params["/test"]["get"]["#/components/parameters/test-3"][0].Node.Content[5].Value)
}
Expand Down
18 changes: 14 additions & 4 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ func IsHttpVerb(verb string) bool {
// define bracket name expression
var (
bracketNameExp = regexp.MustCompile(`^(\w+)\['?(\w+)\'?]$`)
pathCharExp = regexp.MustCompile(`[%=;~.]`)
pathCharExp = regexp.MustCompile(`[\\%=;~.]`)
)

func ConvertComponentIdIntoFriendlyPathSearch(id string) (string, string) {
Expand All @@ -600,7 +600,13 @@ func ConvertComponentIdIntoFriendlyPathSearch(id string) (string, string) {
// check for strange spaces, chars and if found, wrap them up, clean them and create a new cleaned path.
for i := range segs {
if pathCharExp.Match([]byte(segs[i])) {

segs[i], _ = url.QueryUnescape(strings.ReplaceAll(segs[i], "~1", "/"))
if strings.Contains(segs[i], `\`) {
segs[i] = strings.ReplaceAll(segs[i], `\`, "")
cleaned = append(cleaned, segs[i])
continue

Check warning on line 608 in utils/utils.go

View check run for this annotation

Codecov / codecov/patch

utils/utils.go#L606-L608

Added lines #L606 - L608 were not covered by tests
}
segs[i] = fmt.Sprintf("['%s']", segs[i])
if len(cleaned) > 0 {
cleaned[len(cleaned)-1] = fmt.Sprintf("%s%s", segs[i-1], segs[i])
Expand Down Expand Up @@ -633,7 +639,7 @@ func ConvertComponentIdIntoFriendlyPathSearch(id string) (string, string) {
}

// if we have a plural parent, wrap it in quotes.
if i > 0 && segs[i-1][len(segs[i-1])-1] == 's' {
if i > 0 && segs[i-1] != "" && segs[i-1][len(segs[i-1])-1] == 's' {
if i == 2 { // ignore first segment.
cleaned = append(cleaned, segs[i])
continue
Expand Down Expand Up @@ -662,6 +668,8 @@ func ConvertComponentIdIntoFriendlyPathSearch(id string) (string, string) {
return name, replaced
}

// ConvertComponentIdIntoPath will convert a JSON Path into a component ID
// TODO: This function is named incorrectly and should be changed to reflect the correct function
func ConvertComponentIdIntoPath(id string) (string, string) {

segs := strings.Split(id, ".")
Expand All @@ -678,14 +686,16 @@ func ConvertComponentIdIntoPath(id string) (string, string) {
}
}

// if there are brackets, shift the path to encapsulate them correctly.
if len(brackets) > 0 {
cleaned = append(cleaned[:i], append([]string{bracketNameExp.ReplaceAllString(segs[i], "$1/$2")}, cleaned[i:]...)...)
cleaned = append(cleaned[:i],
append([]string{bracketNameExp.ReplaceAllString(segs[i], "$1/$2")}, cleaned[i:]...)...)
continue
}
cleaned = append(cleaned, segs[i])
}

if segs[0] != "#" {
if cleaned[0] != "#" {
cleaned = append(cleaned[:0], append([]string{"#"}, cleaned[0:]...)...)

}
Expand Down
8 changes: 4 additions & 4 deletions utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -737,8 +737,8 @@ func TestConvertComponentIdIntoFriendlyPathSearch_Simple(t *testing.T) {

func TestConvertComponentIdIntoFriendlyPathSearch_Plural(t *testing.T) {
segment, path := ConvertComponentIdIntoFriendlyPathSearch("#/components/schemas/FreshMan/properties/subkeys/examples/0/expires_at")
assert.Equal(t, "$.components.schemas['FreshMan'].properties['subkeys'].example[0].expires_at", path)
assert.Equal(t, "get", segment)
assert.Equal(t, "$.components.schemas['FreshMan'].properties['subkeys'].examples[0].expires_at", path)
assert.Equal(t, "expires_at", segment)
}

func TestConvertComponentIdIntoFriendlyPathSearch_Params(t *testing.T) {
Expand Down Expand Up @@ -790,8 +790,8 @@ func TestConvertComponentIdIntoFriendlyPathSearch_HTTPCode(t *testing.T) {
}

func TestConvertComponentIdIntoPath(t *testing.T) {
segment, path := ConvertComponentIdIntoPath("#/chicken/chips/pizza/cake")
assert.Equal(t, "$.chicken.chips.pizza.cake", path)
segment, path := ConvertComponentIdIntoPath("$.chicken.chips.pizza.cake")
assert.Equal(t, "#/chicken/chips/pizza/cake", path)
assert.Equal(t, "cake", segment)
}

Expand Down

0 comments on commit 90bc8e8

Please sign in to comment.