Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 48 additions & 6 deletions bundler/bundler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,19 @@ import (
"sync"
"testing"

"github.com/pb33f/libopenapi/datamodel/low"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.yaml.in/yaml/v4"

"github.com/pb33f/libopenapi"
"github.com/pb33f/libopenapi/bundler/test/specs/schemawithrefs"
"github.com/pb33f/libopenapi/datamodel"
"github.com/pb33f/libopenapi/datamodel/high/base"
v3high "github.com/pb33f/libopenapi/datamodel/high/v3"
"github.com/pb33f/libopenapi/datamodel/low"
"github.com/pb33f/libopenapi/index"
"github.com/pb33f/libopenapi/orderedmap"
"github.com/pb33f/libopenapi/utils"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.yaml.in/yaml/v4"
)

// Test helper functions to reduce duplication across DigitalOcean tests
Expand Down Expand Up @@ -2209,8 +2210,8 @@ func TestCopySchemaToComponents_NameCollision(t *testing.T) {
func TestCalculateCollisionNameInline_NumericSuffix(t *testing.T) {
// Test: When filename-based name also collides, use numeric suffix
existingNames := map[string]bool{
"Cat": true,
"Cat__external": true, // Filename-based collision also exists
"Cat": true,
"Cat__external": true, // Filename-based collision also exists
"Cat__external__1": true, // First numeric suffix also taken (format: name__basename__N)
}

Expand Down Expand Up @@ -2284,3 +2285,44 @@ components:
assert.Equal(t, "#node", itemsSchema.DynamicRef, "DynamicRef should be '#node'")
}

func TestBundleDocument_Embedded(t *testing.T) {
expected, err := os.ReadFile("test/specs/schemawithrefs_expected.yaml")
require.NoError(t, err)

tests := []struct {
name string
config *datamodel.DocumentConfiguration
}{
{
name: "directory",
config: &datamodel.DocumentConfiguration{
BasePath: "test/specs/schemawithrefs",
AllowFileReferences: true,
},
},
{
name: "embed",
config: &datamodel.DocumentConfiguration{
LocalFS: schemawithrefs.Files,
AllowFileReferences: true,
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
doc, err := libopenapi.NewDocumentWithConfiguration(schemawithrefs.Schema, tt.config)
require.NoError(t, err)

t.Run("v3", func(t *testing.T) {
v3, err := doc.BuildV3Model()
require.NoError(t, err)

b, err := BundleDocument(&v3.Model)
require.NoError(t, err)

assert.Equal(t, string(b), string(expected))
})
})
}
}
4 changes: 4 additions & 0 deletions bundler/test/specs/schemawithrefs/description.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
introduction: |
# Title

Description
11 changes: 11 additions & 0 deletions bundler/test/specs/schemawithrefs/openapi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
openapi: "3.0.0"

info:
title: Test API
version: "2.0"
description:
$ref: "description.yaml#/introduction"
paths:
/v2/actions/{action_id}:
get:
$ref: "resources/actions/actions_get.yaml"
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
operationId: actions_get

summary: Retrieve an action

description: The description for this endpoint

tags:
- Actions

parameters:
- $ref: 'parameters.yaml#/action_id'

responses:
'200':
$ref: 'responses/action.yaml'

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
type: object

properties:
id:
type: integer
description: A description of id
example: 36804636

status:
type: string
description: A description of status
enum:
- started
- completed
- errored
example: completed
default: started
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
action_id:
in: path
name: action_id
description: A unique numeric ID that can be used to identify and reference an action.
required: true
schema:
type: integer
minimum: 1
example: 36804636
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
description: >-
The result will be a JSON object with an action key.
This will be set to an action object containing the standard action attributes.

content:
application/json:
schema:
properties:
action:
$ref: '../models/action.yaml'
11 changes: 11 additions & 0 deletions bundler/test/specs/schemawithrefs/schema.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package schemawithrefs

import (
"embed"
)

//go:embed openapi.yaml
var Schema []byte

//go:embed description.yaml resources
var Files embed.FS
46 changes: 46 additions & 0 deletions bundler/test/specs/schemawithrefs_expected.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
openapi: "3.0.0"
info:
title: Test API
version: "2.0"
description: ""
paths:
/v2/actions/{action_id}:
get:
operationId: actions_get
summary: Retrieve an action
description: The description for this endpoint
tags:
- Actions
parameters:
- in: path
name: action_id
description: A unique numeric ID that can be used to identify and reference an action.
required: true
schema:
type: integer
minimum: 1
example: 36804636
responses:
'200':
description: >-
The result will be a JSON object with an action key. This will be set to an action object containing the standard action attributes.
content:
application/json:
schema:
properties:
action:
type: object
properties:
id:
type: integer
description: A description of id
example: 36804636
status:
type: string
description: A description of status
enum:
- started
- completed
- errored
example: completed
default: started
18 changes: 17 additions & 1 deletion datamodel/low/v3/create_document.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,23 @@ func createDocument(info *datamodel.SpecInfo, config *datamodel.DocumentConfigur
cwd, _ = filepath.Abs(config.BasePath)
// if a supplied local filesystem is provided, add it to the rolodex.
if config.LocalFS != nil {
rolodex.AddLocalFS(cwd, config.LocalFS)
var localFS index.RolodexFS
if fs, ok := config.LocalFS.(index.RolodexFS); ok {
localFS = fs
} else {
// create a local filesystem
localFSConf := index.LocalFSConfig{
BaseDirectory: cwd,
IndexConfig: idxConfig,
FileFilters: config.FileFilter,
DirFS: config.LocalFS,
}

localFS, _ = index.NewLocalFSWithConfig(&localFSConf)
idxConfig.AllowFileLookup = true
}

rolodex.AddLocalFS(cwd, localFS)
} else {

// create a local filesystem
Expand Down
Loading