Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
476 changes: 275 additions & 201 deletions openapi/bundle.go

Large diffs are not rendered by default.

41 changes: 41 additions & 0 deletions openapi/bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,44 @@ func TestBundle_SiblingDirectories_Success(t *testing.T) {
// Compare the actual output with expected output
assert.Equal(t, string(expectedBytes), string(actualYAML), "Bundled document should match expected output")
}

func TestBundle_Issue50_Success(t *testing.T) {
t.Parallel()

ctx := t.Context()

// Load the input document
inputFile, err := os.Open("testdata/bundle/issue50/test/testapi.yaml")
require.NoError(t, err)
defer inputFile.Close()

inputDoc, validationErrs, err := openapi.Unmarshal(ctx, inputFile)
require.NoError(t, err)
require.Empty(t, validationErrs, "Input document should be valid")

// Configure bundling options
opts := openapi.BundleOptions{
ResolveOptions: openapi.ResolveOptions{
RootDocument: inputDoc,
TargetLocation: "testdata/bundle/issue50/test/testapi.yaml",
},
NamingStrategy: openapi.BundleNamingFilePath,
}

// Bundle all external references
err = openapi.Bundle(ctx, inputDoc, opts)
require.NoError(t, err)

// Marshal the bundled document to YAML
var buf bytes.Buffer
err = openapi.Marshal(ctx, inputDoc, &buf)
require.NoError(t, err)
actualYAML := buf.Bytes()

// Load the expected output
expectedBytes, err := os.ReadFile("testdata/bundle/issue50/expected.yaml")
require.NoError(t, err)

// Compare the actual output with expected output
assert.Equal(t, string(expectedBytes), string(actualYAML), "Bundled document should match expected output")
}
51 changes: 49 additions & 2 deletions openapi/localize.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func discoverSchemaReference(ctx context.Context, schema *oas3.JSONSchema[oas3.R
return nil
}

ref, classification := handleReference(schema.GetRef(), "", opts.TargetLocation)
ref, classification := handleLocalizeReference(schema.GetRef(), "", opts.TargetLocation)
if classification == nil || classification.IsFragment {
return nil // Skip internal references
}
Expand Down Expand Up @@ -287,7 +287,7 @@ func discoverGenericReference[T any, V interfaces.Validator[T], C marshaller.Cor
return nil
}

refStr, classification := handleReference(ref.GetReference(), "", opts.TargetLocation)
refStr, classification := handleLocalizeReference(ref.GetReference(), "", opts.TargetLocation)
if classification == nil || classification.IsFragment {
return nil // Skip internal references
}
Expand Down Expand Up @@ -833,3 +833,50 @@ func normalizeFilePath(filePath string) string {

return cleanPath
}

func handleLocalizeReference(ref references.Reference, parentLocation, targetLocation string) (string, *utils.ReferenceClassification) {
r := ref.String()

// Check if this is an external reference using the utility function
classification, err := utils.ClassifyReference(r)
if err != nil {
return "", nil // Invalid reference, skip
}

// For URLs, don't do any path manipulation - return as-is
if classification.Type == utils.ReferenceTypeURL {
return r, classification
}

if parentLocation != "" {
relPath, err := filepath.Rel(filepath.Dir(parentLocation), targetLocation)
if err == nil {
if classification.IsFragment {
r = relPath + r
} else {
if ref.GetURI() != "" {
r = filepath.Join(filepath.Dir(relPath), r)
} else {
r = filepath.Join(relPath, r)
}
}
}

// convert paths back to original separators
// detect original separators from the original reference
pathStyle := detectPathStyle(ref.String())
switch pathStyle {
case "windows":
r = strings.ReplaceAll(r, "/", "\\")
default:
r = strings.ReplaceAll(r, "\\", "/")
}

cl, err := utils.ClassifyReference(r)
if err == nil {
classification = cl
}
}

return r, classification
}
Loading
Loading