Skip to content

Commit 592610d

Browse files
committed
WIP: Test to show bundling failure with embeded directory
1 parent b7fe7bb commit 592610d

File tree

8 files changed

+136
-6
lines changed

8 files changed

+136
-6
lines changed

bundler/bundler_test.go

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"bytes"
88
"crypto/sha256"
99
"fmt"
10+
"io/fs"
1011
"log"
1112
"log/slog"
1213
"net/http"
@@ -20,18 +21,19 @@ import (
2021
"sync"
2122
"testing"
2223

23-
"github.com/pb33f/libopenapi/datamodel/low"
24+
"github.com/stretchr/testify/assert"
25+
"github.com/stretchr/testify/require"
26+
"go.yaml.in/yaml/v4"
2427

2528
"github.com/pb33f/libopenapi"
29+
"github.com/pb33f/libopenapi/bundler/internal/schemawithrefs"
2630
"github.com/pb33f/libopenapi/datamodel"
2731
"github.com/pb33f/libopenapi/datamodel/high/base"
2832
v3high "github.com/pb33f/libopenapi/datamodel/high/v3"
33+
"github.com/pb33f/libopenapi/datamodel/low"
2934
"github.com/pb33f/libopenapi/index"
3035
"github.com/pb33f/libopenapi/orderedmap"
3136
"github.com/pb33f/libopenapi/utils"
32-
"github.com/stretchr/testify/assert"
33-
"github.com/stretchr/testify/require"
34-
"go.yaml.in/yaml/v4"
3537
)
3638

3739
// Test helper functions to reduce duplication across DigitalOcean tests
@@ -2209,8 +2211,8 @@ func TestCopySchemaToComponents_NameCollision(t *testing.T) {
22092211
func TestCalculateCollisionNameInline_NumericSuffix(t *testing.T) {
22102212
// Test: When filename-based name also collides, use numeric suffix
22112213
existingNames := map[string]bool{
2212-
"Cat": true,
2213-
"Cat__external": true, // Filename-based collision also exists
2214+
"Cat": true,
2215+
"Cat__external": true, // Filename-based collision also exists
22142216
"Cat__external__1": true, // First numeric suffix also taken (format: name__basename__N)
22152217
}
22162218

@@ -2284,3 +2286,53 @@ components:
22842286
assert.Equal(t, "#node", itemsSchema.DynamicRef, "DynamicRef should be '#node'")
22852287
}
22862288

2289+
func TestBundleDocument_Files(t *testing.T) {
2290+
tests := []struct {
2291+
name string
2292+
config *datamodel.DocumentConfiguration
2293+
}{
2294+
{
2295+
name: "directory",
2296+
config: &datamodel.DocumentConfiguration{
2297+
BasePath: "internal/schemawithrefs",
2298+
AllowFileReferences: true,
2299+
},
2300+
},
2301+
{
2302+
name: "embed",
2303+
config: &datamodel.DocumentConfiguration{
2304+
LocalFS: &loggingFS{
2305+
t: t,
2306+
delegate: schemawithrefs.Files,
2307+
},
2308+
AllowFileReferences: true,
2309+
},
2310+
},
2311+
}
2312+
2313+
for _, tt := range tests {
2314+
t.Run(tt.name, func(t *testing.T) {
2315+
doc, err := libopenapi.NewDocumentWithConfiguration(schemawithrefs.Schema, tt.config)
2316+
require.NoError(t, err)
2317+
2318+
v3, err := doc.BuildV3Model()
2319+
require.NoError(t, err)
2320+
2321+
b, err := BundleDocument(&v3.Model)
2322+
require.NoError(t, err)
2323+
2324+
t.Log(string(b))
2325+
})
2326+
}
2327+
}
2328+
2329+
type loggingFS struct {
2330+
t testing.TB
2331+
delegate fs.FS
2332+
}
2333+
2334+
func (f *loggingFS) Open(name string) (fs.File, error) {
2335+
file, err := f.delegate.Open(name)
2336+
f.t.Logf("loggingFS: open %q: %v", name, err)
2337+
return file, err
2338+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
introduction: |
2+
# Title
3+
4+
Description
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
openapi: "3.0.0"
2+
3+
info:
4+
title: Test API
5+
version: "2.0"
6+
description:
7+
$ref: "description.yaml#/introduction"
8+
paths:
9+
/v2/actions/{action_id}:
10+
get:
11+
$ref: "resources/actions/actions_get.yaml"
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
operationId: actions_get
2+
3+
summary: Retrieve an action
4+
5+
description: The description for this endpoint
6+
7+
tags:
8+
- Actions
9+
10+
parameters:
11+
- $ref: 'parameters.yaml#/action_id'
12+
13+
responses:
14+
'200':
15+
$ref: 'responses/action.yaml'
16+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
type: object
2+
3+
properties:
4+
id:
5+
type: integer
6+
description: A description of id
7+
example: 36804636
8+
9+
status:
10+
type: string
11+
description: A description of status
12+
enum:
13+
- started
14+
- completed
15+
- errored
16+
example: completed
17+
default: started
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
action_id:
2+
in: path
3+
name: action_id
4+
description: A unique numeric ID that can be used to identify and reference an action.
5+
required: true
6+
schema:
7+
type: integer
8+
minimum: 1
9+
example: 36804636
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
description: >-
2+
The result will be a JSON object with an action key.
3+
This will be set to an action object containing the standard action attributes.
4+
5+
content:
6+
application/json:
7+
schema:
8+
properties:
9+
action:
10+
$ref: '../models/action.yaml'
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package schemawithrefs
2+
3+
import (
4+
"embed"
5+
)
6+
7+
//go:embed openapi.yaml
8+
var Schema []byte
9+
10+
//go:embed description.yaml resources
11+
var Files embed.FS

0 commit comments

Comments
 (0)