Skip to content

Commit 3451748

Browse files
committed
cleaning things up a little
adding some more coverage
1 parent 3dd18dc commit 3451748

File tree

2 files changed

+19
-21
lines changed

2 files changed

+19
-21
lines changed

utils/utils.go

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -740,17 +740,28 @@ func ConvertComponentIdIntoFriendlyPathSearch(id string) (string, string) {
740740
sb.WriteString("']")
741741
segs[i] = sb.String()
742742

743-
if len(cleaned) > 0 {
743+
if len(cleaned) > 0 && i < len(segs)-1 {
744744
sb.Reset()
745745
sb.WriteString(segs[i-1])
746746
sb.WriteString(segs[i])
747747
cleaned[len(cleaned)-1] = sb.String()
748748
continue
749749
} else {
750-
if i > 0 {
750+
if i > 0 && i < len(segs)-1 {
751751
cleaned = append(cleaned, segs[i])
752752
continue
753753
}
754+
if i == len(segs)-1 {
755+
sb.Reset()
756+
l := len(cleaned)
757+
if l > 0 {
758+
sb.WriteString(cleaned[l-1])
759+
sb.WriteString(segs[i])
760+
cleaned[l-1] = sb.String()
761+
} else {
762+
cleaned = append(cleaned, segs[i])
763+
}
764+
}
754765
}
755766
} else {
756767

@@ -761,21 +772,6 @@ func ConvertComponentIdIntoFriendlyPathSearch(id string) (string, string) {
761772
continue
762773
}
763774

764-
// check for brackets in the name, and if found, rewire the path to encapsulate them
765-
// correctly. https://github.com/pb33f/libopenapi/issues/112
766-
brackets := bracketNameExp.FindStringSubmatch(segs[i])
767-
768-
if len(brackets) > 0 {
769-
segs[i] = bracketNameExp.ReplaceAllString(segs[i], "['$1[$2]']")
770-
if len(cleaned) > 0 {
771-
sb.Reset()
772-
sb.WriteString(segs[i-1])
773-
sb.WriteString(segs[i])
774-
cleaned[len(cleaned)-1] = sb.String()
775-
continue
776-
}
777-
}
778-
779775
intVal, err := strconv.Atoi(segs[i])
780776
if err == nil {
781777
if intVal <= 99 {
@@ -812,10 +808,6 @@ func ConvertComponentIdIntoFriendlyPathSearch(id string) (string, string) {
812808
}
813809
}
814810

815-
if len(cleaned) == 0 && len(segs) == 1 {
816-
cleaned = append(cleaned, segs[0])
817-
}
818-
819811
var replaced string
820812
if len(cleaned) > 1 {
821813
replaced = strings.ReplaceAll(strings.Join(cleaned, "."), "#", "$")

utils/utils_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,12 @@ func TestConvertComponentIdIntoFriendlyPathSearch_Slashes(t *testing.T) {
813813
assert.Equal(t, "$.nice.rice.and.spice", path)
814814
}
815815

816+
// https://github.com/pb33f/libopenapi/issues/112
817+
func TestConvertComponentIdIntoFriendlyPathSearch_BracketInName(t *testing.T) {
818+
_, path := ConvertComponentIdIntoFriendlyPathSearch(`#/oo/missus/hows/your/fa[ther]`)
819+
assert.Equal(t, "$.oo.missus['hows']['your']['fa[ther]']", path)
820+
}
821+
816822
func TestConvertComponentIdIntoFriendlyPathSearch_DashSomething(t *testing.T) {
817823
_, path := ConvertComponentIdIntoFriendlyPathSearch(`-rome`)
818824
assert.Equal(t, "$.['-rome']", path)

0 commit comments

Comments
 (0)