mirror of
https://github.com/LukeHagar/libopenapi.git
synced 2025-12-09 12:37:49 +00:00
Added support for references with brackets #112
Brackets in names are now supported. For example `’#/components/schemas/CoffeeCup[Black]’ Signed-off-by: Dave Shanley <dave@quobix.com>
This commit is contained in:
@@ -4,14 +4,14 @@
|
|||||||
package index
|
package index
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestSpecIndex_ExtractRefs_CheckDescriptionNotMap(t *testing.T) {
|
func TestSpecIndex_ExtractRefs_CheckDescriptionNotMap(t *testing.T) {
|
||||||
|
|
||||||
yml := `openapi: 3.1.0
|
yml := `openapi: 3.1.0
|
||||||
info:
|
info:
|
||||||
description: This is a description
|
description: This is a description
|
||||||
paths:
|
paths:
|
||||||
@@ -28,17 +28,17 @@ paths:
|
|||||||
description:
|
description:
|
||||||
type: string
|
type: string
|
||||||
`
|
`
|
||||||
var rootNode yaml.Node
|
var rootNode yaml.Node
|
||||||
_ = yaml.Unmarshal([]byte(yml), &rootNode)
|
_ = yaml.Unmarshal([]byte(yml), &rootNode)
|
||||||
c := CreateOpenAPIIndexConfig()
|
c := CreateOpenAPIIndexConfig()
|
||||||
idx := NewSpecIndexWithConfig(&rootNode, c)
|
idx := NewSpecIndexWithConfig(&rootNode, c)
|
||||||
assert.Len(t, idx.allDescriptions, 2)
|
assert.Len(t, idx.allDescriptions, 2)
|
||||||
assert.Equal(t, 2, idx.descriptionCount)
|
assert.Equal(t, 2, idx.descriptionCount)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSpecIndex_ExtractRefs_CheckPropertiesForInlineSchema(t *testing.T) {
|
func TestSpecIndex_ExtractRefs_CheckPropertiesForInlineSchema(t *testing.T) {
|
||||||
|
|
||||||
yml := `openapi: 3.1.0
|
yml := `openapi: 3.1.0
|
||||||
servers:
|
servers:
|
||||||
- url: http://localhost:8080
|
- url: http://localhost:8080
|
||||||
paths:
|
paths:
|
||||||
@@ -56,10 +56,33 @@ paths:
|
|||||||
type: array
|
type: array
|
||||||
items: true
|
items: true
|
||||||
`
|
`
|
||||||
var rootNode yaml.Node
|
var rootNode yaml.Node
|
||||||
_ = yaml.Unmarshal([]byte(yml), &rootNode)
|
_ = yaml.Unmarshal([]byte(yml), &rootNode)
|
||||||
c := CreateOpenAPIIndexConfig()
|
c := CreateOpenAPIIndexConfig()
|
||||||
idx := NewSpecIndexWithConfig(&rootNode, c)
|
idx := NewSpecIndexWithConfig(&rootNode, c)
|
||||||
assert.Len(t, idx.allInlineSchemaDefinitions, 2)
|
assert.Len(t, idx.allInlineSchemaDefinitions, 2)
|
||||||
assert.Len(t, idx.allInlineSchemaObjectDefinitions, 1)
|
assert.Len(t, idx.allInlineSchemaObjectDefinitions, 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// https://github.com/pb33f/libopenapi/issues/112
|
||||||
|
func TestSpecIndex_ExtractRefs_CheckReferencesWithBracketsInName(t *testing.T) {
|
||||||
|
|
||||||
|
yml := `openapi: 3.0.0
|
||||||
|
components:
|
||||||
|
schemas:
|
||||||
|
Cake[Burger]:
|
||||||
|
type: string
|
||||||
|
description: A cakey burger
|
||||||
|
Happy:
|
||||||
|
type: object
|
||||||
|
properties:
|
||||||
|
mingo:
|
||||||
|
$ref: '#/components/schemas/Cake[Burger]'
|
||||||
|
`
|
||||||
|
var rootNode yaml.Node
|
||||||
|
_ = yaml.Unmarshal([]byte(yml), &rootNode)
|
||||||
|
c := CreateOpenAPIIndexConfig()
|
||||||
|
idx := NewSpecIndexWithConfig(&rootNode, c)
|
||||||
|
assert.Len(t, idx.allMappedRefs, 1)
|
||||||
|
assert.Equal(t, "Cake[Burger]", idx.allMappedRefs["#/components/schemas/Cake[Burger]"].Name)
|
||||||
|
}
|
||||||
|
|||||||
@@ -514,6 +514,9 @@ func IsHttpVerb(verb string) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// define bracket name expression
|
||||||
|
var bracketNameExp = regexp.MustCompile("^(\\w+)\\[(\\w+)\\]$")
|
||||||
|
|
||||||
func ConvertComponentIdIntoFriendlyPathSearch(id string) (string, string) {
|
func ConvertComponentIdIntoFriendlyPathSearch(id string) (string, string) {
|
||||||
segs := strings.Split(id, "/")
|
segs := strings.Split(id, "/")
|
||||||
name, _ := url.QueryUnescape(strings.ReplaceAll(segs[len(segs)-1], "~1", "/"))
|
name, _ := url.QueryUnescape(strings.ReplaceAll(segs[len(segs)-1], "~1", "/"))
|
||||||
@@ -521,8 +524,8 @@ 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.
|
// check for strange spaces, chars and if found, wrap them up, clean them and create a new cleaned path.
|
||||||
for i := range segs {
|
for i := range segs {
|
||||||
reg, _ := regexp.MatchString("[%=;~.]", segs[i])
|
pathCharExp, _ := regexp.MatchString("[%=;~.]", segs[i])
|
||||||
if reg {
|
if pathCharExp {
|
||||||
segs[i], _ = url.QueryUnescape(strings.ReplaceAll(segs[i], "~1", "/"))
|
segs[i], _ = url.QueryUnescape(strings.ReplaceAll(segs[i], "~1", "/"))
|
||||||
segs[i] = fmt.Sprintf("['%s']", segs[i])
|
segs[i] = fmt.Sprintf("['%s']", segs[i])
|
||||||
if len(cleaned) > 0 {
|
if len(cleaned) > 0 {
|
||||||
@@ -530,6 +533,19 @@ func ConvertComponentIdIntoFriendlyPathSearch(id string) (string, string) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
|
// check for brackets in the name, and if found, rewire the path to encapsulate them
|
||||||
|
// correctly. https://github.com/pb33f/libopenapi/issues/112
|
||||||
|
brackets := bracketNameExp.FindStringSubmatch(segs[i])
|
||||||
|
|
||||||
|
if len(brackets) > 0 {
|
||||||
|
segs[i] = bracketNameExp.ReplaceAllString(segs[i], "['$1[$2]']")
|
||||||
|
if len(cleaned) > 0 {
|
||||||
|
cleaned[len(cleaned)-1] = fmt.Sprintf("%s%s", segs[i-1], segs[i])
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
intVal, err := strconv.ParseInt(segs[i], 10, 32)
|
intVal, err := strconv.ParseInt(segs[i], 10, 32)
|
||||||
if err == nil && intVal <= 99 {
|
if err == nil && intVal <= 99 {
|
||||||
segs[i] = fmt.Sprintf("[%d]", intVal)
|
segs[i] = fmt.Sprintf("[%d]", intVal)
|
||||||
|
|||||||
Reference in New Issue
Block a user