diff --git a/datamodel/high/v3/document_test.go b/datamodel/high/v3/document_test.go index def6ce0..e01ab0e 100644 --- a/datamodel/high/v3/document_test.go +++ b/datamodel/high/v3/document_test.go @@ -6,6 +6,8 @@ package v3 import ( "fmt" "github.com/pb33f/libopenapi/datamodel" + v2 "github.com/pb33f/libopenapi/datamodel/high/v2" + lowv2 "github.com/pb33f/libopenapi/datamodel/low/v2" lowv3 "github.com/pb33f/libopenapi/datamodel/low/v3" "github.com/stretchr/testify/assert" "io/ioutil" @@ -364,7 +366,18 @@ func TestStripeAsDoc(t *testing.T) { lowDoc, err = lowv3.CreateDocument(info) assert.Len(t, err, 23) d := NewDocument(lowDoc) - fmt.Println(d) + assert.NotNil(t, d) +} + +func TestK8sAsDoc(t *testing.T) { + data, _ := ioutil.ReadFile("../../../test_specs/k8s.json") + info, _ := datamodel.ExtractSpecInfo(data) + var err []error + lowSwag, err := lowv2.CreateDocument(info) + d := v2.NewSwaggerDocument(lowSwag) + assert.Len(t, err, 1) + assert.NotNil(t, d) + } func TestAsanaAsDoc(t *testing.T) { diff --git a/index/spec_index_test.go b/index/spec_index_test.go index b422aa4..53c085a 100644 --- a/index/spec_index_test.go +++ b/index/spec_index_test.go @@ -323,8 +323,8 @@ func TestSpecIndex_BurgerShopMixedRef(t *testing.T) { index := NewSpecIndex(&rootNode) - assert.Len(t, index.allRefs, 4) - assert.Len(t, index.allMappedRefs, 4) + assert.Len(t, index.allRefs, 5) + assert.Len(t, index.allMappedRefs, 5) assert.Equal(t, 5, index.GetPathCount()) assert.Equal(t, 5, index.GetOperationCount()) assert.Equal(t, 1, index.GetComponentSchemaCount()) @@ -631,10 +631,12 @@ func TestSpecIndex_parameterReferencesHavePaths(t *testing.T) { yml := `paths: /: parameters: + - $ref: '#/components/parameters/param1' - $ref: '#/components/parameters/param1' - $ref: 'paramour.yaml#/components/parameters/param3' get: parameters: + - $ref: '#/components/parameters/param2' - $ref: '#/components/parameters/param2' - name: test in: query @@ -674,7 +676,7 @@ components: assert.Equal(t, "$.components.parameters.param2", params["/"]["get"]["#/components/parameters/param2"].Path) } if assert.Contains(t, params["/"]["get"], "test") { - assert.Equal(t, "$.paths./.get.parameters[1]", params["/"]["get"]["test"].Path) + assert.Equal(t, "$.paths./.get.parameters[2]", params["/"]["get"]["test"].Path) } } } diff --git a/resolver/resolver_test.go b/resolver/resolver_test.go index 7fc6ba2..e023cc9 100644 --- a/resolver/resolver_test.go +++ b/resolver/resolver_test.go @@ -156,7 +156,7 @@ func TestResolver_ResolveComponents_MixedRef(t *testing.T) { assert.NotNil(t, resolver) circ := resolver.Resolve() - assert.Len(t, circ, 2) + assert.Len(t, circ, 10) } diff --git a/test_specs/mixedref-burgershop.openapi.yaml b/test_specs/mixedref-burgershop.openapi.yaml index fa10a45..a722ee0 100644 --- a/test_specs/mixedref-burgershop.openapi.yaml +++ b/test_specs/mixedref-burgershop.openapi.yaml @@ -229,6 +229,12 @@ paths: application/json: schema: $ref: '#/components/schemas/Error' + "400": + description: Unexpected error. Sorry. + content: + application/json: + schema: + $ref: 'https://raw.githubusercontent.com/daveshanley/vacuum/main/model/test_files/burgershop.openapi.yaml' components: schemas: Error: diff --git a/test_specs/single-definition.yaml b/test_specs/single-definition.yaml new file mode 100644 index 0000000..975d7e5 --- /dev/null +++ b/test_specs/single-definition.yaml @@ -0,0 +1,9 @@ +components: + schemas: + Thing: + type: object + description: A thing that does nothing. + properties: + message: + type: string + description: I am pointless. \ No newline at end of file diff --git a/utils/utils.go b/utils/utils.go index fbf2b3d..395579d 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -493,32 +493,21 @@ func ConvertComponentIdIntoFriendlyPathSearch(id string) (string, string) { cleaned[len(cleaned)-1] = fmt.Sprintf("%s%s", segs[i-1], segs[i]) continue } - cleaned = append(cleaned, fmt.Sprintf("%s%s", segs[i], segs[i])) - continue } else { intVal, err := strconv.ParseInt(segs[i], 10, 32) if err == nil && intVal <= 99 { segs[i] = fmt.Sprintf("[%d]", intVal) - if i < len(cleaned) { - cleaned[len(cleaned)-1] = fmt.Sprintf("%s%s", segs[i-1], segs[i]) - } else { - cleaned[len(cleaned)-1] = fmt.Sprintf("%s%s", cleaned[len(cleaned)-1], segs[i]) - } + cleaned[len(cleaned)-1] = fmt.Sprintf("%s%s", cleaned[len(cleaned)-1], segs[i]) continue } if err == nil && intVal > 99 { segs[i] = fmt.Sprintf("['%d']", intVal) - if i < len(cleaned) { - cleaned[len(cleaned)-1] = fmt.Sprintf("%s%s", segs[i-1], segs[i]) - } else { - cleaned[len(cleaned)-1] = fmt.Sprintf("%s%s", cleaned[len(cleaned)-1], segs[i]) - } + cleaned[len(cleaned)-1] = fmt.Sprintf("%s%s", cleaned[len(cleaned)-1], segs[i]) continue } cleaned = append(cleaned, segs[i]) } } - _, err := strconv.ParseInt(name, 10, 32) var replaced string if err != nil { diff --git a/utils/utils_test.go b/utils/utils_test.go index 21e4a54..f430d3e 100644 --- a/utils/utils_test.go +++ b/utils/utils_test.go @@ -621,11 +621,17 @@ func TestConvertComponentIdIntoFriendlyPathSearch_Crazy(t *testing.T) { } func TestConvertComponentIdIntoFriendlyPathSearch_Simple(t *testing.T) { - segment, path := ConvertComponentIdIntoFriendlyPathSearch("#//~1fresh~1pizza/get") - assert.Equal(t, "$.['/fresh/pizza'].get", path) + segment, path := ConvertComponentIdIntoFriendlyPathSearch("#/~1fresh~1pizza/get") + assert.Equal(t, "$['/fresh/pizza'].get", path) assert.Equal(t, "get", segment) } +func TestConvertComponentIdIntoFriendlyPathSearch_Params(t *testing.T) { + segment, path := ConvertComponentIdIntoFriendlyPathSearch("#/why/0") + assert.Equal(t, "$.why[0]", path) + assert.Equal(t, "0", segment) +} + func TestConvertComponentIdIntoFriendlyPathSearch_Crazy_Github(t *testing.T) { segment, path := ConvertComponentIdIntoFriendlyPathSearch("#/paths/~1crazy~1ass~1references/get/responses/404/content/application~1xml;%20charset=utf-8/schema") assert.Equal(t, "$.paths['/crazy/ass/references'].get.responses['404'].content['application/xml; charset=utf-8'].schema", path) @@ -650,6 +656,12 @@ func TestConvertComponentIdIntoFriendlyPathSearch_CrazyShort(t *testing.T) { assert.Equal(t, "/crazy/ass/references", segment) } +func TestConvertComponentIdIntoFriendlyPathSearch_Short(t *testing.T) { + segment, path := ConvertComponentIdIntoFriendlyPathSearch("/~1crazy~1ass~1references") + assert.Equal(t, "$['/crazy/ass/references']", path) + assert.Equal(t, "/crazy/ass/references", segment) +} + func TestConvertComponentIdIntoFriendlyPathSearch_Array(t *testing.T) { segment, path := ConvertComponentIdIntoFriendlyPathSearch("#/paths/~1crazy~1ass~1references/get/parameters/0") assert.Equal(t, "$.paths['/crazy/ass/references'].get.parameters[0]", path)