mirror of
https://github.com/LukeHagar/libopenapi.git
synced 2025-12-06 04:20:11 +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
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"gopkg.in/yaml.v3"
|
||||
"testing"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"gopkg.in/yaml.v3"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSpecIndex_ExtractRefs_CheckDescriptionNotMap(t *testing.T) {
|
||||
|
||||
yml := `openapi: 3.1.0
|
||||
yml := `openapi: 3.1.0
|
||||
info:
|
||||
description: This is a description
|
||||
paths:
|
||||
@@ -28,17 +28,17 @@ paths:
|
||||
description:
|
||||
type: string
|
||||
`
|
||||
var rootNode yaml.Node
|
||||
_ = yaml.Unmarshal([]byte(yml), &rootNode)
|
||||
c := CreateOpenAPIIndexConfig()
|
||||
idx := NewSpecIndexWithConfig(&rootNode, c)
|
||||
assert.Len(t, idx.allDescriptions, 2)
|
||||
assert.Equal(t, 2, idx.descriptionCount)
|
||||
var rootNode yaml.Node
|
||||
_ = yaml.Unmarshal([]byte(yml), &rootNode)
|
||||
c := CreateOpenAPIIndexConfig()
|
||||
idx := NewSpecIndexWithConfig(&rootNode, c)
|
||||
assert.Len(t, idx.allDescriptions, 2)
|
||||
assert.Equal(t, 2, idx.descriptionCount)
|
||||
}
|
||||
|
||||
func TestSpecIndex_ExtractRefs_CheckPropertiesForInlineSchema(t *testing.T) {
|
||||
|
||||
yml := `openapi: 3.1.0
|
||||
yml := `openapi: 3.1.0
|
||||
servers:
|
||||
- url: http://localhost:8080
|
||||
paths:
|
||||
@@ -56,10 +56,33 @@ paths:
|
||||
type: array
|
||||
items: true
|
||||
`
|
||||
var rootNode yaml.Node
|
||||
_ = yaml.Unmarshal([]byte(yml), &rootNode)
|
||||
c := CreateOpenAPIIndexConfig()
|
||||
idx := NewSpecIndexWithConfig(&rootNode, c)
|
||||
assert.Len(t, idx.allInlineSchemaDefinitions, 2)
|
||||
assert.Len(t, idx.allInlineSchemaObjectDefinitions, 1)
|
||||
}
|
||||
var rootNode yaml.Node
|
||||
_ = yaml.Unmarshal([]byte(yml), &rootNode)
|
||||
c := CreateOpenAPIIndexConfig()
|
||||
idx := NewSpecIndexWithConfig(&rootNode, c)
|
||||
assert.Len(t, idx.allInlineSchemaDefinitions, 2)
|
||||
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
|
||||
}
|
||||
|
||||
// define bracket name expression
|
||||
var bracketNameExp = regexp.MustCompile("^(\\w+)\\[(\\w+)\\]$")
|
||||
|
||||
func ConvertComponentIdIntoFriendlyPathSearch(id string) (string, string) {
|
||||
segs := strings.Split(id, "/")
|
||||
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.
|
||||
for i := range segs {
|
||||
reg, _ := regexp.MatchString("[%=;~.]", segs[i])
|
||||
if reg {
|
||||
pathCharExp, _ := regexp.MatchString("[%=;~.]", segs[i])
|
||||
if pathCharExp {
|
||||
segs[i], _ = url.QueryUnescape(strings.ReplaceAll(segs[i], "~1", "/"))
|
||||
segs[i] = fmt.Sprintf("['%s']", segs[i])
|
||||
if len(cleaned) > 0 {
|
||||
@@ -530,6 +533,19 @@ func ConvertComponentIdIntoFriendlyPathSearch(id string) (string, string) {
|
||||
continue
|
||||
}
|
||||
} 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)
|
||||
if err == nil && intVal <= 99 {
|
||||
segs[i] = fmt.Sprintf("[%d]", intVal)
|
||||
|
||||
Reference in New Issue
Block a user