Skip to content

Commit

Permalink
chore: add more e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexVarchuk committed Oct 3, 2024
1 parent 1f8c90f commit d6515a1
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 9 deletions.
6 changes: 4 additions & 2 deletions __tests__/bundle/bundle-external-value/redocly.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
apis:
main:
root: ./test.yaml
first:
root: ./test-success.yaml
second:
root: ./test-wrong-examples.yaml

extends:
- recommended
30 changes: 26 additions & 4 deletions __tests__/bundle/bundle-external-value/snapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,36 @@ paths:
schema:
type: object
examples:
mergeRequest:
summary: Merge request example
test-resolved:
summary: Example should resolved to value
value:
foo: bar
key: value
test-no-resolved:
summary: Example shouldn't be resolved to value
value:
type: object
externalValue: ./external-value.json
components: {}
bundling ./test.yaml...
📦 Created a bundle for ./test.yaml at stdout <test>ms.
bundling ./test-success.yaml...
📦 Created a bundle for ./test-success.yaml at stdout <test>ms.
bundling ./test-wrong-examples.yaml...
[1] test-wrong-examples.yaml:27:17 at #/paths/~1/post/requestBody/content/application~1xml/examples/test-wrong-ref
Can't resolve $ref: ENOENT: no such file or directory './__tests__/bundle/bundle-external-value/external-value-bad-path.json'
25 | examples:
26 | test-wrong-ref:
27 | summary: Example shouldn't resolved to value
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
28 | externalValue: './external-value-bad-path.json'
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
29 |
Error was generated by the bundler rule.
❌ Errors encountered while bundling ./test-wrong-examples.yaml: bundle not created (use --force to ignore errors).
`;
33 changes: 33 additions & 0 deletions __tests__/bundle/bundle-external-value/test-success.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
openapi: 3.1.0
security: []
info:
version: 1.0.0
title: Example.com
termsOfService: https://example.com/terms/
contact:
email: contact@example.com
url: http://example.com/contact
license:
name: Apache 2.0
url: http://www.apache.org/licenses/LICENSE-2.0.html
description: OpenAPI description with external example
components: {}

paths:
/:
post:
summary: Test request externalValue with relative reference in examples
requestBody:
content:
application/xml:
schema:
type: object
examples:
test-resolved:
summary: Example should resolved to value
externalValue: './external-value.json'
test-no-resolved:
summary: Example shouldn't be resolved to value
value:
type: object
externalValue: './external-value.json'
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ paths:
schema:
type: object
examples:
mergeRequest:
summary: Merge request example
externalValue: './external-value.json'
test-wrong-ref:
summary: Example shouldn't resolved to value
externalValue: './external-value-bad-path.json'
11 changes: 11 additions & 0 deletions packages/core/src/bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,17 @@ function makeBundleVisitor(
Example: {
leave(node: any, ctx: UserContext) {
if (node.externalValue && node.value === undefined) {
const resolved = ctx.resolve({ $ref: node.externalValue });

if (!resolved.location || resolved.node === undefined) {
reportUnresolvedRef(resolved, ctx.report, ctx.location);
return;
}

if (keepUrlRefs && isAbsoluteUrl(node.externalValue)) {
return;
}

node.value = ctx.resolve({ $ref: node.externalValue }).node;
delete node.externalValue;
}
Expand Down

0 comments on commit d6515a1

Please sign in to comment.