Skip to content

Commit 24a60d0

Browse files
committed
feat: fail build if ref can't be resolved
1 parent fdceb74 commit 24a60d0

File tree

19 files changed

+200
-43
lines changed

19 files changed

+200
-43
lines changed

src/apitypes/rest/rest.document.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ const openApiDocumentMeta = (data: OpenAPIV3.Document): RestDocumentInfo => {
5353
export const buildRestDocument: DocumentBuilder<OpenAPIV3.Document> = async (parsedFile, file, ctx) => {
5454
const { fileId, slug = '', publish = true, apiKind, ...fileMetadata } = file
5555

56-
const { data, dependencies } = await getBundledFileDataWithDependencies(fileId, ctx.parsedFileResolver)
56+
const { data, dependencies } = await getBundledFileDataWithDependencies(fileId, ctx.parsedFileResolver, !!ctx.config.strictValidation)
5757

5858
let bundledFileData = data
5959

src/apitypes/unknown/unknown.document.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export const buildUnknownDocument: DocumentBuilder<string> = async (parsedFile,
3333
const {
3434
data,
3535
dependencies: fileDependencies,
36-
} = await getBundledFileDataWithDependencies(fileId, ctx.parsedFileResolver)
36+
} = await getBundledFileDataWithDependencies(fileId, ctx.parsedFileResolver, !!ctx.config.strictValidation)
3737
bundledFileData = data
3838
dependencies = fileDependencies
3939
}

src/utils/document.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {
3232
} from '../types'
3333
import { bundle, Resolver } from 'api-ref-bundler'
3434
import { FILE_FORMAT_JSON, FILE_FORMAT_YAML } from '../consts'
35+
import { takeIf } from './objects'
3536

3637
export const EXPORT_FORMAT_TO_FILE_FORMAT = new Map<OperationsGroupExportFormat, FileFormat>([
3738
[YAML_EXPORT_GROUP_FORMAT, FILE_FORMAT_YAML],
@@ -120,6 +121,7 @@ export const getDocumentTitle = (fileId: string): string => {
120121
export const getBundledFileDataWithDependencies = async (
121122
fileId: FileId,
122123
parsedFileResolver: _ParsedFileResolver,
124+
throwOnBadRef: boolean,
123125
): Promise<{ data: any; dependencies: string[] }> => {
124126
const dependencies: string[] = []
125127

@@ -130,6 +132,8 @@ export const getBundledFileDataWithDependencies = async (
130132
return {}
131133
}
132134

135+
// todo: this error has no effect because it is suppressed here: https://github.com/udamir/api-ref-bundler/blob/master/src/resolver.ts#L33
136+
// move this throw to bundler hooks or you'll see just "Cannot resolve: filename" message in case of wrong file kind
133137
if (data.kind !== FILE_KIND.TEXT) {
134138
throw new Error(`Dependency with path ${filepath} is not a text file`)
135139
}
@@ -141,7 +145,14 @@ export const getBundledFileDataWithDependencies = async (
141145
return data.data
142146
}
143147

144-
const bundledFileData = await bundle(fileId, resolver)
148+
const bundledFileData = await bundle(fileId, resolver, {
149+
hooks: {
150+
...takeIf(
151+
{ onError: (message: string) => { throw new Error(message) } },
152+
throwOnBadRef,
153+
),
154+
},
155+
})
145156

146157
return { data: bundledFileData, dependencies: dependencies }
147158
}

test/bundle-dependencies.test.ts

Lines changed: 0 additions & 28 deletions
This file was deleted.

test/projects/bundle-dependencies/case2/config.json renamed to test/projects/reference-bundling/case1/config.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
{
2-
"packageId": "bundle-dependencies",
2+
"packageId": "reference-bundling",
33
"apiType": "rest",
44
"version": "v1",
55
"files": [
66
{
7-
"fileId": "dependency.yaml",
7+
"fileId": "reference.yaml",
88
"publish": true,
99
"labels": [],
1010
"commitId": "6c778b1f44200bd19944a6a8eac10a4e5a21a1cd"
1111
},
1212
{
13-
"fileId": "main.yaml",
13+
"fileId": "openapi.yaml",
1414
"publish": true,
1515
"labels": [],
1616
"commitId": "6c778b1f44200bd19944a6a8eac10a4e5a21a1xd"

test/projects/bundle-dependencies/case1/main.yaml renamed to test/projects/reference-bundling/case1/openapi.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ paths:
1313
schema:
1414
type: array
1515
items:
16-
$ref: './dependency.yaml#/User'
16+
$ref: './reference.yaml#/User'

test/projects/bundle-dependencies/case2/dependency.yaml renamed to test/projects/reference-bundling/case1/reference.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,3 @@ User:
33
properties:
44
id:
55
type: integer
6-
name:
7-
type: string
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"packageId": "reference-bundling",
3+
"apiType": "rest",
4+
"version": "v1",
5+
"files": [
6+
{
7+
"fileId": "openapi.yaml",
8+
"publish": true,
9+
"labels": [],
10+
"commitId": "6c778b1f44200bd19944a6a8eac10a4e5a21a1xd"
11+
}
12+
]
13+
}

test/projects/bundle-dependencies/case2/main.yaml renamed to test/projects/reference-bundling/case2/openapi.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ paths:
1313
schema:
1414
type: array
1515
items:
16-
$ref: './non-existent-dependency.yaml#/User'
16+
$ref: './non-existent-reference.yaml#/User'
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"packageId": "reference-bundling",
3+
"apiType": "rest",
4+
"version": "v1",
5+
"files": [
6+
{
7+
"fileId": "transitive-reference.yaml",
8+
"publish": true,
9+
"labels": [],
10+
"commitId": "6c778b1f44200bd19944a6a8eac10a4e5a21a1cd"
11+
},
12+
{
13+
"fileId": "reference.yaml",
14+
"publish": true,
15+
"labels": [],
16+
"commitId": "6c778b1f44200bd19944a6a8eac10a4e5a21a1cd"
17+
},
18+
{
19+
"fileId": "openapi.yaml",
20+
"publish": true,
21+
"labels": [],
22+
"commitId": "6c778b1f44200bd19944a6a8eac10a4e5a21a1xd"
23+
}
24+
]
25+
}

0 commit comments

Comments
 (0)