Files
libopenapi/index/extract_refs_test.go
Dave Shanley 148822fa2a 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>
2023-06-17 14:12:27 -04:00

89 lines
2.2 KiB
Go

// Copyright 2023 Princess B33f Heavy Industries / Dave Shanley
// SPDX-License-Identifier: MIT
package index
import (
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"
"testing"
)
func TestSpecIndex_ExtractRefs_CheckDescriptionNotMap(t *testing.T) {
yml := `openapi: 3.1.0
info:
description: This is a description
paths:
/herbs/and/spice:
get:
description: This is a also a description
responses:
200:
content:
application/json:
schema:
type: array
properties:
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)
}
func TestSpecIndex_ExtractRefs_CheckPropertiesForInlineSchema(t *testing.T) {
yml := `openapi: 3.1.0
servers:
- url: http://localhost:8080
paths:
/test:
get:
responses:
'200':
description: OK
content:
application/json:
schema:
type: object
properties:
test:
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)
}
// 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)
}